Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an instance where I would like to be able to handle events from a background thread. Specifically, to be able to handle events generated by external hardware (via serial port, TCP/IP connection, etc.) to pass their data to the BackgroundWorker.

C#
void Worker_DoWork(object sender, DoWorkEventArgs e)
{
    simpleButton3.Click += new EventHandler(simpleButton3_Click);

    while (!Worker.CancellationPending)
    {
        // Wait for event to be triggered
    }

    simpleButton3.Click -= new EventHandler(simpleButton3_Click);
}

void simpleButton3_Click(object sender, EventArgs e)
{
    // Do something...

    Thread.Sleep(5000);

    // Do something else...
}


However, what I'm finding in this example is that the Sleep command is putting the main (GUI) thread to sleep instead of the BackgroundWorker.

Is it possible for a BackgroundWorker thread to process events?

Thank you!
Posted

Well, for serial ports and TCP/IP connection, it is not hard to read/write data from your background workers an do most of the processing here and call ReportProgress if you want to do something on the UI with the result.

For effeciency, you have to not ReportProgress too often though. If progress is reported in percentage, often reporting the progress only when the percentage actually changes, works relatively well and without too much overhead.

For the opening and closing of the serial port or TCP/IP, you can decide if you want to do it in the main (UI) thread or the background worker. In my application, I have do it in the main thread.

Also, if you do both sending and receiving, depending on your application, you might use distinct background workers for sending and receiving data. If my application I uses a pair of them for serial port and another pair for TCP/IP.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 29-Aug-11 20:38pm    
Yes, quite reasonable directions, my 5.
--SA
All wrong. Why do you think you add a handler to a Click event in your background thread? It makes no sense. Do you think if you managed to add a handler to some event's invocation list, it will make an event itself calling this method in the same thread? Why? No way!

Events, delegate instances and any methods generally have nothing to do with the threads; any method can be called from any thread, but effect of it could be different, of course.

In particular, this is what happens when you call any UI-related methods or properties from non-UI thread. You simply cannot do it. Instead, you need to use the method Invoke or BeginInvoke of System.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
Philippe Mori 29-Aug-11 20:09pm    
Effectivelly it does not make much sense to hook up UI events from the background thread... As explained, hooking up events is essentially independant of the thread that make the call.

See also my solution for information related more to the OP text than to its code sample...
Sergey Alexandrovich Kryukov 29-Aug-11 20:39pm    
Exactly. Saw your post, agree with you, up-voted...
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900