BackgroundWorker and events handling

H

Hardy Wang

Hi all,
I am migrating a Windows Form application from .Net 1.1 to 2.0. I try to
use BackgroundWorker object to handle a very lengthy process.
I have a separated class to handle some very complex logic. In .Net 1.1,
I create some events inside this class to notify WinForm the status of
process:

obj.OneFileStarted += new OneFileStartedEventHandler(obj_OneFileStarted);
obj.OneFileFinished += new OneFileFinishedEventHandler(obj_OneFileFinished);
obj.AllFinished += new AllFinishedEventHandler(obj_AllFinished);
obj.ErrorHappened += new ErrorHappenedEventHandler(obj_ErrorHappened);
.......
System.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(obj.LengthyJob));
t.Start();

Then in my WinForm class, I just hook these events to run the UI content
change based on status/progress of object processing method.

In new BackgroundWorker model, what is the proper way to notify UI of
different statuses/events? Because I only see a ReportProgress method which
can talk with outside world. I need more than this. If I don't make any
change, still use old way to receive events, I get "Cross-thread operation
not valid: Control 'labelFileName' accessed from a thread other than the
thread it was created on." exception, even though I change values of
Controls inside my event handler methods of WinForm.

private void obj_OneFileStarted(object sender, ProcessFileArgs e) {
// I have exception here: Cross-thread operation not valid: Control
'labelFileName' accessed from a thread other than the thread it was created
on.
labelFileName.Text = e.FileName;
}
 
N

Nicholas Paldino [.NET/C# MVP]

Hardy,

It looks like you were being bad in .NET 1.1. In .NET 1.1, the Control
class didn't check to see if it's members were accessing it on the proper
thread. In .NET 2.0, they have changed this.

If you want to use the BackgroundWorker class, then in your operation,
when you call the ReportProgress event, you want to pass an object for the
userState parameter. This is what you will use to indicate what you should
do in the event handler that will process the event on the UI thread.

Hope this helps.
 
J

Jeffrey Tan[MSFT]

Hi Hardy,

I have added a reply to you for this issue in
microsoft.public.dotnet.framework.clr newsgroup, please check it there.
Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks a lot!

Nicholas Paldino said:
Hardy,

It looks like you were being bad in .NET 1.1. In .NET 1.1, the Control
class didn't check to see if it's members were accessing it on the proper
thread. In .NET 2.0, they have changed this.

If you want to use the BackgroundWorker class, then in your operation,
when you call the ReportProgress event, you want to pass an object for the
userState parameter. This is what you will use to indicate what you should
do in the event handler that will process the event on the UI thread.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hardy Wang said:
Hi all,
I am migrating a Windows Form application from .Net 1.1 to 2.0. I try
to use BackgroundWorker object to handle a very lengthy process.
I have a separated class to handle some very complex logic. In .Net
1.1, I create some events inside this class to notify WinForm the status
of process:

obj.OneFileStarted += new OneFileStartedEventHandler(obj_OneFileStarted);
obj.OneFileFinished += new
OneFileFinishedEventHandler(obj_OneFileFinished);
obj.AllFinished += new AllFinishedEventHandler(obj_AllFinished);
obj.ErrorHappened += new ErrorHappenedEventHandler(obj_ErrorHappened);
......
System.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(obj.LengthyJob));
t.Start();

Then in my WinForm class, I just hook these events to run the UI
content change based on status/progress of object processing method.

In new BackgroundWorker model, what is the proper way to notify UI of
different statuses/events? Because I only see a ReportProgress method
which can talk with outside world. I need more than this. If I don't make
any change, still use old way to receive events, I get "Cross-thread
operation not valid: Control 'labelFileName' accessed from a thread other
than the thread it was created on." exception, even though I change values
of Controls inside my event handler methods of WinForm.

private void obj_OneFileStarted(object sender, ProcessFileArgs e) {
// I have exception here: Cross-thread operation not valid: Control
'labelFileName' accessed from a thread other than the thread it was
created on.
labelFileName.Text = e.FileName;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top