子執行緒傳值到介面顯示C#
阿新 • • 發佈:2018-12-03
delegate void ShowDataCallBack(string StringData); int i = 0; public void Data(string message) { if (label1.InvokeRequired) { ShowDataCallBack callBack = new ShowDataCallBack(Data); label1.Invoke(callBack, new object[] { message + " " + i.ToString() }); i = i + 1; } else { this.label1.Text = message + i.ToString(); } }
private void Form1_Load(object sender, EventArgs e) { //開啟軟體開始接受條碼資料 Thread thread1 = new Thread(new ThreadStart(GetBarcodeInfo)); thread1.Start(); // GetJsonData(); }
public void GetBarcodeInfo()
{
string receiveString = null;
byte[] receiveData = null;
//例項化一個遠端端點,IP和埠可以隨意指定,等呼叫client.Receive(ref remotePoint)時會將該端點改成真正傳送端端點
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
int port = 11000;
while (true)
{
UdpClient client = null;
client = new UdpClient(port);
receiveData = client.Receive(ref remotePoint);//接收資料
receiveString = Encoding.Default.GetString(receiveData);
Data(receiveString);
Thread.Sleep(1000);
Data("");
client.Close();//關閉連線
}
}