简体   繁体   中英

GUI Update with multiple Backgroundworker C#

Here i'm using multiple backgroundworker s to do one network related operation.Basically its a process of checking the user is registered with my page or not. I'm having nearly 1000 accounts to check, hence i'm using datagridview to import users and fetch the username from same for checking purpose. My code working fine and displays the result, But the problem is when updating the status in datagridview its not that much effective. In that long process method i used to set status text for every process of method, like login process started logged in failed to login .But Backgroundworker does not updating the status column. It displaying the status after all backgroundworker completes only. Can anyone give me an idea for how to update the status for each account ?? thanks in advance

It my code:

int threadNum;
    public  BackgroundWorker bcheker;
    private void toolStripButton2_Click(object sender, EventArgs e)
    {
        if (wbcheckStatus == WorkerStatus.NotStarted || wblogcheckStatus == WorkerStatus.Completed)
        {
            threadNum = -1;
            SetControlsStatus(ProgramStatus.BChecking);
            toolStripButton2.Image = aTumblr.Properties.Resources.control_stop;
            for (int i = 0; i < 4; i++)
            {
                bcheker = new BackgroundWorker();
                bcheker.DoWork += new DoWorkEventHandler(bcheker_dowork);
                bcheker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bchecker_completed);
                bcheker.ProgressChanged +=bcheker_ProgressChanged;
                bcheker.WorkerReportsProgress = true;
                bcheker.WorkerSupportsCancellation = true;
                bcheker.RunWorkerAsync();
            }
        }
        else
        {
            threadNum = 10000;
            bcheker.CancelAsync();
            SetControlsStatus(ProgramStatus.BlogChecking);
        }
    }       
    public void bcheker_dowork(object sender, DoWorkEventArgs e)
    {
        while (!bcheker.CancellationPending)
        {
            int rownum = Interlocked.Increment(ref threadNum);
            if (rownum >= bchecktableModel.Rows.Count)
            {
                break;
            }
            Thread.Sleep(1000);
            BlogChecker bc = new BlogChecker(bchecktableModel[rownum, 1].Text, bchecktableModel[rownum, 2]);
            bc.check();                
            wblogcheckStatus = WorkerStatus.Running;

        }
        if (bcheker.CancellationPending)
        {
            wblogcheckStatus = WorkerStatus.Completed;
            SetControlsStatus(ProgramStatus.BCheckingDone);

        }
    }

    public void bcheker_ProgressChanged(Object sender, ProgressChangedEventArgs e)
    {

    }

    public void bchecker_completed(object sender, EventArgs e)
    {
        if (threadNum == bchecktableModel.Rows.Count+1)
        {
            SetControlsStatus(ProgramStatus.BCheckingDone);

            wblogcheckStatus = WorkerStatus.Completed;
        }
    }

First of all, It looks like you have an issue of multiple handlers registering to the DoWorkEvent.
I am not sure what is happening, but I would check How many time the bcheker_dowork method is being called.

In order to report about your progress, use the BackgroundWorker.ReportProgress method.

public void bcheker_ProgressChanged(Object sender, ProgressChangedEventArgs e)
{
    //Update your progress here
}

Also, in your DoWork() method, you shouldn't be using "bcheker" as that will simply refer to the last BackgroundWorker created. Instead, cast the sender parameter to a local BackgroundWorker variable and use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM