0

I have a big problem and hope you guys can help me...

I need to start a Thread called ListenOnSocket, no problem so far..

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.

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.

Can anyone please help, I am really stuck, sorry if there is not much to go on I am quite new to C#.

Uğur Aldanmaz
  • 1,018
  • 1
  • 11
  • 16
  • 2
    Even if you fix the compiler error, that's not going to work. Your next question would be I'm getting [Cross thread exception](http://stackoverflow.com/questions/10775367/cross-thread-operation-not-valid-control-textbox1-accessed-from-a-thread-othe). So, I'm not going to fix it either; I recommend you to take a book and read. You'll never regret. Hint fixing this compiler error: Use lambda expression. – Sriram Sakthivel Mar 31 '15 at 18:25

4 Answers4

1

You can use Lambda Expression to pass parameter.

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

But you will get the CrossThreadException when the ListenOnSocket method execute. So you need to use BeginInvoke to set label text.

So search the CrossThreadException and why you will get it.

Note: I do not write the sample code for this, because searching is more beneficial.

Uğur Aldanmaz
  • 1,018
  • 1
  • 11
  • 16
0

You need to marshal this back to the UI thread:

public static void ListenOnSocket(Label lblstatus)
{
    this.BeginInvoke(new Action(() => 
    {
        //i want to change the label from here.
        lblstatus.text = "Hello";
    });
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • This solves the problem2 which I mentioned in my comment below the question, but OP still doesn't know how to call it in a thread. Btw if this is the only code in the method, OP doesn't needs a thread at all(I guess he have some more looking at the method name). – Sriram Sakthivel Mar 31 '15 at 18:39
0

It looks like you might actually want a ParameterizedThreadStart here. You would pass the Label in as its parameter. Control changes also need to be performed on the UI thread.

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

They key gotcha here is it's not valid to update a UI element (like a label) from a background thread.

If you have a long running task then you probably don't want to run that on the UI thread as it will block.

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.

For more information on how to do this see How to update the GUI from another thread in C#? 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.

Community
  • 1
  • 1
jaquesri
  • 188
  • 2