简体   繁体   中英

Receiving CrossThreadMessagingException while Debugging WinForms Application

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...

也许相机对象是由另一个线程创建和管理的。您能否公开更多有关相机对象的代码?

I know WMEncoder is a COM object. You might try creating CameraEncoder in the GUI thread instead of a different thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM