【问题标题】:Thread and Invoke, how to modify a label?Thread和Invoke,如何修改标签?
【发布时间】:2013-03-16 10:45:51
【问题描述】:

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

}

非常感谢您的帮助!

【问题讨论】:

  • “不起作用”是什么意思?请发布您收到的任何错误消息/异常。
  • 我添加了例外 :) 谢谢你的帮助!
  • 尝试在 Invoke 调用之前添加IntPtr tempHandle = this.Handle;。这将访问异常抱怨的窗口句柄。
  • 添加后'IntPtr tempHandle = this.Handle;'在 Invoke 调用之前,出现一个新错误:嵌入式语句不能是语句或带标签的语句
  • 你在哪里打电话StartThread?显示代码。

标签: c# multithreading invoke


【解决方案1】:

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

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

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

【讨论】:

  • 不错的 jordanhill123 !现在应用程序没有错误,但我的标签没有实现......请编辑:)
【解决方案2】:

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

这是工作代码:

表格:

public partial class SMSapplication : Form
{
    private SynchronizationContext context = null;
    private SerialPort port;

    public SMSapplication()
    {
        InitializeComponent();
        this.countSMSok.Text = "0";
        this.context = WindowsFormsSynchronizationContext.Current;
    }

    public void updateCountSMS(String label)
    {
        this.context.Post(new SendOrPostCallback(updateCountSMSSync), label);
    }

    private void updateCountSMSSync(object o)
    {
        string label = o as string;
        int num;

        if (label == this.countSMSok.Name.ToString())
        {
            if (int.TryParse(this.countSMSok.Text.ToString(), out num))
            {
                this.countSMSok.Text = (++num).ToString();
            }
        }
    }

    private void btnRequestStart_Click(object sender, EventArgs e)
    {
        clsWorker objclsWorker = new clsWorker(this, this.port, this.urlChecker.Text);
        objclsWorker.StartThread();
    }
}

和工人:

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

【讨论】:

  • 哎呀我的好,你是个好人!这是完美的工作!我已经明白我的愚蠢了。我创造了一个新的实例......感谢你,我会睡得不那么愚蠢:)!谢谢你+++
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 2011-10-25
  • 2016-09-27
  • 1970-01-01
  • 2017-05-21
  • 2018-09-29
  • 2019-05-06
  • 2019-03-04
相关资源
最近更新 更多