C# 线程和调用,如何修改标签?

C# 线程和调用,如何修改标签?,c#,multithreading,invoke,C#,Multithreading,Invoke,我用c#开发一个应用程序,我的线程和UI有问题 我想在线程运行时在标签上添加+1。问题是我不知道怎样才能解决这个问题。。。我读了很多“如何”的书,但是解决方案不能与我的应用程序配合使用 我的线程类: class clsWorker { //Thread myThread = new Thread(new ThreadStart(ThreadLoop)); public SerialPort port; public String url

我用c#开发一个应用程序,我的线程和UI有问题

我想在线程运行时在标签上添加+1。问题是我不知道怎样才能解决这个问题。。。我读了很多“如何”的书,但是解决方案不能与我的应用程序配合使用

我的线程类:

 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();
    }

}

非常感谢你的帮助

确保创建标签的实例并将标签添加到控件中

    this.countSMSnok = new System.Windows.Forms.Label();
    this.countSMSok = new System.Windows.Forms.Label();

    this.Controls.Add(this.countSMSnok );
    this.Controls.Add(this.countSMSok );

您不能初始化
SMSapplication
类的新对象,更新它并期望它更新您的第一个表单。这些是不同的“东西”。 您还应该使用
SynchronizationContext
而不是
invoke

以下是工作代码:

表格:

和工人:

class clsWorker
{
    public SerialPort port;
    public String url;
    SMSapplication clsobjAPP = null;

    public clsWorker(SMSapplication app, SerialPort serialPort, String urlChamp)
    {
        this.clsobjAPP = app;
        this.port = serialPort;
        this.url = urlChamp;
    }

    public void StartThread()
    {
        new Thread(new ThreadStart(ThreadLoop)).Start();
    }


    public void ThreadLoop()
    {
        clsobjAPP.updateCountSMS("countSMSok");
    }
}

“不起作用”是什么意思?请发布您收到的任何错误消息/异常。我已添加了异常:)谢谢您的帮助!尝试添加
IntPtr tempHandle=this.Handle在调用之前。这将访问异常所抱怨的窗口句柄。添加“IntPtr tempHandle=This.handle;”后在调用之前,会出现一个新错误:嵌入语句不能是语句或带标签的语句调用
StartThread
?显示代码。很棒的jordanhill123!现在应用程序没有bug,但是我的标签没有实现。。。请编辑:)呼,我的好,你是个好孩子!这是完美的工作!我明白我的愚蠢。我创建了一个新实例。。。多亏了你,我才会睡得不那么傻:)!!谢谢+++
class clsWorker
{
    public SerialPort port;
    public String url;
    SMSapplication clsobjAPP = null;

    public clsWorker(SMSapplication app, SerialPort serialPort, String urlChamp)
    {
        this.clsobjAPP = app;
        this.port = serialPort;
        this.url = urlChamp;
    }

    public void StartThread()
    {
        new Thread(new ThreadStart(ThreadLoop)).Start();
    }


    public void ThreadLoop()
    {
        clsobjAPP.updateCountSMS("countSMSok");
    }
}