473,781 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9813
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******** ********@TK2MSF TNGP11.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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:u%******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP11.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******** ********@TK2MSF TNGP09.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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:u%******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP11.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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:Ov******** ******@TK2MSFTN GP12.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******** ********@TK2MSF TNGP09.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***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:u%******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP11.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
1286
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 application will crash. I suspect this is because the ListChanged event is raised on a different thread to the thread that my dataset object was bound to the datagrid. My question is, how do I get around this problem? I would think this would be a...
1
14263
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 controls after makeBackup is complete. I know what the problem is (I'm trying to update controls from a thread that did not create them), but how can I fix it? This my code: private delegate void BackupCompletedEventHandler(); private event...
1
1577
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 goes on and does some work based on this event. My problem is that once I fire that event, the thread that fired it waits until the thread that received it finishes. I fixed the problem by making the bit of code that is listening for the...
5
3223
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 want thread 'B' to fire event of 'O' BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!
0
954
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 event or an event from a asp.net application? this class works because i use it from a vb.net winform application. thx
3
1964
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 event, and then the operation handling this event calls threaded object's dispose method. The problem is: If an exception is thrown the event is never raised, the operation never executes dispose and my resources get stuck on the memory until the app...
3
1234
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 on a "DataForm" which is loaded like this: If IsNothing(Data) Then Data = New frmData
0
1322
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 on a different thread. I have used delegates to allow me to add to the treeview and it all works fine. I cant however seem to set the "SelectedNode" property or the treeview. I have tried directly and also using a delegate. I all cases, the UI...
2
1941
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 doing in the following way. 1. In method GenNumbers() you remove this statement "OnEventSeven(new DivBySevenEventArgs(i));" with these
0
2009
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 follow the example in MSDN http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchTopQuestionsAboutASPNETDataGridServerControl.asp Section "Editing Multiple Rows at once". I would like to follow the second solution...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10308
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.