简体   繁体   English

更改标签文本并在同一空隙中启动线程

[英]Change label text and start threads in the same void

I have a big problem and hope you guys can help me... 我有一个大问题,希望你们能帮助我...

I need to start a Thread called ListenOnSocket , no problem so far.. 我需要启动一个名为ListenOnSocketThread ,到目前为止没有问题。

Thread ListenOnSocket = new Thread(newThreadStart(Form1.ListenOnSocket));
ListenOnSocket.Start();

But when I want to change the label from within ListenOnSocket , I get an object reference is required for the non-static field, method or property. 但是,当我想从ListenOnSocket更改标签时,我得到了非静态字段,方法或属性的对象引用。

So normally you would passe the label on by doing this 所以通常您会通过这样做来传递标签

 public static void ListenOnSocket(Label lblstatus)
 {
     //i want to change the label from here.
     lblstatus.text = "Hello";
 }

but then I get 但后来我明白了

No overload for ListenOnSocket matches delegate System.Threading.Threadstart in my threadstart. ListenOnSocket没有重载匹配我的threadstart中的委托System.Threading.Threadstart。

Can anyone please help, I am really stuck, sorry if there is not much to go on I am quite new to C#. 谁能帮忙,我真的很受困扰,如果没有太多事情要做,对不起,我是C#的新手。

You can use Lambda Expression to pass parameter. 您可以使用Lambda表达式传递参数。

Thread ListenOnSocket = new Thread( () => { Form1.ListenOnSocket(yourParameter); } );
ListenOnSocket.Start();

But you will get the CrossThreadException when the ListenOnSocket method execute. 但是,当执行ListenOnSocket方法时,您将获得CrossThreadException So you need to use BeginInvoke to set label text. 因此,您需要使用BeginInvoke设置标签文本。

So search the CrossThreadException and why you will get it. 因此,搜索CrossThreadException以及为什么会得到它。

Note: I do not write the sample code for this, because searching is more beneficial. 注意:我不为此编写示例代码,因为搜索更有利。

You need to marshal this back to the UI thread: 您需要将其编组回UI线程:

public static void ListenOnSocket(Label lblstatus)
{
    this.BeginInvoke(new Action(() => 
    {
        //i want to change the label from here.
        lblstatus.text = "Hello";
    });
}

It looks like you might actually want a ParameterizedThreadStart here. 看来您实际上可能在这里需要ParameterizedThreadStart。 You would pass the Label in as its parameter. 您将传递Label作为其参数。 Control changes also need to be performed on the UI thread. 还需要在UI线程上执行控件更改。

    public void DoSomething()
    {
        // Actually a ParameterizedThreadStart, but you don't need to state this explicitly
        //var listenOnSocket = new Thread(new ParameterizedThreadStart(ListenOnSocket));
        var listenOnSocket = new Thread(ListenOnSocket);

        // Pass the label as the ParameterizedThreadStart parameter
        // TestLabel is a label within the form
        listenOnSocket.Start(TestLabel);
    }

    private void ListenOnSocket(object statusLabelObject) // Parameter must be of type Object
    {
        var statusLabel = statusLabelObject as Label;
        if (statusLabel == null)
            throw new ArgumentException(@"Parameter must be of type Label", "statusLabelObject");

        // Changes to controls must be performed on the UI thread.
        BeginInvoke((Action)(() => statusLabel.Text = @"text"));
    }

They key gotcha here is it's not valid to update a UI element (like a label) from a background thread. 他们在这里的关键在于,从后台线程更新UI元素(如标签)是无效的。

If you have a long running task then you probably don't want to run that on the UI thread as it will block. 如果您的任务运行时间较长,则可能不想在UI线程上运行该任务,因为它将阻塞。

Assuming that you're creating a thread because you have something that runs for too long to run on the UI thread, it might be worth looking into way of marshalling calls from background threads onto the UI thread. 假设您正在创建线程是因为您的某个东西在UI线程上运行的时间过长,可能值得研究将后台线程对UI线程的调用编组的方式。

For more information on how to do this see How to update the GUI from another thread in C#? 有关如何执行此操作的更多信息,请参见如何从C#中的另一个线程更新GUI? if you're looking to update the status from a long running task, you might want to look into background worker: MSDN: How to Use Background Worker which is a helper class designed to help with long running background tasks. 如果要从长时间运行的任务中更新状态,则可能需要调查后台工作程序: MSDN:如何使用后台工作程序 ,这是一个旨在帮助长时间运行的后台任务的帮助程序类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM