0

I'm looking for the answer for a long, however I haven't find an answer how should communication between threads/main thread and run-time created threads look like. I had this problem in Java and now it comes back in C#.

Let's say I would like to write C# application, that has the form with simple text Label and I want to run thread(s) that increments numeric value written in that label. I'm creating main class with the form, and thread's class. then I'm creating new object of the thread's class in my main class and starting thread on it.

How can I change text of a label which is defined in main?

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Niko
  • 812
  • 9
  • 22
  • Possible duplicate of http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c?rq=1 ? – Fermin Silva Aug 07 '15 at 18:53
  • C# (in contrast to java) is a modern, properly designed programming language which has language features that help writing asynchronous / threaded code with ease. For more information, see [Asynchronous Programming with Async and Await (C# and Visual Basic)](https://msdn.microsoft.com/en-us/library/hh191443.aspx) on MSDN. – Federico Berasategui Aug 07 '15 at 19:03

1 Answers1

0

So if I understand you correctly, you're asking how to change the GUI from another thread.

This is done through a construct called the SynchronizationContext, which essentially provides a way to run code on another thread. So in your case, if you wanted to change the text of a label defined in the GUI thread, you would take the SynchronizationContext corresponding to your GUI thread and post code to it through your other thread.

Another concept you will have to get familiar with are Tasks. A Task is an abstraction that's functionally the same thing as a thread. Two Tasks can run at the same time. Task.Run starts a new Task with its workload represented by a function.

With that said, here's an example in WPF:

public class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var context = SynchronizationContext.Current;

        Task.Run(() => context.Post(state => Button.Content = "Hello World!"));
    }
}

Notice that even though I'm inside Task.Run (which means I'm not on the GUI thread), I'm still able to execute code on the Button by posting to the window's SynchroniztaionContext.


Edit: If you're not comfortable with Task yet, and you'd like to use Thread instead, you may be able to do that as well:

var context = SynchronizationContext.Current;

var thread = new Thread(() => context.Post(state => Button.Content = "Hello World!"));

thread.Start();
James Ko
  • 32,215
  • 30
  • 128
  • 239
  • So it means its not possible to run a thread that changes GUI? It must me task? As far as I understand it changes Button content once. I would like to do more complicated thing. For example calculate something in thread and update progress bar. For now on, I created resources class and I send my data there. – Niko Aug 08 '15 at 08:30