Callback mainthread in a class

C

Cheryl

Hi all. I am writing a component library that does not have an interface.
I would like to create a class that allows the worker thread to callback the
mainthread so that the Form interface does not have to invoke the main
thread before updating the control. Would this be possible? If yes, how
should I do so?
 
N

Nicholas Paldino [.NET/C# MVP]

Cheryl,

Are you saying that you want to have the event that you fired be on the
UI thread? If this is the case, instead of just firing the event normally,
you could cycle through the delegate chain. If the object that the
MethodInfo that the delegate is pointing to has an ISynchronizeInvoke
implementation, you can pass the delegate and the parameters to that.
 
C

Cheryl

Yes. What I mean is to fire the event on the UI thread. Sorry that I am
not too familar with the multithreading library in C#. Would you mind
giving me more illustration on "cycle through the delegate chain"?


Nicholas Paldino said:
Cheryl,

Are you saying that you want to have the event that you fired be on the
UI thread? If this is the case, instead of just firing the event
normally, you could cycle through the delegate chain. If the object that
the MethodInfo that the delegate is pointing to has an ISynchronizeInvoke
implementation, you can pass the delegate and the parameters to that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cheryl said:
Hi all. I am writing a component library that does not have an
interface. I would like to create a class that allows the worker thread
to callback the mainthread so that the Form interface does not have to
invoke the main thread before updating the control. Would this be
possible? If yes, how should I do so?
 
N

Nicholas Paldino [.NET/C# MVP]

Cheyrl,

When you make a call to fire an event, you usually do something like
this:

eventHandler(this, new EventArgs());

Or something similar, where eventHandler is the event declaration for
your event (of course, you check for null as well).

Instead of that, you would do this:

foreach (EventHandler handler in eventHandler.GetInvocationList())
{
// Check for the ISynchronizeInvoke implementation.
ISynchronizeInvoke si = handler.Target as ISynchronizeInvoke;

// If there is an implementation, call it on that thread.
if (si != null)
{
// Call.
si.Invoke(handler, new object[2]{this, new EventArgs()});
}
else
{
// Call normally.
handler(this, new EventArgs);
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cheryl said:
Yes. What I mean is to fire the event on the UI thread. Sorry that I am
not too familar with the multithreading library in C#. Would you mind
giving me more illustration on "cycle through the delegate chain"?


Nicholas Paldino said:
Cheryl,

Are you saying that you want to have the event that you fired be on
the UI thread? If this is the case, instead of just firing the event
normally, you could cycle through the delegate chain. If the object that
the MethodInfo that the delegate is pointing to has an ISynchronizeInvoke
implementation, you can pass the delegate and the parameters to that.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Cheryl said:
Hi all. I am writing a component library that does not have an
interface. I would like to create a class that allows the worker thread
to callback the mainthread so that the Form interface does not have to
invoke the main thread before updating the control. Would this be
possible? If yes, how should I do so?
 

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