在调试WinForms应用程序时接收CrossThreadMessagingException [英] Receiving CrossThreadMessagingException while Debugging WinForms Application
问题描述
我使用Wndows XP SP3 x86 + VSTS 2008使用C#编写一个简单的Windows Forms应用程序。有一个名为 button1
的按钮,这里是它的点击事件的事件处理程序,当执行if语句时,有 Microsoft.VisualStudio.Debugger。 Runtime.CrossThreadMessagingException
。有没有人有什么好的想法有什么问题?
private void button1_Click(object sender,EventArgs e)
{
string recording = ConfigurationSettings.AppSettings [recording];
//执行以下if语句时抛出异常
if(recording.Equals(enable,StringComparison.InvariantCultureIgnoreCase))
{
CameraEncoder.Stop );
}
}
还有一些代码:
static WMEncoder CameraEncoder = new WMEncoder();
EDIT1:
我很困惑应用Marc在我的代码中使用 Invoke
的想法。我应该使用以下代码段吗?
CameraEncoder.Invoke((MethodInvoker)delegate
{
CameraEncoder .Stop();
});
通常,当我们看到(定期)就像一个工作线程或更新UI的定时器 - 但是一个按钮点击应该通过UI线程提高,所以我不认为这是通常的问题。
$ b $那么,什么是
相机
?什么是录制
?也没有解释,我们不能猜到没有引入额外的变量... 根据他们是什么,也许这将工作...
camera.Invoke((MethodInvoker)delegate
{
if(camera.Equals(enable,StringComparison.InvariantCultureIgnoreCase ))
{
Recording.Stop();
}
});
但不知道什么 canera
是,我正在抓住吸管...
I am using Wndows XP SP3 x86 + VSTS 2008 to write a simple Windows Forms application using C#. There is a button called button1
and here is the event handler for its click event, when executing the if statement, there is Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException
. Does anyone have any good ideas what is wrong?
private void button1_Click(object sender, EventArgs e)
{
string recording = ConfigurationSettings.AppSettings["recording"];
// exception thrown when executing the following if statement
if (recording.Equals("enable", StringComparison.InvariantCultureIgnoreCase))
{
CameraEncoder.Stop();
}
}
Some more code:
static WMEncoder CameraEncoder = new WMEncoder();
EDIT1:
I am confused how to apply Marc's idea of using Invoke
in my code. Should I use the following code segment?
CameraEncoder.Invoke((MethodInvoker) delegate
{
CameraEncoder.Stop();
});
Normally, the problem when we see this (regularly) is something like a worker thread or a timer updating the UI - but a button click should be raised through the UI thread, so I don't think it is the "usual problem".
So: what is camera
? And what is Recording
? Neither is explained, and we can't guess without introducing extra variables...
Depending on what they are, maybe this'll work...
camera.Invoke((MethodInvoker) delegate
{
if (camera.Equals("enable", StringComparison.InvariantCultureIgnoreCase))
{
Recording.Stop();
}
});
But without knowing what canera
is, I'm clutching at straws...
这篇关于在调试WinForms应用程序时接收CrossThreadMessagingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!