> When I use this object in my form (main UI thread), I get an error which
> > tells me to use Textbox.Invoke (which I understand and I know how to solve
> > the error from my form). No problem.
As you can read I know how to solve this problem and that's your answer.
I'm sorry if I have also problems to expose this problem. lol
In fact, don't mind about the main UI thread.
Let's focus on my object: this object when it is instanciated runs inside a
thread. I start a child thread with an object method. An event is raised
inside the child thread, but I want this event to be raised inside its parent
thread (object thread).
internal class TestClass
{
public event EventHandler MyEvent;
public void MyMethod()
{
Thread.CurrentThread.Name = "Main";
Thread child = new Thread(new ThreadStart(ChildMethod));
child.Start();
}
void RaiseEvent( object sender, EventArgs e )
{
Console.WriteLine("On thread: " + Thread.CurrentThread.Name);
if ( MyEvent != null )
MyEvent( sender, e );
}
private void ChildMethod()
{
// some job
RaiseEvent(this, null); //on child thread. I want to raise this
event on parent Main thread
// some job
}
}
Hope you see what I need.
> In your EventHandler which is catching the Event Raised from the Child
> Thread, call another method which will call itself if not currently
[quoted text clipped - 46 lines]
> >
> > Thanks in advance for your help.
Dan Manges - 03 Jul 2006 18:19 GMT
[snip]
> private void ChildMethod()
> {
[quoted text clipped - 6 lines]
>
> Hope you see what I need.
[snip]
Why can't you use an Invoke method from ChildMethod() to call RaiseEvent() ?
Dan Manges
Stoitcho Goutsev (100) - 04 Jul 2006 21:30 GMT
Noulouk,
I don't think you can pull this off without using Control.Invoke.
BackgroundWorker from .NET 2.0. does that and if you have enough time you
can dig into its code to find out how it does it.
My suggestion is to do the same that System.Timers.Timer component does.
This class has a property called SynchronizingObject of type
ISynchronizeInvoke. If an object implements this interface it has to make
sure that the call is marshaled to appropriate thread if necessary. At the
moment only the Control class implement this interface.
When your object needs to fire the event it checks whether the property is
set if it is, it uses objects Invoke method otherwise it fires the event in
the usual way.

Signature
HTH
Stoitcho Goutsev (100)
> > When I use this object in my form (main UI thread), I get an error which
>> > tells me to use Textbox.Invoke (which I understand and I know how to
[quoted text clipped - 98 lines]
>> >
>> > Thanks in advance for your help.