Connecting Tech Pros Worldwide Forums | Help | Site Map

How to Raise an Event on a Different Thread

Charles Law
Guest
 
Posts: n/a
#1: Nov 20 '05
Suppose a worker thread needs to signal to the main thread that an event has
occurred. Ordinarily, any event raised by the worker thread will be on its
own thread.

How can the worker thread raise an event on the main thread instead?

TIA

Charles



AlexS
Guest
 
Posts: n/a
#2: Nov 20 '05

re: How to Raise an Event on a Different Thread


Charles,
Take a look at examples at
http://msdn.microsoft.com/msdnmag/is...Multithreading and in MSDN
also, if you haven't seen them yet

"Charles Law" <blank@nowhere.com> wrote in message
news:%23JCrt6WTEHA.3844@TK2MSFTNGP11.phx.gbl...[color=blue]
> Suppose a worker thread needs to signal to the main thread that an event[/color]
has[color=blue]
> occurred. Ordinarily, any event raised by the worker thread will be on its
> own thread.
>
> How can the worker thread raise an event on the main thread instead?
>
> TIA
>
> Charles
>
>[/color]


Charles Law
Guest
 
Posts: n/a
#3: Nov 20 '05

re: How to Raise an Event on a Different Thread


Hi Alex

Thanks for the reference. I have been trying with Invoke, and more recently
BeginInvoke, but they both have their problems.

Invoke does not return until the UI thread is free, which causes deadlock
when the UI thread is waiting for the worker thread to do something.

BeginInvoke returns immediately, but the UI is not updated until the thread
is free, which means that updates can occur out of sequence. In particular,
I send data out of the serial port and produce a trace in a window
on-screen. When replies are sent, I add them to the trace. The trace of the
received data is invoked from an event raised by the worker/comms thread.
When there are a lot of exchanges of outgoing and incoming data, the
outgoing data is added to the trace first, and then the incoming data, and
thus the correct sequence is lost.

I was looking for a way for the comms class to raise its 'DataReceived'
event on the UI thread, so that everything would be synchronised. Any ideas?

Charles


"AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
news:u%233d5iXTEHA.2692@TK2MSFTNGP09.phx.gbl...[color=blue]
> Charles,
> Take a look at examples at
> http://msdn.microsoft.com/msdnmag/is...Multithreading and in MSDN
> also, if you haven't seen them yet
>
> "Charles Law" <blank@nowhere.com> wrote in message
> news:%23JCrt6WTEHA.3844@TK2MSFTNGP11.phx.gbl...[color=green]
> > Suppose a worker thread needs to signal to the main thread that an event[/color]
> has[color=green]
> > occurred. Ordinarily, any event raised by the worker thread will be on[/color][/color]
its[color=blue][color=green]
> > own thread.
> >
> > How can the worker thread raise an event on the main thread instead?
> >
> > TIA
> >
> > Charles
> >
> >[/color]
>
>[/color]


AlexS
Guest
 
Posts: n/a
#4: Nov 20 '05

re: How to Raise an Event on a Different Thread


Hi, Charles

I would stick with BeginInvoke. As I see it you need to check how your trace
is implemented. If you really need to sequence it, you can use some
collection object (Queue, Stack, Hashtable - whatever), to post events in
correct order and then to extract them for display in correct order.
DataReceived event won't solve the problem by itself.

If I get your situation, you can keep the sequence of sent data, but answers
might be not sequenced properly? Possibly you need some contraption to link
your send to your receive. For example, you can save send somewhere or pass
it to receiving thread. Then when you receive answer you can show both using
BeginInvoke. Or you can link the answer to original send and show the link
in your trace. Depends.

It's not very clear for me what is "everything is synchronized". By itself
raising event between the threads is same as raising event on same thread.
Except of course threads synchronization issues. Which are different from
yours.

HTH
Alex

"Charles Law" <blank@nowhere.com> wrote in message
news:%23zJ$CgYTEHA.2416@TK2MSFTNGP09.phx.gbl...[color=blue]
> Hi Alex
>
> Thanks for the reference. I have been trying with Invoke, and more[/color]
recently[color=blue]
> BeginInvoke, but they both have their problems.
>
> Invoke does not return until the UI thread is free, which causes deadlock
> when the UI thread is waiting for the worker thread to do something.
>
> BeginInvoke returns immediately, but the UI is not updated until the[/color]
thread[color=blue]
> is free, which means that updates can occur out of sequence. In[/color]
particular,[color=blue]
> I send data out of the serial port and produce a trace in a window
> on-screen. When replies are sent, I add them to the trace. The trace of[/color]
the[color=blue]
> received data is invoked from an event raised by the worker/comms thread.
> When there are a lot of exchanges of outgoing and incoming data, the
> outgoing data is added to the trace first, and then the incoming data, and
> thus the correct sequence is lost.
>
> I was looking for a way for the comms class to raise its 'DataReceived'
> event on the UI thread, so that everything would be synchronised. Any[/color]
ideas?[color=blue]
>
> Charles
>
>
> "AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
> news:u%233d5iXTEHA.2692@TK2MSFTNGP09.phx.gbl...[color=green]
> > Charles,
> > Take a look at examples at
> > http://msdn.microsoft.com/msdnmag/is...Multithreading and in[/color][/color]
MSDN[color=blue][color=green]
> > also, if you haven't seen them yet
> >
> > "Charles Law" <blank@nowhere.com> wrote in message
> > news:%23JCrt6WTEHA.3844@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Suppose a worker thread needs to signal to the main thread that an[/color][/color][/color]
event[color=blue][color=green]
> > has[color=darkred]
> > > occurred. Ordinarily, any event raised by the worker thread will be on[/color][/color]
> its[color=green][color=darkred]
> > > own thread.
> > >
> > > How can the worker thread raise an event on the main thread instead?
> > >
> > > TIA
> > >
> > > Charles
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Charles Law
Guest
 
