So I have this C# application that is getting values (6 variables) from Arduino. Initially I used a timer which calls some read functionts every 100ms but it hangs my UI and responds a little heavy. I want to use a backgroundworker which continuously reads those variables. I put those read calls in DoWork method and I put the values in variables from C# and assign them in a label or something. But it doesn't work. (sorry for my english)
namespace testtemp
{
public partial class tempreaderform : Form
{
public tempreaderform()
{
InitializeComponent();
}
communicator comport = new communicator();
Boolean portConnection = false;
String red_light1;
private void button1_Click(object sender, EventArgs e)
{
if (comport.connect(57600, "I'M ARDUINO", 4, 8, 16))
{
label1.Text = "Connection Successful - Connected to "+comport.port;
portConnection = true;
backgroundWorker1.RunWorkerAsync(); // I am not sure if here I should start the backgroundworker.
}
else
{
label1.Text = "Not connected . . . ";
portConnection = false;
}
}
private void groupBox1_Paint(object sender, PaintEventArgs e)
{
Graphics ellipse1 = groupBox1.CreateGraphics();
Brush red_on = new SolidBrush(Color.Red);
Brush red_off = new SolidBrush(Color.DarkRed);
label2.Text = "Red : " + red_light1; //Here is a label which I want to show "1" if "red_light1" is "1" and "0" if "red_light1" is "0"
if (red_light1 == "1") ellipse1.FillEllipse(red_on, 250, 133, 24, 24);
else ellipse1.FillEllipse(red_off, 250, 133, 24, 24); // Here is what I really want to do (the label.Text part was only to see the value) I want to color a ellipse depending on "red_light1" value.
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
red_light1 = comport.message(4, 8, 32);
}
}
}
}
}
NOTE: I tryied this "label1.Text = red_light1" in DoWork method but it says that I can't give values to variables created in another thread, something like this.