我是编程新手(对此感到抱歉),我对使用 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
}
}
我使用了图片框,它工作正常,但是当我使用文本框或标签时,它给了我这个错误。我知道这里有很多解决方案,我已经尝试了其中一些,但我仍然错了。我希望你能帮助我。如果你能帮助我,我真的很感激。多谢你们。