|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
invoke in eventsHi,
I have an instance of a class in which a thread is running. When a special condition in that instance is met it raises an event to my main class (UI), but I can't directly interact with the user interface controls in my eventhandler without some sort of invoke. How should that be done - is it possible to locate the invoke in the instance so that the eventhandler actually belongs to the UI? Thanks, Ole "Ole" <o**@blabla.com> wrote in message The Invoke method should be invoked on a Control belonging to th UI. If news:erhCqmDMIHA.4740@TK2MSFTNGP02.phx.gbl... > I have an instance of a class in which a thread is running. When a special > condition in that instance is met it raises an event to my main class > (UI), but I can't directly interact with the user interface controls in my > eventhandler without some sort of invoke. How should that be done - is it > possible to locate the invoke in the instance so that the eventhandler > actually belongs to the UI? you pass to the instance of your class one such control (for instance the "this" from the main form in your application), then it can use such control to call Invoke and marshall execution into the main thread before raising the event. Something similar to the following: class MyClass { private Control ctl; public Myclass(Control ctl) { this.ctl=ctl; } public event EventHandler theEvent; private void EventRaiser() { if (theEvent!=null) theEvent(this, EventArgs.Empty); } void CodeThatRaisesEvent() { ctl.Invoke(new MethodInvoker(EventRaiser)); } } Hi,
You have to use Control.Invoke to make sure that the method is executed in the UI thread. In order to do this the background thread needs to have a reference to a control in the UI, any control will do. Take a look at the Control.Invoke help in MSDN or check the archives of this NG. this question is posted on a regular base. Show quote "Ole" <o**@blabla.com> wrote in message news:erhCqmDMIHA.4740@TK2MSFTNGP02.phx.gbl... > Hi, > > I have an instance of a class in which a thread is running. When a special > condition in that instance is met it raises an event to my main class > (UI), but I can't directly interact with the user interface controls in my > eventhandler without some sort of invoke. How should that be done - is it > possible to locate the invoke in the instance so that the eventhandler > actually belongs to the UI? > > Thanks, > Ole > Ole,
If you are concerned about not having to pass the instance of the form to the class, what you could do is get the invocation list of the delegate for the event you are firing. You would then cycle through the list, and check the MethodInfo for the each delegate in the list. If the type it is associated with implements ISynchronizeInvoke, then you can cast the target of the delegate to that and invoke the event on through the Invoke implementation (which is, in fact, what the Invoke method of Control is). -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com "Ole" <o**@blabla.com> wrote in message news:erhCqmDMIHA.4740@TK2MSFTNGP02.phx.gbl... > Hi, > > I have an instance of a class in which a thread is running. When a special > condition in that instance is met it raises an event to my main class > (UI), but I can't directly interact with the user interface controls in my > eventhandler without some sort of invoke. How should that be done - is it > possible to locate the invoke in the instance so that the eventhandler > actually belongs to the UI? > > Thanks, > Ole > Thanks for your help!
Ole Show quote "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in message news:%2329rNVHMIHA.1168@TK2MSFTNGP02.phx.gbl... > Ole, > > If you are concerned about not having to pass the instance of the form > to the class, what you could do is get the invocation list of the delegate > for the event you are firing. You would then cycle through the list, and > check the MethodInfo for the each delegate in the list. If the type it is > associated with implements ISynchronizeInvoke, then you can cast the > target of the delegate to that and invoke the event on through the Invoke > implementation (which is, in fact, what the Invoke method of Control is). > > -- > - Nicholas Paldino [.NET/C# MVP] > - mvp@spam.guard.caspershouse.com > > "Ole" <o**@blabla.com> wrote in message > news:erhCqmDMIHA.4740@TK2MSFTNGP02.phx.gbl... >> Hi, >> >> I have an instance of a class in which a thread is running. When a >> special condition in that instance is met it raises an event to my main >> class (UI), but I can't directly interact with the user interface >> controls in my eventhandler without some sort of invoke. How should that >> be done - is it possible to locate the invoke in the instance so that the >> eventhandler actually belongs to the UI? >> >> Thanks, >> Ole >> > > |
|||||||||||||||||||||||