首页 > 解决方案 > Update and display constantly changing variable value in threads using winforms

问题描述

I'm new to winforms so when I click onto a button, it will perform a while loop where i++ is done inside the loop. So i will need the value to be printed out to keep track of the current value of i. How do I link the value to the text/label in winform?
Here's the function that I'm incrementing the i/j/k:

public static void TestSequence1()
        {
            int i = 0;
            Thread.Sleep(1000);
            //Console.WriteLine("Sequence1 working on thread: {0}", Thread.CurrentThread.ManagedThreadId);
            while (true)
            {
                waitHandle.WaitOne();
                int a = ++i;
                label1.Invoke((Action)(() => label1.Text = a.ToString()));
                //Console.WriteLine("Current i value: {0}", i);
            }
        }

I will need to show the value of j (shown above) cause I would like to know if the value of j stop increamenting when I click pause button to pause the thread as the workHandle.WaitOne() is inside the loop. Just trying whether I placed it right and also the waitHandle.Set() where I placed it in the Start button.

标签: c#multithreadingwinforms

解决方案


推荐阅读