Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET: How to have background thread signal main thread data is available?
    text
    copied!<p>What is the proper technique to have <strong>ThreadA</strong> signal <strong>ThreadB</strong> of some event, without having <strong>ThreadB</strong> sit blocked waiting for an event to happen?</p> <p>i have a background thread that will be filling a shared List&lt;T&gt;. i'm trying to find a way to asynchronously signal the "main" thread that there is data available to be picked up.</p> <hr> <p>i considered setting an event with an EventWaitHandle object, but i can't have my main thread sitting at an Event.WaitOne().</p> <hr> <p>i considered having a delegate callback, but a) i don't want the main thread doing work in the delegate: the thread needs to get back to work adding more stuff - i don't want it waiting while the delegate executes, and b) the delegate needs to be marshalled onto the main thread, but i'm not running a UI, i have no Control to .Invoke the delegate against.</p> <hr> <p>i considered have a delegate callback that simply starts a zero interval System.Windows.Forms.Timer (with thread access to the timer synchronized). This way the thread only needs to be stuck as it calls</p> <p><code>Timer.Enabled = true;</code></p> <p>but that seems like a hack.</p> <p>In the olden days my object would have created a hidden window and had the thread post messages to that hidden windows' HWND. i considered creating a hidden control, but i gather that you cannot .Invoke on a control with no handle created. Plus, i have no UI: my object could have been created on a web-server, service, or console, i don't want there to be a graphical control appearing - nor do i want to compile a dependency on System.Windows.Forms.</p> <hr> <p>i considered having my object expose an ISynchronizeInvoke interface, but then i would need to implement .Invoke(), and that's my problem.</p> <hr> <p>What is the proper technique to have thread A signal thread B of some event, without having thread B sit blocked waiting for an event to happen?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload