Problem with deriving System.EventArgs

  • Thread starter Thread starter Michael Moreno
  • Start date Start date
M

Michael Moreno

Hello,

I am failing to understand why I get an invalid cast exception with
this very simple code.

I have derived the EventArgs class as follow:

public class StringArgs : System.EventArgs
{
public string Msg;
public StringArgs(string aMsg)
{
Msg = aMsg;
}
}


and I fire the event as shown below

private void EventLog(string Msg)
{
System.EventArgs e = new StringArgs(Msg);
object[] pList = { this, e };

BeginInvoke(new System.EventHandler(UpdateRTBLog), pList);
}


now when I receive the event in this method

private void UpdateRTBLog(object o, System.EventArgs e)
{
StringArgs se = (StringArgs) e;
RTBLog.Text = se.Msg;
}


I get during runtime an Invalid cast error:
"System.InvalidCastException: Specified cast is not valid." on
StringArgs se = (StringArgs) e;


Please would you be kind enough to tell me why this is happening and
how to fix this?

Many thanks for your help.

Michael
 
Michael said:
Hello,

I am failing to understand why I get an invalid cast exception with this
very simple code.

I have derived the EventArgs class as follow:

public class StringArgs : System.EventArgs
{
public string Msg;
public StringArgs(string aMsg)
{
Msg = aMsg;
}
}


and I fire the event as shown below

private void EventLog(string Msg)
{
System.EventArgs e = new StringArgs(Msg);
object[] pList = { this, e };

BeginInvoke(new System.EventHandler(UpdateRTBLog), pList);
}


now when I receive the event in this method

private void UpdateRTBLog(object o, System.EventArgs e)
{
StringArgs se = (StringArgs) e;
RTBLog.Text = se.Msg;
}


I get during runtime an Invalid cast error:
"System.InvalidCastException: Specified cast is not valid." on
StringArgs se = (StringArgs) e;


Please would you be kind enough to tell me why this is happening and how
to fix this?

Many thanks for your help.

Michael

Hi,

is your "System.EventArgs e" in your Event-Handler "UpdateRTBLog" not
the array of objects named pList? If so, I think you can't convert it to
StringArgs but not sure about the syntax of BeginInvoke. But why do
you use BeginInvoke?

Yours sincerely

Andy
 
is your "System.EventArgs e" in your Event-Handler "UpdateRTBLog" not the

Well, for some reasons it seems that the event I receive is empty which
does not make any sense at all.
array of objects named pList? If so, I think you can't convert it to
StringArgs but not sure about the syntax of BeginInvoke. But why do you use
BeginInvoke?

I have this sequence of event:

External Process fire an event to an object to which I have a reference
to in my process using a TCP channel.

In the "listener" process I get the event and fire an event to the Main
Form with a string in it. In the main form, if I try to display this
string into an EditBox, the process freezes. So I did a few test and it
looks like that I receive the event in a different thread and not in
the main thread which is hard to understand given that I have not
created any worker thread at all yet. So I assumed that "magically" I
was receiving this event in a worker thread and that was part of the
..Net Remoting behaviour (I did not find any documentation to confirm
this though). In the main form I got a kind of a confirmation for this
because I checked whether a call to BeginInvoke was required using
InvokeRequired and it returns TRUE !


Many thanks for your help.

Michael
 
Back
Top