By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 2,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

BackgroundWorker and events handling

P: n/a
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;
}

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy
Oct 2 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Hardy Wang" <ha*******@newsgroups.nospamwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
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;
}

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy

Oct 2 '06 #2

P: n/a
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 3 '06 #3

P: n/a
Thanks a lot!

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Hardy Wang" <ha*******@newsgroups.nospamwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
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;
}

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy


Oct 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.