2014-12-03 46 views
0

我在写一个使用.NET 4.5的WPF应用程序。该应用程序将显示来自串行端口的数据。目前,我想使用串口的DataReceived事件来更新多行文本框的内容。如何使用Dispatcher.Invoke连接事件处理程序?

当我运行该应用程序时,下面的注释行将抛出“An unhandled exception of type 'System.Reflection.TargetParameterCountException' occurred in mscorlib.dll”和“{Parameter count mismatch}”内部异常为空。

事件处理程序中的RxString变量(它是一个类成员)从串行端口获取值,以便数据通过串行端口从设备流入事件处理程序。我不知道如何进一步调试。基于我为WinForms应用程序找到的示例代码,这似乎是正确的。

private void DisplayText(object sender, EventArgs e) 
{ 
    textbox1.AppendText(RxString); 
} 

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    RxString = serialPort1.ReadExisting(); 
    textbox1.Dispatcher.Invoke(new EventHandler(DisplayText)); // *** Exception occurs here 
} 

的堆栈跟踪:

at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Reflection.RuntimeMethodInfo.UnsafeInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
    at System.Delegate.DynamicInvokeImpl(Object[] args) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
    at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.ProcessQueue() 
    at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
    at System.Windows.Threading.Dispatcher.Run() 
    at System.Windows.Application.RunDispatcher(Object ignore) 
    at System.Windows.Application.RunInternal(Window window) 
    at System.Windows.Application.Run(Window window) 
    at System.Windows.Application.Run() 
    at WpfSerial.App.Main() in c:\Users\scott\Documents\Visual Studio 2013\Projects\SerialStuff\WpfSerial\WpfSerial\obj\Debug\App.g.cs:line 0 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

回答

0

尝试更换

textbox1.Dispatcher.Invoke(new EventHandler(DisplayText)); 

与此:

textbox1.Dispatcher.Invoke(DisplayText(RxString)); 

阿尔特DisplayText()取一个字符串参数是这样的:

private void DisplayText(string rxString) 
{ 
    textbox1.AppendText(rxString); 
} 

我不认为你需要“DisplayText”是一个EventHandler,也不需要“RxString”是一个全局变量。

相关问题