0

私はプログラミングの初心者です (申し訳ありません)。myo アームバンドを使用した Windows フォーム アプリケーションでこれらのエラーに関する問題があります。

クロススレッド操作が無効です: コントロール 'label5' は、それが作成されたスレッド以外のスレッドからアクセスされました。

バックグラウンドワーカーを使用する必要があることを読みましたが、これにはバックグラウンドワーカークラスを使用できないと思います。だから、ここに私の単純化されたコードがあります:

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を使用しましたが、正常に動作しますが、テキストボックスまたはラベルを使用すると、そのエラーが発生します。ここには多くの解決策があることを知っています。それらのいくつかを試しましたが、それでもエラーになります。あなたが私を助けてくれることを願っています。あなたが私を助けてくれれば本当に感謝します. みんなありがとう。

4

0 に答える 0