0

I'm a newbie on programming (sorry for that) and I have a problem regarding these errors on my windows form application using the myo armband:

Cross-thread operation not valid: Control 'label5' accessed from a thread other than the thread it was created on.

I've read that i need to use backgroundworker but i think i cant use backgroundworker class on this one. so, here is my simplified code:

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

I've used pictureBox and it works fine but when i use textbox or label, it gives me that error. I know there's a lot of solutions here, i've tried some of them but still i'm in error. I hope you can help me. i really appreciate if you will help me. Thanks guys.

nath
  • 1
  • You can't modify GUI components from a thread that doesn't own the underlying window handle. You need to use delegates and Invoke, https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke(v=vs.110).aspx – jensa Nov 15 '15 at 16:49
  • 1
    Just looking at the related column on the right there are at least 10 questions like this and you have got a list of them while you were writing your question. Why don't you look at them before posting again another one? – Steve Nov 15 '15 at 16:57

0 Answers0