我有一个这样的代码:
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = " Device Testing...";
positive = false;
clearsensors_gui();
datarec = false;
cmd = 04;
datarec = serialport_FT(0, 1);
if (datarec)
{
char ab = Convert.ToChar(rec_data[1]);
//MessageBox.Show("\n" + ab + "\n");
int cab = Convert.ToInt16(ab);
int cabc1 = cab & 1;
int cabc2 = cab & 2;
int cabc3 = cab & 4;
int cabc4 = cab & 8;
int cabc5 = cab & 16;
int cabc6 = cab & 32;
if (cabc1 == 1)
ovalShape1.FillColor = Color.Green;
else
ovalShape1.FillColor = Color.Red;
if (cabc2 == 2)
ovalShape2.FillColor = Color.Green;
else
ovalShape2.FillColor = Color.Red;
if (cabc3 == 4)
ovalShape3.FillColor = Color.Green;
else
ovalShape3.FillColor = Color.Red;
if (cabc4 == 8)
ovalShape4.FillColor = Color.Green;
else
ovalShape4.FillColor = Color.Red;
if (cabc5 == 16)
ovalShape5.FillColor = Color.Green;
else
ovalShape5.FillColor = Color.Red;
if (cabc6 == 32)
ovalShape6.FillColor = Color.Green;
else
ovalShape6.FillColor = Color.Red;
toolStripStatusLabel1.Text = " Device Tested";
}
else
{
toolStripStatusLabel1.Text = "Try Again or Communication With Device Failure....";
}
}
上面的代码是读取传感器,即 datarec = serialport_FT(0, 1); 函数在 GUI 端为我提供了一个传感器输出,稍后将用红色\绿色的椭圆形ShapeX(1-6) 进行描述
问题:datarec = serialport_FT(0, 1);
这个函数需要 liltime ,所以 GUI 冻结到那个时候如何避免这种情况?
我尝试使用后台工作人员,但没有找到将整个过程放在哪里的地方,当它转到椭圆形形状并更改其属性时,也遇到了跨线程操作错误。
我没有得到要在后台使用的函数的哪一部分以及何时何地回到第一个线程
如果我必须使用线程,请帮助我使用 backgroundworker 或使用调用