How to make BackgroundWorker ProgressChanged events execute in sequence?

BackgroundWorker raises ProgressChanged events on the current SynchronizationContext of the thread that called RunWorkerAsync().

BackgroundWorker raises ProgressChanged events on the current SynchronizationContext of the thread that called RunWorkerAsync(). The default SynchronizationContext runs callbacks on the ThreadPool without any synchronization. If you use BackgroundWorker in a UI application (WPF or WinForms), it will use that UI platform's SynchronizationContext, which will execute callbacks in order.

I have created the sample as a console application with target . NET Framework 3.5. I have changed the output type to Windows application an it worked as you have described. – Andris Nov 7 at 16:48 My issue is that I would like the whole body of the ProgressChanged event to execute in the order I have called them.

I have update the question to reflect better what I would like to do. – Andris Nov 7 at 16:54 @Andris: Under a UI SynchronizationContext, they will execute in order. – SLaks Nov 7 at 16:55 @SLaks: As I said I have changed the application output type to a Windows application, but please not that in the output in my revised example the first 5 comes before the first 4, so the call with parameter 5 was executed before the one with parameter 4.

Isn't changing the output type enough? I'm not sure how to set the SynchronizationContext to a "UI SynchronizationContext". I'm not familiar with the topic.

– Andris Nov 7 at 17:15 2 You need to run a UI thread by calling Application.Run(). (this is a blocking call). The project type doesn't matter at all.

However, if you're not doing UI, you probably shouldn't be using a BackgroundWorker at all. – SLaks Nov 7 at 17:20.

Do not use this solution! May lead to deadlocks as SLaks has pointed it out. I seem to have stumbled upon an answer.

I changed the code the following way: MethodImpl(MethodImplOptions. Synchronized) static void bg_ProgressChanged(object sender, ProgressChangedEventArgs e) { Console. WriteLine(e.

ProgressPercentage); Thread. Sleep(100); Console. WriteLine(e.

ProgressPercentage); } and now I get the output I want: 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9.

In a UI application, this will make no difference, since the callbacks will only run on the UI thread. – SLaks Nov 7 at 17:26 1 Note that this can cause deadlocks. MethodImpl(MethodImplOptions.

Synchronized) is equivalent to lock(this) and should not be used. – SLaks Nov 7 at 17:26 @SLaks: Good tip. Thanks again.

– Andris Nov 7 at 17:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions