473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Simple threading question about UI interaction

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
Nov 17 '05 #1
11 2899
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]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ei**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
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


Nov 17 '05 #3
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" <ji**@nospam.nospam> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
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



Nov 17 '05 #4
I had the if backwards
It should read:
if(null != sync)

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

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
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


Nov 17 '05 #5
Thanks again.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uB**************@TK2MSFTNGP15.phx.gbl...
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" <ji**@nospam.nospam> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ei**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Jim H" <ji**@nospam.nospam> wrote in message
news:uR**************@TK2MSFTNGP15.phx.gbl...
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



Nov 17 '05 #6
Jim H <ji**@nospam.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jim H <ji**@nospam.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #8
Jim H <ji**@nospam.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
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.

Nov 17 '05 #10
Yes, my question was answered.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:Iz**************@TK2MSFTNGXA01.phx.gbl...
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.

Nov 17 '05 #11
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.

Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
11
by: Chris Fink | last post by:
If I create a new thread in a web app and the user closes the browser before this long running thread is finisihed, does the thread die? If so, is there a way to prevent it from dying and keep the...
0
by: Benoit Martin | last post by:
Hi, I've had my application going back to desktop randomly when executing ShowDialog commands. I posted to this list and was asked to post code but unfortunately I cannot reproduce this problem...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
18
by: Bob Cummings | last post by:
Not sure if this is the correct place or not. Anyhow in school we were taught that when trying to calculate the efficiency of an algorithm to focus on something called FLOPs or Floating Point...
41
by: km | last post by:
Hi all, Is there any PEP to introduce true threading features into python's next version as in java? i mean without having GIL. when compared to other languages, python is fun to code but i feel...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
7
by: kimiraikkonen | last post by:
Hello experts, I've been already working on a project and also asked and i've managed to create a basic Gmail mail sender, but i want to add a progressbar that shows "sending is in progress" but...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.