代码如下,但是有不少错误,先是这句:
if (form1.labelHour.InvokeRequired)
在labelHour下有红线,我把这个label在designer处把private改成public还是报错,还有问题就是不能修改这个label值,这是什么原因?
```c#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace atinyTest_New
{
public delegate void MyInvoke(string str);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button_Start_Click(object sender, EventArgs e)
{
Test test1 = new Test();
Thread thread = new Thread(new ThreadStart(test1.fun));
thread.Start();
}
}
public class Test
{
bool enableTest = false;
private string myIp;
private byte myFrameCnt = 0;
const int frameSize = 1280;
float[,] frameData = new float[frameSize, 3];
private int TotalFrameCount = 0; //总连接次数
private int FailedFrameCount = 0; //失败连接次数
private double Avg = 0; //偏差均值
private double stdDeviation; //数据偏差均方差
private int DataExceptionCount = 0; //数据异常次数
TimeSpan totalSpan;
TimeSpan currentSpan;
Form form1;
public Test()
{
form1 = new Form1();
}
public void fun()
{
//_myInvoke("dddd");
SetText("ddd");
}
private void SetText(string s)
{
if (form1.labelHour.InvokeRequired)
{
MyInvoke _myInvoke = new MyInvoke(SetText);
form1.Invoke(_myInvoke, new object[] { s });
}
else
{
form1.labelHour.Text = s;
}
}
}
}
```