Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Invoke()时出现跨线程异常_C#_Winforms_Windows Mobile 5.0_Motorola Emdk_Smart Device Framework - Fatal编程技术网

C# 使用Invoke()时出现跨线程异常

C# 使用Invoke()时出现跨线程异常,c#,winforms,windows-mobile-5.0,motorola-emdk,smart-device-framework,C#,Winforms,Windows Mobile 5.0,Motorola Emdk,Smart Device Framework,我正在开发一个运行在基于WindowsMobile 5的条形码扫描仪上的应用程序。有时我会遇到跨线程异常,导致应用程序失败 该应用程序是用C#3.5编写的,构建在Motorola EMDK for.NET的基础上,但也使用了部分 在我的主窗体中,我有一个面板,根据应用程序所在的上下文更改内容。所有视图共享一个通用界面IContentView 我还使用一些后台线程来监视设备当前是否正在充电(触发用户注销),并监视设备是否可以连接到服务器 我在调用面板上的更改时使用John Skeets构造,以确保

我正在开发一个运行在基于WindowsMobile 5的条形码扫描仪上的应用程序。有时我会遇到跨线程异常,导致应用程序失败

该应用程序是用C#3.5编写的,构建在Motorola EMDK for.NET的基础上,但也使用了部分

在我的主窗体中,我有一个面板,根据应用程序所在的上下文更改内容。所有视图共享一个通用界面IContentView

我还使用一些后台线程来监视设备当前是否正在充电(触发用户注销),并监视设备是否可以连接到服务器

我在调用面板上的更改时使用John Skeets构造,以确保在正在更改的控件上调用更改:

    public void ShowContent(IContentView content)
    {
        contentPanel.Invoke(() =>
            {
                contentPanel.Controls.Clear();
                contentPanel.Controls.Add(content as UserControl);
                contentPanel.Focus();
            });
    }
contentPanel是System.Windows.Forms.Panel

但我仍然得到以下例外:

Control.Invoke must be used to interact with controls created on a separate thread.
   at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
   at System.Windows.Forms.Control.get_Parent()
   at System.Windows.Forms.Control.ControlCollection.Add(Control value)
   at BarcodeScanner.MainView.MainForm.<>c__DisplayClass1e.<ShowContent>b__1d()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Windows.Forms.Control.TASK.Invoke()
   at System.Windows.Forms.Control._InvokeAll()
   at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
   at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
   at System.Windows.Forms.Application.Run(Form fm)
   at BarcodeScanner.Program.Main()
Control.Invoke必须用于与在单独线程上创建的控件交互。
在Microsoft.AGL.Common.MISC.HandleAr(PAL_错误)
在System.Windows.Forms.Control.get_Parent()中
位于System.Windows.Forms.Control.ControlCollection.Add(控件值)
在BarcodeScanner.MainView.MainForm.c__DisplayClass1e.b__1d()中
在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi、对象obj、BindingFlags invokeAttr、绑定器绑定器、对象参数、CultureInfo区域性、布尔值isBinderDefault、程序集调用者、布尔值验证访问、堆栈爬网标记和堆栈标记)
位于System.Reflection.RuntimeMethodInfo.InternalInvoke(对象obj、BindingFlags invokeAttr、绑定器绑定器、对象[]参数、CultureInfo区域性、布尔验证访问、StackScrawMark和stackMark)
在System.Reflection.RuntimeMethodInfo.Invoke(对象obj、BindingFlags invokeAttr、绑定器绑定器、对象[]参数、CultureInfo区域性)
在System.Reflection.MethodBase.Invoke(对象obj,对象[]参数)处
在System.Windows.Forms.Control.TASK.Invoke()中
在System.Windows.Forms.Control.\u InvokeAll()中
在System.Windows.Forms.Control.WnProc(WM-WM、Int32-wParam、Int32-lParam)
在System.Windows.Forms.Control.\u InternalWnProc(WM WM、Int32 wParam、Int32 lParam)
在Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)上
在System.Windows.Forms.Application.Run(表单fm)上
在BarcodeScanner.Program.Main()上
我错过了什么?我是否需要执行其他操作来正确地将更改从线程封送到面板


非常感谢您的建议。

对于我来说,似乎在将
内容作为用户控件添加到
控件时出现了问题

检查创建了哪个线程
IContentView content
,我想不是在主线程中,这可能是问题所在

请看这里:

因此,在Windows窗体中,这似乎也是“禁止的”,但框架代码并没有严格检查这一点


因此,解决方案是在主线程中创建所有GUI控件,可能还使用
Invoke()

,这显然没有解决问题。您需要发布原始代码和异常堆栈跟踪,您在此处发布的内容对我们没有帮助。请确保在主线程中也创建了
IContentView
,它似乎是
UserControl
。非常感谢。我觉得你在这里很合适。在面板上设置的一些用户控件(主要是事件触发的控件)是在需要时构造的。我更改了它,以便在应用程序启动期间构造所有控件。谢谢你把我推向正确的方向。