Hello,
I'm receiving a "TargetInvocationException" on the follow line of
code:
datagridServers.Invoke(new
UpdateDisplayCallback(this.UpdateServerDisplay), new object[]
{ server });
The method:
private void AddServer(MyServerClass server){
if(server != null)
arraylistServers.Add(server);
if(datagridServers.InvokeRequired)
datagridServers.Invoke(new
UpdateDisplayCallback(this.UpdateServerDisplay), new object[]
{ server });
else
this.UpdateServerDisplay(server);
}
The signature for UpdateServerDisplay is "private void
UpdateServerDisplay(MyServerClass server)"
The signature for UpdateDisplayCallback is "private delegate void
UpdateDisplayCallback(MyServerClass server)"
In one of many attempts to solve the problem, I placed a breakpoint on
the line ("if(server != null)") and the 'server' instance contains
everything that it should contain, meaning that it is an instance of
MyServerClass. But when I compile and run the application, I get a
TargetInvocationException on the line I pointed out above, and the
InnerException says "Unable to cast object of type
'App.ServerInfoClass' to type 'App.MyServerClass'. It seems that it's
trying to cast a ServerInfoClass into a MyServerClass, for no reason?
I can't seem to find where I'm going wrong here. Stepping through the
program, I'm passing the correct type to the method, but it seems to
have a problem with the "datagridServers.Invoke...") line. I've been
trying to figure this out for a few days, at no avail ofcourse. Any
hints would be greatly appreciated. Thanks