RaiseEvent in multithreading
I am using RaiseEvent in a thread and I pass a value to it and try to set a control's text to the value. Except I get a cross-thread error which is expected. I could invoke the control, but I don't want to have to do that.
My question is, is there a way to invoke/delegate the RaiseEvent itself so it will work on the ui thread without doing anything else?
Re: RaiseEvent in multithreading
Why don't you want to invoke the control?
Re: RaiseEvent in multithreading
I could, it's more of is there another way to do it?
Re: RaiseEvent in multithreading
It seems that you are raising the event on a thread other than the UI thread. If so invoke/delegate seems to be the answer.
http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
Re: RaiseEvent in multithreading
There are two ways to marshal a method call to another thread: the ISynchronizeInvoke interface and the SynchronizingContext class.
Usually we use the InvokeRequired property and Invoke method of a form or control. That's the ISynchronizeInvoke interface. If you want to do that then the object raising the event has to have a reference to a control that was created on the UI thread. Consider the SynchronizingObject property of the Timers.Timers and FileSystemWatcher classes. That's exactly what that is.
If you want to do this without a direct reference to a control then you can use the SynchronizingContext class. For an example, follow the Asynchronous link in my signature.