DaTurk said:
I know in a WinForm you can check if an Invoke is inquired and invoke
a method on the main thread. But can you do this in a class? Can I
call Invoke(someMethod) in my asynch event handler?
Can you elaborate on your question? You can call Control.Invoke() on
any method you like, whether that method exists in the Control-based
class or not. The delegate retains a reference to the instance used for
instance methods, so the context isn't important. It still gets called
regardless.
If you're asking whether there's a way to call some sort of "invoke"
method like Control.Invoke() but from a class that's not derived from a
Control, the short answer is "not easily".
The long answer is that to do something like this you need some sort of
context that defines where the delegate should run. The most obvious
way to do this is to have a queue of a class that includes a delegate
and parameters to pass to the delegate, and then consume that class in a
single thread. Other threads can add delegates and their parameters to
the queue, and then the consuming thread would dequeue them and execute
them.
That would ensure that the delegates get executed on the consuming thread.
When I first started doing .NET stuff, I played around a little with
some .NET class that, if I recall, help with this sort of thing outside
of the context of a Control-derived class. They are the AsyncOperation
and SynchronizationContext classes. It's been awhile and I don't recall
the specifics as to how I used them. But I do seem to recall that they
addressed an issue similar to what you're asking about.
Pete