c# - 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++ - 分离视频帧 C++ 的 Y 和 UV
- curl - 从 EC2 实例调用 AWS Gateway Rest API
- python - 二叉树Python的最小深度不起作用
- python - 如何使用 python 脚本 ssh 到远程 linux 服务器,并能够在 python 脚本中使用 ssh 时以用户指定的身份执行命令
- javascript - 生命周期钩子中的声明方法与VueJs中的方法属性有什么区别
- python - 如何在 Python 中创建和遍历决策树
- java - 访问另一个类中的树形图并遍历项目
- redis - 如何扩展 Redis 队列
- python - 根据另一列中的值选择组
- android - 我应该如何连接 Room、LiveData 和 ContentProvider?