Posts: n/a
#5: Nov 20 '05

re: How to Raise an Event on a Different Thread


Hi Alex

Thanks again.
[color=blue]
> I would stick with BeginInvoke. As I see it you need to check how your[/color]
trace

That was the unhappy conclusion I was coming to. I look forward to the time
when forms and controls are thread-safe ;-)

Charles



"AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
news:Ov2AiVbTEHA.3512@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hi, Charles
>
> I would stick with BeginInvoke. As I see it you need to check how your[/color]
trace[color=blue]
> is implemented. If you really need to sequence it, you can use some
> collection object (Queue, Stack, Hashtable - whatever), to post events in
> correct order and then to extract them for display in correct order.
> DataReceived event won't solve the problem by itself.
>
> If I get your situation, you can keep the sequence of sent data, but[/color]
answers[color=blue]
> might be not sequenced properly? Possibly you need some contraption to[/color]
link[color=blue]
> your send to your receive. For example, you can save send somewhere or[/color]
pass[color=blue]
> it to receiving thread. Then when you receive answer you can show both[/color]
using[color=blue]
> BeginInvoke. Or you can link the answer to original send and show the link
> in your trace. Depends.
>
> It's not very clear for me what is "everything is synchronized". By itself
> raising event between the threads is same as raising event on same thread.
> Except of course threads synchronization issues. Which are different from
> yours.
>
> HTH
> Alex
>
> "Charles Law" <blank@nowhere.com> wrote in message
> news:%23zJ$CgYTEHA.2416@TK2MSFTNGP09.phx.gbl...[color=green]
> > Hi Alex
> >
> > Thanks for the reference. I have been trying with Invoke, and more[/color]
> recently[color=green]
> > BeginInvoke, but they both have their problems.
> >
> > Invoke does not return until the UI thread is free, which causes[/color][/color]
deadlock[color=blue][color=green]
> > when the UI thread is waiting for the worker thread to do something.
> >
> > BeginInvoke returns immediately, but the UI is not updated until the[/color]
> thread[color=green]
> > is free, which means that updates can occur out of sequence. In[/color]
> particular,[color=green]
> > I send data out of the serial port and produce a trace in a window
> > on-screen. When replies are sent, I add them to the trace. The trace of[/color]
> the[color=green]
> > received data is invoked from an event raised by the worker/comms[/color][/color]
thread.[color=blue][color=green]
> > When there are a lot of exchanges of outgoing and incoming data, the
> > outgoing data is added to the trace first, and then the incoming data,[/color][/color]
and[color=blue][color=green]
> > thus the correct sequence is lost.
> >
> > I was looking for a way for the comms class to raise its 'DataReceived'
> > event on the UI thread, so that everything would be synchronised. Any[/color]
> ideas?[color=green]
> >
> > Charles
> >
> >
> > "AlexS" <salexru2000NO@SPAMsympaticoPLEASE.ca> wrote in message
> > news:u%233d5iXTEHA.2692@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > > Charles,
> > > Take a look at examples at
> > > http://msdn.microsoft.com/msdnmag/is...Multithreading and in[/color][/color]
> MSDN[color=green][color=darkred]
> > > also, if you haven't seen them yet
> > >
> > > "Charles Law" <blank@nowhere.com> wrote in message
> > > news:%23JCrt6WTEHA.3844@TK2MSFTNGP11.phx.gbl...
> > > > Suppose a worker thread needs to signal to the main thread that an[/color][/color]
> event[color=green][color=darkred]
> > > has
> > > > occurred. Ordinarily, any event raised by the worker thread will be[/color][/color][/color]
on[color=blue][color=green]
> > its[color=darkred]
> > > > own thread.
> > > >
> > > > How can the worker thread raise an event on the main thread instead?
> > > >
> > > > TIA
> > > >
> > > > Charles
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]


Closed Thread