sending event data between UI and worker threads?

T

Tim Mackey

hi,
i have a worker thread that raises an event. i'm sending an object as the
sender of the event.
my UI thread is listening for the event, and i'm wondering if it will screw
up the UI thread if it responds to an event raised from a worker thread, and
accesses the object sent with the event?

i'm hoping .NET has smoothed over this issue but i'd like to hear from the
experts that its an ok thing to do before i go ahead and implement it in my
app.

many thanks
tim
 
M

Miha Markic

Hi Tim,

Tim Mackey said:
hi,
i have a worker thread that raises an event. i'm sending an object as the
sender of the event.
my UI thread is listening for the event, and i'm wondering if it will screw
up the UI thread if it responds to an event raised from a worker thread, and
accesses the object sent with the event?

You do realize that if UI thread just "responds" to event the code is run on
worker thread and not in UI thread?
To jump to UI thread you'll need to call Invoke method.
About the problems:
It depends on what worker thread is doing at the same time with objects that
are accessed by UI thread.
You should synchronize access to those objects (monitor, critical section or
something else).
 
T

Tim Mackey

hi Miha,
thank you for your reply. i was hoping that because the event listener was
established on the UI thread, it would also respond on the UI thread, but
i'm assuming this is not the case from your reply.
i have put together the following code, would you be kind enough to comment
on its thread-safety?

// event wire-up on form
addDocWizard.DocumentUploaded += new EventHandler(this.OnDocumentUploaded);

....

delegate void OnDocumentUploadedDelegate(object sender, EventArgs e);
private void OnDocumentUploaded(object sender, EventArgs e)
{
if(this.InvokeRequired)
{
// switch to UI thread
OnDocumentUploadedDelegate del = new
OnDocumentUploadedDelegate(OnDocumentUploaded);
BeginInvoke(del, new object[]{sender, e});
}
else
{
// definitely running on UI thread.
// run code safely...
}
}


many thanks
tim.
 
M

Miha Markic

Hi Tim,

Tim Mackey said:
hi Miha,
thank you for your reply. i was hoping that because the event listener was
established on the UI thread, it would also respond on the UI thread, but
i'm assuming this is not the case from your reply.

Right assumption.
i have put together the following code, would you be kind enough to comment
on its thread-safety?

Your code just shows the thread synchronization (event->into UI thread)
which is correct.
However it does not show the access to common data...
 
1

100

Hi Tim,
As long as the event handler is running on the worker thread the UI thread
is not involved in anything. Strictly speaking you don't send events to the
UI thread. However I suppose that the code in the event handler will use
some resources that the UI thread might use at the same time so you have to
make sure no race situations will occur.
To answer you question: You won't screw up the UI thread more then you might
screw up any other thread as long as you don't update the user interface
from the event handler.


B\rgds
100
 

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

Top