跨线程操作无效:控制'label5'从其创建的线程以外的线程访问

时间:2015-11-15 16:45:56

标签: c# multithreading backgroundworker myo

我是编程的新手(对不起)我使用myo臂带在我的Windows窗体应用程序上遇到这些错误的问题:

跨线程操作无效:控制'label5'从其创建的线程以外的线程访问。

我已经读过我需要使用backgroundworker但我认为我不能在这个上使用backgroundworker类。所以,这是我的简化代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using MyoSharp.Device;
using MyoSharp.Communication;

namespace HandGestureApplication
{
public partial class Form1 : Form
{
    private readonly IChannel _channel;
    private readonly IHub _hub;
    public Form1()
    {
        InitializeComponent();
        MessageBox.Show("Lets get started");
        _channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create()));
        _hub = Hub.Create(_channel);

        _hub.MyoConnected += Hub_MyoConnected;
        _hub.MyoDisconnected += Hub_MyoDisconnected;
    }

    private void Hub_MyoDisconnected(object sender, MyoEventArgs e)
    {
        MessageBox.Show("disConnected");
        e.Myo.OrientationDataAcquired -= Myo_OrientationDataAcquired;
        e.Myo.SetEmgStreaming(false);
    }

    private void Hub_MyoConnected(object sender, MyoEventArgs e)
    {
        MessageBox.Show("Connected");
        e.Myo.OrientationDataAcquired += Myo_OrientationDataAcquired;
        e.Myo.SetEmgStreaming(true);
    }

    private void Myo_OrientationDataAcquired(object sender, OrientationDataEventArgs e)
    {
        const float PI = (float)System.Math.PI;

        var roll = (int)((e.Roll + PI) / (PI * 2.0f) * 10);
        var pitch = (int)((e.Pitch + PI) / (PI * 2.0f) * 10);
        var yaw = (int)((e.Yaw + PI) / (PI * 2.0f) * 10);

        label5.Text = roll.ToString(); // this is the error
    }
}

我已经使用过pictureBox并且工作正常但是当我使用文本框或标签时,它会给我错误。我知道这里有很多解决方案,我已经尝试了其中的一些,但我仍然是错误的。我希望你能帮助我。如果你能帮助我,我真的很感激。谢谢你们。

0 个答案:

没有答案
相关问题