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;
}
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;
}