Erratic error System.Reflection.TargetException: Object does not match target type.

J

Joshua Ellul

Hi There,

I have created a .NET class which I am using in VB.

The class basically performs a single operation in a loop and raises an
event (which is caught by the VB application).

Everything works fine, but then hours later the following error arrises (it
is very haphazard):

System.Reflection.TargetException: Object does not match target type.
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags
invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32
culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr,
Binder binder, Object target, Object[] args, ParameterModifier[] modifiers,
CultureInfo culture, String[] namedParameters)
at System.RuntimeType.ForwardCallToInvokeMember(String memberName,
BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at XSOneWire.OneWireNetworkInterfaceEvents.DS1990ADiscovered(DS2409
sender, String DS1990AID, Byte MainAux)
at XSOneWire.OneWireNetworkInterface.ds2409_DS1990ADiscovered(DS2409
sender, String DS1990AID, Byte MainAux)
at XSOneWire.DS2409.Alarm()
at XSOneWire.OneWireNetworkInterface.CheckNextDS2409()
at XSOneWire.OneWireNetworkInterface.IBLoop()

This is the code line crashing:
if (DS1990ADiscovered != null)

{

DS1990ADiscovered(sender, DS1990AID, MainAux);

}


The event:
public event DeviceDiscoveredDelegate DeviceDiscovered;


with the delegate:
public delegate void DS1990ADiscoveredDelegate(DS2409 sender, string
DS1990AID, byte MainAux);


This is the event in vb:
Private Sub owni_DS1990ADiscovered(ByVal sender As DS2409, ByVal DS1990AID
As String, ByVal MainAux As Byte)
debug.print DS1990AID
End Sub



Now the place where it is crashing works 1,000,000 times over however all of
a sudden it crashes. Exactly the same code, exactly the same values. All I
can think of is some marshalling problem.

Is there some sort of bug in .NET that will eventually make this happen?

Notes:
- A main class is given COM descriptions for use in the vb application,
however other classes are not (these other classes are passed as objects
also)
 
Joined
Sep 28, 2006
Messages
1
Reaction score
0
System.Reflection.TargetException

Hello Joshua,

I had the same kind of problem, when hosting a .NET Windows Forms Control in the Internet Explorer.
After several tests, I concluded that the exception occurs only when we raise the event in the context of a thread different that the thread that created the instance of the .NET Control (in my case, the IE UI thread, that, in the COM's perspective, is a STA thread).
When we raise an event from another thread, the call goes through a proxy, created by the .NET COM Interop. It seems that the cause of the problemt is the code of that proxy, because when I do a direct call to the COM object, in the IE UI thread, everyhing works fine.
(I am sure that the event handler is always executed by the IE UI thread, beacuse when I got the things to work raising the event from another thread, I saw that the event handler is executed by the IE UI thread. So, there is a COM proxy in the middle!)
I solved the problem using the functionality of the methods Invoke/BeginInvoke of the type Windows.Forms.Control. With this methods, we can execute a method in the context of the thread that creates the control (in my case the IE UI thread).
I don't know if your problem is exactly the same, but when we use a .NET Control by a VB6 applicattion, we have the same infrastructure in the middle.

The definition of my Windows Forms Controls has the following structure:

// the delegate matching the event signature
internal delegate void CB(string m);

// the definition of the event's interface
[Guid(...)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IEvents {
[DispId(1)]
void CallbackIntoIE(string m);
}

// the Windows Forms Control
[ComSourceInterfaces("IEvents")]
public class WFC: System.Windows.Forms.UserControl {

// the constructor
public WFC() {
... constructor stuff
// ensure that the OS window is created by the thread that instantiates
// the control (IE UI thread, in my case)
IntrPtr dummy = this.Handle;
}

// the event
internal event CB CallbackIntoIE;

// *** FIRE THE EVENT ***

// the delegate that matches the method Fire_CallbackIntoIE
private delegate void ICB(string s);

public void Fire_CallbackIntoIE(string m) {
if (this.InvokeRequired) {
// the method was called by another thread
// execute the same method in the context of Control's owners thread
this.BeginInvoke(new ICB(Fire_CallbackIntoIE), new object[] {m});

} else {
// executing in Control's owner thread
if (_cb != null)
// fire the event in the context of Control's owner thread
CallbackIntoIE(m);
// else, no listeners
}
}

... other methods
}

I hope this helps.

Best regards,

Carlos Martins
 

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