C # Synchronizing two objects through events

I have 2 objects. Foo and Bar in two different threads. Now I want to raise an event in Foo, but in the Bar stream.

and how can I use SynchronizationContext.Current for this?

+3
source share
3 answers

Neither "Foo" nor "Bar" really have any threads ... you will need an external message-message mechanism to transfer messages between threads, for example, in winforms (Control.Invoke) and WPF (Dispatcher). Alternatively, something like a (synchronized) lineup of producers / consumers will be enough if you don't mind if one of the threads is dedicated to waiting for (passively) messages.

If you have not written your own synchronous context, we cannot be told what will happen Synchronization.Current; in many cases it is null.

Can you add more context to the problem?

+4
source

Silverlight, ? , SynchronizationContext.Send().

(, , Bar, , Send() d. , Foo Send() d).

0

, , - InvokeRequired() BeginInvoke(), , , , , , .

... Before Foo starts in Bar, create an instance of some UI element, say a label ... Then When an event occurs in Foo, call BeginInvoke () in this user interface element in the event handler. This ensures that the code that is executed is launched on Bar ...

But why are you doing this?

0
source

Source: https://habr.com/ru/post/1705825/


All Articles