Event and Invoke invalid cast

Z

Zürcher See

I'm using the following structure for my programm, and I never had problems.
Now by the invoke of the MyObject_End eventhandler I get the following
error:

System.Windows.FormsSpecified cast is not valid.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate
method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
....

What's wrong with it now? Cast of what?

Thank's

Program structure:

class Main
{
protected EventHandler myObjectEnd;

public Main()
{
this.myObjectEnd=new EvenrHandler(this.MyObject_end);
...
}

protected void StartNewMyObject()
{
...
MyObject myObject=new MyObject();
myObject.End+=this.myObjectEnd;
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(myObject.Start));
...
}

protected void MyObjectEnd(MyObject myObject)
{
//Handle the MyObject End event
...
}

private void MyObject_End(object sender, EventArgs e)
{
if (this.InvokeRequired) this.Invoke(this.myObjectEnd,new
object[]{sender,e});
else this.MyObjectEnd((MyObject)sender);
}
}

class MyObject
{
public event EventHandler End;

protected void OnEnd() {if (this.End!=null) this.End(this,new
EventArgs());}

public void Start()
{
...
this.OnEnd();
}
}
 
Z

Zürcher See

No, is not the windows forms thread that call this function, at least, not
the first time, when I get the error.
The first time is a "working" thread of the threadpool, because is not the
forms thread (InvokeRequired==true)
the same method is invoked. But is not the first time that I use this
"structure", and I never had problems.

Ollie said:
are you call it from the same thread, windows forms are STA

HTH

Ollie Riches

Zürcher See said:
I'm using the following structure for my programm, and I never had problems.
Now by the invoke of the MyObject_End eventhandler I get the following
error:

System.Windows.FormsSpecified cast is not valid.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate
method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
....

What's wrong with it now? Cast of what?

Thank's

Program structure:

class Main
{
protected EventHandler myObjectEnd;

public Main()
{
this.myObjectEnd=new EvenrHandler(this.MyObject_end);
...
}

protected void StartNewMyObject()
{
...
MyObject myObject=new MyObject();
myObject.End+=this.myObjectEnd;
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(myObject.Start));
...
}

protected void MyObjectEnd(MyObject myObject)
{
//Handle the MyObject End event
...
}

private void MyObject_End(object sender, EventArgs e)
{
if (this.InvokeRequired) this.Invoke(this.myObjectEnd,new
object[]{sender,e});
else this.MyObjectEnd((MyObject)sender);
}
}

class MyObject
{
public event EventHandler End;

protected void OnEnd() {if (this.End!=null) this.End(this,new
EventArgs());}

public void Start()
{
...
this.OnEnd();
}
}
 
O

Ollie

If you want to update a UI component of windows forms application from
another thread(worker thread) yuo will have to marshal the call back onto
the UI thread the easiest way to do this is to use the 'Invoke' method on a
windows form control.

HTH

Ollie Riches
Zürcher See said:
No, is not the windows forms thread that call this function, at least, not
the first time, when I get the error.
The first time is a "working" thread of the threadpool, because is not the
forms thread (InvokeRequired==true)
the same method is invoked. But is not the first time that I use this
"structure", and I never had problems.

Ollie said:
are you call it from the same thread, windows forms are STA

HTH

Ollie Riches

Zürcher See said:
I'm using the following structure for my programm, and I never had problems.
Now by the invoke of the MyObject_End eventhandler I get the following
error:

System.Windows.FormsSpecified cast is not valid.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate
method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
....

What's wrong with it now? Cast of what?

Thank's

Program structure:

class Main
{
protected EventHandler myObjectEnd;

public Main()
{
this.myObjectEnd=new EvenrHandler(this.MyObject_end);
...
}

protected void StartNewMyObject()
{
...
MyObject myObject=new MyObject();
myObject.End+=this.myObjectEnd;
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(myObject.Start));
...
}

protected void MyObjectEnd(MyObject myObject)
{
//Handle the MyObject End event
...
}

private void MyObject_End(object sender, EventArgs e)
{
if (this.InvokeRequired) this.Invoke(this.myObjectEnd,new
object[]{sender,e});
else this.MyObjectEnd((MyObject)sender);
}
}

class MyObject
{
public event EventHandler End;

protected void OnEnd() {if (this.End!=null) this.End(this,new
EventArgs());}

public void Start()
{
...
this.OnEnd();
}
}
 

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