调用onclick on按钮在C#中的不同线程中抛出了代码 [英] Invoking an onclick on button threw code in a different thread in C#

查看:265
本文介绍了调用onclick on按钮在C#中的不同线程中抛出了代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动相机的按钮.我有时想在不按按钮的情况下启动相机,然后抛出代码.

I have a button that starts my camera. I want to sometimes start the camera dinammicly threw code, without pressing the button.

代码:

private async void StartCamera()
{
    if (!CameraList.HasItems) //-------> CameraList is in the UI
    {
        MessageArea.Text = "No cameras found; cannot start processing";
        return;
    }

    // Clean leading/trailing spaces in API keys. 
    Properties.Settings.Default.FaceAPIKey = Properties.Settings.Default.FaceAPIKey.Trim();
    Properties.Settings.Default.EmotionAPIKey = Properties.Settings.Default.EmotionAPIKey.Trim();
    Properties.Settings.Default.VisionAPIKey = Properties.Settings.Default.VisionAPIKey.Trim();

    // Create API clients. 
    _faceClient = new FaceServiceClient(Properties.Settings.Default.FaceAPIKey);
    _emotionClient = new EmotionServiceClient(Properties.Settings.Default.EmotionAPIKey);
    _visionClient = new VisionServiceClient(Properties.Settings.Default.VisionAPIKey);

    // How often to analyze. 
    _grabber.TriggerAnalysisOnInterval(Properties.Settings.Default.AnalysisInterval);

    // Reset message. 
    MessageArea.Text = ""; // -------> MessageArea is in the UI

    // Record start time, for auto-stop
    _startTime = DateTime.Now;


    await _grabber.StartProcessingCameraAsync(CameraList.SelectedIndex); // This is the problem, with the previous two I just can skip it, but here I can't avoid the CameraList
}

private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    StartCamera();
}

我必须告诉我CameraList变量是一个UI组合框.

I must tell that the CameraList variable is a UI combobox.

因此,当我尝试使用StartCamera函数时,出现一个异常,内容为 {调用线程无法访问该对象,因为其他线程拥有它."}

So when I try to use the StartCamera function I get an exception that says {"The calling thread cannot access this object because a different thread owns it."}

当我尝试使用startButton UI并使用以下方法时,也会发生同样的事情:

The same thing happnes when I try to use the startButton UI and using:

    StartButton.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent));

在先前的线程中,我被告知:您只能从最初创建它的线程(即UI线程)访问UI元素.因此,您不能在后台线程上运行代码.这无关紧要最初是关于如何调用事件处理程序的问题.如果还有其他问题,请提出一个新问题."

in a previous thread I was told: "You can only access a UI element from the thread on which it was originally created, i.e. the UI thread. So you cannot run your code on a background thread. This has nothing to do with your original question about how to invoke the event handler though. Please ask a new question if you have another issue."

我在这里.

推荐答案

我打算为您发布一些示例代码,但是该线程

I was going to post some sample code for you, but there are a bunch of good answers on this thread How to update the GUI from another thread in C#?.

基本上,您必须使用委托来回调ui在其上运行的线程.这是因为您要对所有ui修改/输入进行序列化.您不希望2个不同的线程同时更改ui中的某些内容.以我的经验来看,用锁将您要回调的方法也包围起来并不是一个坏主意,就像在竞争条件下增加了一层保护一样.

Basically, you have to use a delegate to call back to the thread the ui is running on. This is because you want all ui modifications / input to be serialized. You don't want 2 different threads changing something in the ui at the same time. In my experience, it's not a bad idea to surround the method you are calling back with a lock as well, just as an added layer of protection against race conditions.

无论如何,请看一下该链接.它应该可以帮助您.

Anyway, give that link a look. It should help you out.

这篇关于调用onclick on按钮在C#中的不同线程中抛出了代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