c#.Net CF Form.Invoke引发ArgumentException
我从下面的代码中收到ArgumentException,我正在努力理解它堆栈跟踪中的最后一个条目是c#.Net CF Form.Invoke引发ArgumentException,c#,events,compact-framework,user-interface,invoke,C#,Events,Compact Framework,User Interface,Invoke,我从下面的代码中收到ArgumentException,我正在努力理解它堆栈跟踪中的最后一个条目是 System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean verifyAccess,
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr,
Binder binder, Object[] parameters, CultureInfo culture,
Boolean verifyAccess, StackCrawlMark& stackMark)
当我单步通过DeviceResponse时,会按照我的预期填充,并且目标已定位且符合预期,但targetForm.Invoke每次都会抛出
任何帮助都将不胜感激
事件定义为:
public static event EventHandler<MsgEventArgs<DeviceResponse>> DeviceResponseReceived;
已接收公共静态事件处理程序设备响应;
正在从此代码引发事件:
//Raise the event
if (DeviceResponseReceived != null)
{
if (DeviceResponseReceived.Target is System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm = DeviceResponseReceived.Target as System.Windows.Forms.Form;
targetForm.Invoke(DeviceResponseReceived, new MsgEventArgs<DeviceResponse>(deviceResponse));
}
}
//引发事件
如果(DeviceResponseReceived!=null)
{
if(DeviceResponseReceived.Target为System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm=设备响应接收。目标为System.Windows.Forms.Form;
调用(DeviceResponseReceived,新的msgevenTargets(deviceResponse));
}
}
MsgEventArgs是一个从EventArgs派生的通用事件参数类:
public class MsgEventArgs<T> : EventArgs
{
public MsgEventArgs(T value)
{
m_value = value;
}
private T m_value;
public T Value
{
get { return m_value; }
}
}
公共类MsgEventArgs:EventArgs
{
公共MsgEventArgs(T值)
{
m_值=值;
}
私人T m_值;
公共价值
{
获取{返回m_值;}
}
}
在我的表单中,我已在表单构造函数中注册事件:
DeviceResponse.DeviceResponseReceived += new EventHandler<MIASmartClient.Messaging.Transport.MsgEventArgs<DeviceResponse>>(DeviceResponse_DeviceResponseReceived);
DeviceResponse.DeviceResponseReceived+=新事件处理程序(DeviceResponse\u DeviceResponseReceived);
具体实施如下:
void DeviceResponse_DeviceResponseReceived(object sender, MIASmartClient.Messaging.Transport.MsgEventArgs<DeviceResponse> e)
{
_presenter.DeviceResponseReceived(e.Value);
}
void DeviceResponse\u DeviceResponseReceived(对象发送方,MIASmartClient.Messaging.Transport.MsgEventArgs e)
{
_演示者。设备响应接收(e.值);
}
感谢您在没有尝试代码的情况下花时间查看,以下代码中有一点让我感到奇怪:
if (DeviceResponseReceived != null)
{
if (DeviceResponseReceived.Target is System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm = DeviceResponseReceived.Target as System.Windows.Forms.Form;
targetForm.Invoke(DeviceResponseReceived, new MsgEventArgs<DeviceResponse>(deviceResponse));
}
}
if(DeviceResponseReceived!=null)
{
if(DeviceResponseReceived.Target为System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm=设备响应接收。目标为System.Windows.Forms.Form;
调用(DeviceResponseReceived,新的msgevenTargets(deviceResponse));
}
}
检查是否分配了
DeviceResponseReceived
委托(我假设是?),然后告诉targetForm
调用该委托。代理实际指向的是哪里?我想您真正想做的是调用targetForm
?中相应的方法,而不尝试代码,以下代码中有一件事让我感到奇怪:
if (DeviceResponseReceived != null)
{
if (DeviceResponseReceived.Target is System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm = DeviceResponseReceived.Target as System.Windows.Forms.Form;
targetForm.Invoke(DeviceResponseReceived, new MsgEventArgs<DeviceResponse>(deviceResponse));
}
}
if(DeviceResponseReceived!=null)
{
if(DeviceResponseReceived.Target为System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm=设备响应接收。目标为System.Windows.Forms.Form;
调用(DeviceResponseReceived,新的msgevenTargets(deviceResponse));
}
}
检查是否分配了DeviceResponseReceived
委托(我假设是?),然后告诉targetForm
调用该委托。代理实际指向的是哪里?我猜您真正想做的是从on事件调用targetForm
?中相应的方法:
事件是一种特殊的多播
只能从调用的委托
在它们所在的类或结构中
已声明(发布者类)
这是有道理的。声明事件的类(发布者)应该是唯一确定何时何地引发事件的类。这也是事件仅向客户机代码(订阅者)公开某些操作(如订阅和取消订阅)的原因
在代码中,您将DeviceResponseReceived事件作为targetForm.Invoke中的委托参数传递,并期望目标(表单)调用它。目标不是声明事件的位置,因此出现异常
您希望确保DeviceResponse\u DeviceResponseReceived事件处理程序在UI线程上执行,因为它可能会接触UI组件。然后在那里你可以检查一下。有关如何从其他线程更新UI的更多信息,请查看。来自on事件:
事件是一种特殊的多播
只能从调用的委托
在它们所在的类或结构中
已声明(发布者类)
这是有道理的。声明事件的类(发布者)应该是唯一确定何时何地引发事件的类。这也是事件仅向客户机代码(订阅者)公开某些操作(如订阅和取消订阅)的原因
在代码中,您将DeviceResponseReceived事件作为targetForm.Invoke中的委托参数传递,并期望目标(表单)调用它。目标不是声明事件的位置,因此出现异常
您希望确保DeviceResponse\u DeviceResponseReceived事件处理程序在UI线程上执行,因为它可能会接触UI组件。然后在那里你可以检查一下。查看有关如何从其他线程更新UI的更多信息。您好,Fredrik对我的知识不足表示歉意,我可能误解了您的意思,但我已将DeviceResponseReceived定义为同一类中的事件:public static event EventHandler DeviceResponseReceived我希望调用将事件引发到正确UI线程上的订阅表单。我错过了一些基本的东西吗?您好Fredrik为我的知识不足道歉,我可能误解了您,但我已将DeviceResponseReceived定义为同一类中的事件:public static event event handler DeviceResponseReceived我希望调用在正确的UI线程上将事件引发到订阅表单。我是否错过了一些基本的东西?在更彻底地阅读之后,我会提出一种不同的方法。但这真的很难,因为所有的声明都是断章取义的;我无法弄清楚事件是在哪个类中声明的,以及它与目标表单的关系。你能用mo更新这些问题吗