简体   繁体   中英

C# BackgroundWorker.ProgressChanged not fired immediately after ReportProgress is called

The code looks like this:

class Mainwindow: Window{ 

int progress = 0;

public void sendrequest(){

    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += new DoWorkEventHandler(worker_DoWork);
    worker.ProgressChanged += new ProgressChangedEventHandler (             
    worker_ProgressChanged);
    worker.WorkerReportsProgress = true;
    worker.RunWorkerAsync();

    //...Here I sent requests to Bloomberg API 
    // Each request is handled by EventHandler

    session.SendRequest(request, null);
}


public void EventHandler(Event eventObject, Session session){
    progress ++;
    Console.WriteLine (process);
}

void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            Console.WriteLine("Doing work! calling progress report!");
            (sender as BackgroundWorker).ReportProgress(progress, null);
            Thread.Sleep(100);
        }

    }

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine("Started Change! " + e.ProgressPercentage);
        pg1.Value = e.ProgressPercentage;
    }

The output looks like:

Doing work! calling progress report!
0
1
Doing work! calling progress report!
2
3
4
5
Doing work! calling progress report!
6
7
8
9
10
Doing work! calling progress report!
11
12
13
14
Doing work! calling progress report!
15
16
17
18
19
Doing work! calling progress report!
20
....

But my worker_progressChanged was never called until the end of the loop (int i = 0; i < 100; i++), and my progress bar was hence never updated and stays at 0. To my understanding, the ProgressChanged belongs to UI thread. What should I do to have my ProcessChanged fired immediately after ReportProgress is called?

From msdn :

The call to the ReportProgress method is asynchronous and returns immediately

It doesn't block the for loop inside DoWork .

If you want to report progress synchronously, then you can use synchronous Invoke method yourself (see eg this ). In such case you don't even need BackgroundWorker , just another Thread .

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