none
C# WinForms - Control worker thread from UI thread RRS feed

  • Question

  • Hello!

    I am learning about Threading and I understand basics about updateing UI thread from worker thread.

    But how about vice versa.

    Lets say we have a worker thread with infinite while loop (with initial parameter maxCounter = 10).
    inside While loop is a counter which counts 1,2,3...10, 1,2,3..10, 1,2,3...10.....
    and it updates UI control (current value of the counter to the textbox)

    I would like to press a button on my form, and change a maxCounter value to 20 for example.
    I would now expect to count 1,2,3...20,1,2,3...20,....

    How do I do this?

    Should my maxCounter variable be treated as SharedResource and all operations (reads and writes) on this variable should be
    synchronized using lock?

    Thanks

    Sunday, December 10, 2017 2:38 PM

All replies

  • You will probably use int for maxCounter, and according to Documentation, reads and writes are atomic, i.e. multiple threads can write and read such data. Therefore, you do not need lock or other measures.

    The synchronisation is required for other cases, such as the ‘++’ operation or the long type.

    (See “C# Language Specification Version 6”, §5.5).

    Sunday, December 10, 2017 4:12 PM
  • Hello forum-dv,

    You don't need to use synchronized lock in your case, which is designed for allowing a thread to access the resources one by one. The following is a simple example to achieve it.

     public partial class Form4 : Form
        {
            public int maxCounter;
    
            public Form4()
            {
                InitializeComponent();
            }
            private void Form4_Load(object sender, EventArgs e)
            {
                maxCounter = 10;
                Thread t = new Thread(UpdateTextBox);
                t.IsBackground = true;
                t.Start();
            }
            public void UpdateTextBox()
            {
                while (true) {
                    for (int i = 0; i < maxCounter; i++) {
    
                            textBox1.Invoke(new Action<int>(
                            (x) => textBox1.Text = i.ToString())
                            , i);
                            Thread.Sleep(200);                 
                    }
                }                                         
            }
            private void button1_Click(object sender, EventArgs e)
            {
                maxCounter = 20;
            }
        }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 11, 2017 6:53 AM
    Moderator
  • Thank you both for clarifying some things to me.

    What should I do in case maxCounter is long (or int64).

    What should I do in case my variable is actualy an object.

    For example:

    public class MySettings
    {
        public long MaxCounter { get; set; }
        public int MinDelayInSeconds { get; set; }
        public string LogFileName { get; set; }
        public int MaxRecordsInLogFile { get; set; }
    }
    

    If I change object (instance of MySettings) from UI, how do I notify my worker thread?
    -> hey I have new settings for you. From now on you should do your job according to this new settings.

    Sorry for this beginner questions.

    Monday, December 11, 2017 5:08 PM
  • Hello forum-dv,

    >>If I change object (instance of MySettings) from UI, how do I notify my worker thread?

    In this case you don't need to notify your worker thread. You just access "MySettings.MaxCounter" from your worker thread. The worker thread will obtain the latest value. As Viorel_ said, reads and writes are atomic. It doesn't exist a circumstance that you read the old value from other thread after you has already changed the property value in UI Thread.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 12, 2017 5:53 AM
    Moderator