473,387 Members | 1,904 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,387 software developers and data experts.

How to Raise an Event on a Different Thread

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
Nov 20 '05 #1
4 9789
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 20 '05 #2
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" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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


Nov 20 '05 #3
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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



Nov 20 '05 #4
Hi Alex

Thanks again.
I would stick with BeginInvoke. As I see it you need to check how your 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" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:Ov**************@TK2MSFTNGP12.phx.gbl... 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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:u%****************@TK2MSFTNGP09.phx.gbl...
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" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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
>
>



Nov 20 '05 #5

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

Similar topics

1
by: Kulgan | last post by:
Hi, I have a dataset object that is binded to a datagrid in my main thread. Now, when I add a row to the dataset object in a another thread (since it may take awhile to complete), sometimes my...
1
by: VMI | last post by:
Im using a separate thread in my code to copy a huge file, but I get the "Controls created on one thread cannot be parented to a control on a different thread" when I want to update the Form...
1
by: Michael McCarthy | last post by:
I've written an event that tells me that I've connected properly to a remote telnet location. When I know that I'm connected I fire the event which is being listened for in a bit of code that then...
5
by: Waleed AlRashoud | last post by:
Hi, I hope u can help me I asked this question before but I didn't explain it very well. Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do some work on 'O', OK? I...
0
by: Mike | last post by:
I have a asp.net (vb.net) web app that calls a VB.NET class. This class has a raise Event in it. - raised event getStuff I can't call the raised Event getStuff. Can i not call or execute a raised...
3
by: Giovanni Bassi | last post by:
Hello Group, I am running an operation in a different thread. There are resources that are released when the thread is done running. This is done at the end of the execution as it raises an...
3
by: Michael Maes | last post by:
Hello, I know forms aren't thread-safe, but I'm a bit stuck here.... I have an MDI-Application which after 'Login' loads the Data from an SQL Server into DataSets. Those DataSets are Parented...
0
by: Bobby Owens | last post by:
Hi, I'm current having a problem with the treeview control and multi threading. The treeview is on a form. A request is then sent to a server using IP for the data. The data arrives in an event...
2
by: tony | last post by:
Hello !! I have a question about event. This small program below is taken from the internet and I wonder if there are any advantage to use this Onxxx in this case it's OnEventSeven instead of...
0
by: Silver Oak | last post by:
I have a DataGrid in which one of the columns is TemplateColumn that was created dynamically using iTemplate. I would like to have multi-row editing capability on the DataGrid. I'm trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.