带Invoke()的TargetParameterCountException

时间:2014-04-28 14:01:40

标签: c# winforms generics lambda extension-methods

我正在尝试更新控件,但不断遇到交叉线程异常错误(注意:只有在使用VS进行调试时才会出现交叉线程异常错误,当我运行.exe时工作正常)。所以我对此进行了调整,并得出了Invoke方法。现在我收到了这个错误。

我正在尝试使用扩展方法:

    public static void Invoke<T>(this T c, Action<T> action) where T : Control
    {
        if (c.InvokeRequired)
            c.TopLevelControl.Invoke(action);
        else
            action(c);
    }

更新进度条。使用以下代码。

pb.TextLabel.Invoke(c => c.Text = "Copying " + Path.GetFileName(this.mp2File));

但我一直收到以下错误:

  

System.Reflection.TargetParameterCountException未处理
  HResult = -2147352562消息=参数计数不匹配   Source = System.Windows.Forms StackTrace:          在System.Windows.Forms.Control.MarshaledInvoke(Control caller,Delegate方法,Object [] args,布尔同步)          在System.Windows.Forms.Control.Invoke(Delegate方法,Object [] args)          at AV_Processor.Extensions.Invoke [T](T c,Action`1 action)          at AV_Processor.FormProcessor.Copy(Int32 _i)          at AV_Processor.FormProcessor.ProcessTable(Int32 pState)          at AV_Processor.FormProcessor.buttonProcess_Click(Object sender,EventArgs e)          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在System.Windows.Forms.Control.WmMouseUp(消息&amp; m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息&amp; m)          在System.Windows.Forms.ButtonBase.WndProc(消息&amp; m)          在System.Windows.Forms.Button.WndProc(消息&amp; m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr)   dwComponentID,Int32原因,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Form.ShowDialog(IWin32Window所有者)          at C:\ Users \ madeup \ Documents \ Visual Studio中的AV_Interface.FormInterface.buttonSubmit_Click(Object sender,EventArgs e)   2010 \ Projects \ madeup \ madeup \ AV-Interface \ FormInterface.cs:第756行          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在System.Windows.Forms.Control.WmMouseUp(消息&amp; m,MouseButtons按钮,Int32点击)          在System.Windows.Forms.Control.WndProc(消息&amp; m)          在System.Windows.Forms.ButtonBase.WndProc(消息&amp; m)          在System.Windows.Forms.Button.WndProc(消息&amp; m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr)   dwComponentID,Int32原因,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32)   原因,ApplicationContext上下文)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32)   原因,ApplicationContext上下文)          at C:\ Users \ donkeykong \ Documents \ Visual Studio 2010 \ Projects \ blahblah中的AV_Interface.FormInterface.Main()   Source Controlled \ bababa \ sosdfg \ FormInterface.cs:第362行          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ExecutionContext.RunInternal(ExecutionContext   executionContext,ContextCallback回调,对象状态,布尔值   preserveSyncCtx)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean   preserveSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()InnerException:

我将它设置为编译到所有CPU,但只有当我在VS调试器中运行它时才会出现交叉线程异常错误。如何在调试器中运行上面的代码?

0 个答案:

没有答案