I was just doing some code, when i noticed 1 thing. Lets take this example.
We have 2 objects.
object1 have a worker thread, and an event.
object2 subscribes to the event, that object 1 exposes.
object1's worker thread calculates some stuff, and when its done, it raises event, and object2 catches it.
Heres the problem. The method thata object2 got, to handle this event, runs in the SAME thread, as the thread that it was raised from(This is an issue when it comes to win forms).
When i find this thing that i wonder about in OO things, i try and take the context, into some IRL example. I have tried over and over again, but this makes no sense to me.
As i couldnt imagine, that this a design error, it must have been thought of.
I have read about async event-based components. But in my project, i find this overkill.
Is there some workarround, to that, so that i wont have to invoke my UI all the time, when i catch events from my homemade socket, which raises event from a thread that i created

Event And Threading.
BMoore
It makes sense to me that an event raised in a thread has the event handler executed in the same thread.
If the event handler is really UI based I would try to raise the event in the UI thread. No need to take into consideration cross thread invoking when working with the UI.
VJM
"This is completely done in the library; and doesn't relinquish responsibility of the user's event handler to make sure Invoke is required or not"
Is it possible at all, to make the user not have to invoke he UI, at all then
papaboom
Basically, you use AsyncOperation.PostOperationCompleted() that calls a SendOrPostCallback delegate that wraps the method that raises the event.
The AsyncOperation object must be created (via AsyncOperationManager.CreateOperation method) on the thread where the event handler needs to be called (if you're using the Event-based Asynchronous Pattern, this would be done in the MethodNameAsync() method before the asynchronous operation is started either through an asynchronous delegate, using a Thread Pool work item, or manually creating a new thread).
This is completely done in the library; and doesn't relinquish responsibility of the user's event handler to make sure Invoke is required or not.
See How to: Implement a Component That Supports the Event-based Asynchronous Pattern for an example--which doesn't mean you have to use the Event-based Asynchronous pattern.
Andy Wahrenberger MSFT
Wow. That was a super fast answer.
Anyways, Andreas.
The thing is, that im doing a socket for a special purpose, that need to get data from the socket all the time. I do this by having a thread, that just have a while(true), while the connection is open.
From this thread, i raise the event, when theres data on the connection.
zhengjs
Object1 would be the library object, and object 2 would be the user object(This case a winforms)
Christopher P.
Vedek Andy
An event handler on a form really has no guarantee what thread will be used to invoke the event handler. As you know, only the UI thread should be the only thread accessing form controls and data: essentially the only thread that should be running any private form code.
Because an event handler can be called from any thread it should be making a call for Form.InvokeRequired before attempting to do anything with Form data/controls.
There are ways that the thread invoking the event handler can force the event handler to be called on the original thread; but there's still no guarantee (from the event handler's point of view) that it will be called with any particular thread, and should still check InvokeRequired.
Not to mention, why should the caller have to know that an event need be raised in a specific way
Eric Perlin - MSFT
It is because im doing this IRC library.
As i said in my other thread, i need to change the thread it raises the event into, as i dont want the user of the library to think about there being a thread in the library.
"There are ways that the thread invoking the event handler can force the event handler to be called on the original thread"
Could you try and show me an example of this
William Boatin