Bring data from an asynchronous operation to the main thread

This is the “problem” I have with the data that I get from the library, which has some Async operations when sending and receiving data. While I am receiving data and receiving data in my mobile Windows Form or Desktop, I need to deal with cross-threading. I do this when validating InvokeRequired and perform the action if true ... etc. But I really would like it to disappear and correctly execute my data, so that I can attach them to manipulation, etc., Without addressing this cross-topic, the problem.

Question: how can I manipulate data in my library and then raise the event to the client? Where they could, no matter what they want, without processing end-to-end streams.

This should be true for the Compact Framework, so clients are mobile clients. And so the solution found using ISynchronizeInvoke is not valid.

Any help to make this a pleasantly appreciated! Thanks in advance.

0
asynchronous thread-safety compact-framework
Dec 04 '09 at 13:06
source share
1 answer

You can create a COntrol in the constructor of your Library, then call with it and raise an event after the call. Then the consumer will receive the event in the context of the thread that created your library class. If you make it a Component, most likely it will be created in the user interface thread, and therefore your events will increase in the user interface thread.

EDIT 1

As an example:

private Control m_invoker = new Control(); public event EventHandler MyEvent; private void RaiseMyEvent(object o, EventArgs args) { EventHandler handler = MyEvent; if (handler == null) return; if (m_invoker.InvokeRequired) { m_invoker.BeginInvoke(new EventHandler(RaiseMyEvent), new object[] { o, args }); return; } handler(o, args); } 

Thus, your code will call RaiseMyEvent, which in turn transfers the call to the stream, which creates the current object, and then raises the actual event.

+1
Dec 04 '09 at 14:19
source share



All Articles