我在 c# 中处理应用程序,但我的线程和 UI 有问题...
我想在线程运行时在我的标签上添加 +1。问题是我不知道如何解决这个问题......我已经阅读了很多“如何”,但解决方案 donc 适用于我的应用程序..
我的线程类:
class clsWorker
{
//Thread myThread = new Thread(new ThreadStart(ThreadLoop));
public SerialPort port;
public String url;
Thread t;
clsSMS clsobjSMS = new clsSMS();
SMSapplication clsobjAPP = new SMSapplication();
public clsWorker(SerialPort serialPort, String urlChamp)
{
this.port = serialPort;
this.url = urlChamp;
}
public void StartThread()
{
t = new Thread(new ThreadStart(ThreadLoop));
t.Start();
}
public void ThreadLoop()
{
// How I can add +1 on the countSMSok label ??
clsobjAPP.updateCountSMS("countSMSok");
}
}
我的应用程序类:
public partial class SMSapplication : Form
{
public void updateCountSMS(String label)
{
int num;
this.countSMSnok = new System.Windows.Forms.Label();
this.countSMSok = new System.Windows.Forms.Label();
this.Controls.Add(this.countSMSnok );
this.Controls.Add(this.countSMSok );
if (label == this.countSMSok.Name.ToString())
{
if (int.TryParse(this.countSMSok.Text.ToString(), out num))
this.countSMSok.Invoke((MethodInvoker)(() => this.countSMSok.Text = num++.ToString()));
}
else if (label == this.countSMSnok.Name.ToString())
{
if (int.TryParse(this.countSMSnok.Text.ToString(), out num))
this.countSMSnok.Invoke((MethodInvoker)(() => this.countSMSnok.Text = num++.ToString()));
}
}
private void btnRequestStart_Click(object sender, EventArgs e)
{
this.btnRequestStart.Enabled = false;
this.btnRequestStop.Enabled = true;
objclsWorker = new clsWorker(this.port, this.urlChecker.Text);
objclsWorker.StartThread();
}
}
非常感谢您的帮助!