Raise event in main thread
My form uses MessageQueue.BeginPeek to look for messages on the queue. When one is found, and .PeekCompleted gets raised, I want to raise an event. Due to other objects registered with the event, I need to raise the event on the main thread.
Is there a proper way to do this? Right now I call a method using Form.Invoke. The method simply raises the event. Just doesn't seem right to me.
Thanks,
Mike
Re: Raise event in main thread
I'm sorry I don't quite understand whta's going on... but if you want to add one single event to multiple object, you might want to consider event properties?
look here for an example http://vbforums.com/showthread.php?t=357956
(ie, in the add{} block you could add the event to multiple objects)
possibly not what you want:D but worth a look
Re: Raise event in main thread
Well, I'm following the observer pattern like this article describes - not the first part using interfaces, but under the section titled "Observer Pattern in the .NET Framework".
My issue is I'd like to raise the event in the main GUI thread, and not in the thread where MessageQueue.PeekCompleted happens.
Since I'm using asynchronous methods, .PeekCompleted is not executing in the main GUI thread. Since this main thread has created other forms, and those forms are registered with the event, if those forms decide to update their UI in response to the event, you run into problems.
As I said earlier, I'm getting around it by using Form.Invoke. The only thing the invoked method does is raise the event. Just seems not quite right.
Mike