F
Fred Strauss
I have a question regarding multi-threading in a Windows Forms
application.
I am writing a simple application to merge together a group of
files. I have created a class that holds the list of files and
contains a method to actually merge the files together.
The list is built and manipulated using the provided user interface.
In order to provide progress updates while the merge operation is
taking place I have created 3 events the merge object will fire as the
merge proceeds. The reason for the events is I did not want the merge
object to have any intrinsic knowledge of the UI components being
updated
I have set up event handlers on my form to handle these events and
update the UI. Specifically the handlers update a progress bar and
label control on the form. This sort of works when the Merge() method
is invoked on the UI thread in that I see the progress bar updates but
not the label update. I suspect this is because the Merge() method
never allows the UI to be updated properly while it's running on the
same thread.
So...I would like to be able to invoke the merge method on a new
thread. I realize I can do this by creating a delegate for the Merge()
method and using BeginInvoke() to call it. However, I also understand
it's not kosher to update UI elements on anything but the UI thread so
I'm wondering what happens when the Merge() method called on the
non-UI thread starts firing events?
Are the registered event handlers invoked on the thread the Merge()
method is running on or on the UI thread?
Is invoking an event handler from a UI object on a non-UI thread OK?
If they are invoked on the non-UI thread how should I go about
updating the UI elements. Do I need to create an UpdateUI method and
call it using Invoke()?
Thanks for any help!
Fred
application.
I am writing a simple application to merge together a group of
files. I have created a class that holds the list of files and
contains a method to actually merge the files together.
The list is built and manipulated using the provided user interface.
In order to provide progress updates while the merge operation is
taking place I have created 3 events the merge object will fire as the
merge proceeds. The reason for the events is I did not want the merge
object to have any intrinsic knowledge of the UI components being
updated
I have set up event handlers on my form to handle these events and
update the UI. Specifically the handlers update a progress bar and
label control on the form. This sort of works when the Merge() method
is invoked on the UI thread in that I see the progress bar updates but
not the label update. I suspect this is because the Merge() method
never allows the UI to be updated properly while it's running on the
same thread.
So...I would like to be able to invoke the merge method on a new
thread. I realize I can do this by creating a delegate for the Merge()
method and using BeginInvoke() to call it. However, I also understand
it's not kosher to update UI elements on anything but the UI thread so
I'm wondering what happens when the Merge() method called on the
non-UI thread starts firing events?
Are the registered event handlers invoked on the thread the Merge()
method is running on or on the UI thread?
Is invoking an event handler from a UI object on a non-UI thread OK?
If they are invoked on the non-UI thread how should I go about
updating the UI elements. Do I need to create an UpdateUI method and
call it using Invoke()?
Thanks for any help!
Fred