invoke in events

O

Ole

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
 
A

Alberto Poblacion

Ole said:
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?

The Invoke method should be invoked on a Control belonging to th UI. If
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));
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 
O

Ole

Thanks for your help!
Ole

Nicholas Paldino said:
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]
- (e-mail address removed)

Ole said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top