Simple threading question about UI interaction

J

Jim H

I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event to
update the gui. Is it safe to do this?

jim
 
N

Nicholas Paldino [.NET/C# MVP]

Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate you
pass for the event has an implementation of ISynchronizeInvoke, it will call
that to invoke the delegate. So, if your method is on a class that derives
from Control, it will be invoked on the UI thread.

Hope this helps.
 
J

Jim H

Thanks for the info. That helper class is written in .NET 2.0 and I am
using VS2003 with .NET1.1.

Is there a way for me to tell, in my event handler, if I am in the ui thread
or a worker thread? If I am in my form code can I do this:
ISynchronizeInvode sync = this as ISynchronizeInvoke;
if(null == sync)
{
//I'm in a worker
this.Invoke(myDelegate, args);
}
else
{
//I'm in the UI thread
//myDelegate.DynamicInvoke(args)
}

The form is derived from Control which implements ISynchronizeInvkde right?

jim

Nicholas Paldino said:
Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate
you pass for the event has an implementation of ISynchronizeInvoke, it
will call that to invoke the delegate. So, if your method is on a class
that derives from Control, it will be invoked on the UI thread.

Hope this helps.


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

Jim H said:
I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event
to update the gui. Is it safe to do this?

jim
 
N

Nicholas Paldino [.NET/C# MVP]

Jim,

The code is easily adapted to work with .NET 1.1. You don't have to use
the generic event handlers, and can use any delegate you want.

The code that you supplied will work as well, but you would have to do
it in all of your event handlers, which makes the case for a class like
this.




Jim H said:
Thanks for the info. That helper class is written in .NET 2.0 and I am
using VS2003 with .NET1.1.

Is there a way for me to tell, in my event handler, if I am in the ui
thread or a worker thread? If I am in my form code can I do this:
ISynchronizeInvode sync = this as ISynchronizeInvoke;
if(null == sync)
{
//I'm in a worker
this.Invoke(myDelegate, args);
}
else
{
//I'm in the UI thread
//myDelegate.DynamicInvoke(args)
}

The form is derived from Control which implements ISynchronizeInvkde
right?

jim

Nicholas Paldino said:
Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate
you pass for the event has an implementation of ISynchronizeInvoke, it
will call that to invoke the delegate. So, if your method is on a class
that derives from Control, it will be invoked on the UI thread.

Hope this helps.


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

Jim H said:
I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event
to update the gui. Is it safe to do this?

jim
 
J

Jim H

I had the if backwards
It should read:
if(null != sync)

Nicholas Paldino said:
Jim,

That event code will be handled in the thread that is firing the event
(not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate
you pass for the event has an implementation of ISynchronizeInvoke, it
will call that to invoke the delegate. So, if your method is on a class
that derives from Control, it will be invoked on the UI thread.

Hope this helps.


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

Jim H said:
I have work being done in worker threads and I trigger an event, that gui
objects have subscribed to, when the work is complete. Will that event
handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event
to update the gui. Is it safe to do this?

jim
 
J

Jim H

Thanks again.

Nicholas Paldino said:
Jim,

The code is easily adapted to work with .NET 1.1. You don't have to
use the generic event handlers, and can use any delegate you want.

The code that you supplied will work as well, but you would have to do
it in all of your event handlers, which makes the case for a class like
this.




Jim H said:
Thanks for the info. That helper class is written in .NET 2.0 and I am
using VS2003 with .NET1.1.

Is there a way for me to tell, in my event handler, if I am in the ui
thread or a worker thread? If I am in my form code can I do this:
ISynchronizeInvode sync = this as ISynchronizeInvoke;
if(null == sync)
{
//I'm in a worker
this.Invoke(myDelegate, args);
}
else
{
//I'm in the UI thread
//myDelegate.DynamicInvoke(args)
}

The form is derived from Control which implements ISynchronizeInvkde
right?

jim

Nicholas Paldino said:
Jim,

That event code will be handled in the thread that is firing the
event (not the UI thread).

If you want to get around this, there is a utility class written by
Juval Lowy called EventsHelper. You can find it at:

http://www.idesign.net/idesign/uploads/EventsHelper.zip

This will allow you to fire an event. If the target of the delegate
you pass for the event has an implementation of ISynchronizeInvoke, it
will call that to invoke the delegate. So, if your method is on a class
that derives from Control, it will be invoked on the UI thread.

Hope this helps.


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

I have work being done in worker threads and I trigger an event, that
gui objects have subscribed to, when the work is complete. Will that
event handler code be executed in the ui thread or the worker thread?

What I am trying to do is process the data in a thread and use an event
to update the gui. Is it safe to do this?

jim
 
J

Jon Skeet [C# MVP]

Jim H said:
I had the if backwards
It should read:
if(null != sync)

For the sake of readability, I believe it should actually read:

if (sync != null)

The "constant first in if expressions" style from C doesn't do any good
in C# apart from the rare situation where you're checking a boolean
against true or false directly.
 
J

Jim H

I'm mainly a C/C++ programmer and it's a good habit to have. It prevents
those bugs where you mistakenly perform an assignment rather than a
comparison. Does the C# compiler prevent this some how?

jim
 
J

Jon Skeet [C# MVP]

Jim H said:
I'm mainly a C/C++ programmer and it's a good habit to have.

It's a good habit to have in C/C++. That doesn't mean it's a good habit
to have in C#. It reduces readability for no advantage.
It prevents
those bugs where you mistakenly perform an assignment rather than a
comparison. Does the C# compiler prevent this some how?

Yes. The type of an "if" expression has to be a boolean. The only time
you could get it wrong is:

if (x=true)
or
if (x=false)

- but in those cases I'd prefer to write it as

if (x)
or
if (!x)

in the first place.
 
J

Jeffrey Tan[MSFT]

Hi Jim,

Is your problem resolved? Normally, in a multithreading application, we can
use Control.InvokeRequired property to determine if the accessing to
another control need to be marshaled. If it returns true, we can use
Control.Invoke/BeginInvoke to marshal the calling/manipulating to the UI
controls properties/methods. This is the rule of doing multithreading in
Net Winform.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Ok, if you need further help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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