修改线程操作 [英] Modify the Thread Operation

查看:188
本文介绍了修改线程操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何通过使用另一个线程修改线程操作。我想更改在线程操作中更新的标签文本,但我想在不干扰原始操作的情况下通过另一个线程更改标签文本。例如,第一个线程标签文本加1,当它达到9时再次变为0,但是当我运行第二个线程时,我想将标签文本增加2,最大范围是100.谢谢

How can we modify the thread operation by using another thread.like i want to change the label text which is updating in a thread operation,but i want to change the text of label by another thread without disturbing the original operation.for example in first thread label text incremented by one and when it reaches to 9 it again becomes 0,but when i run my second thread i want to increment the label text by 2 and maximum range is 100.Thanks

推荐答案

您需要先了解GUI线程是什么。然后,您需要了解如何更新(从另一个线程)GUI线程上创建的GUI元素。最后,您可能需要了解如何在线程之间进行同步以避免线程操作之间的争用。

这看起来非常类似于家庭作业。因此,我让你自己查阅相应的信息。

干杯

Andi
You need to first understand what the GUI thread is. Then you need to understand how to update (from another thread) GUI elements that were created on the GUI thread. Finally, you might need to get acquainted with how to synchronize between threads to avoid races between the thread actions.
This looks very much like a homework assignment. Therefore, I let you look up the respective information yourself.
Cheers
Andi


这个问题没有意义因为你不能从UI线程以外的任何线程更改标签文本。什么是UI线程?它与您的标签对象有关,这是一个调用 Application.Run 的线程,其中包含创建拥有标签的所有控件的表单。您可以仅在同一个线程中修改标签文本(或任何内容)。要在任何其他线程中触发此类更改,您可以使用针对WPF和 System.Windows.Forms 调用机制。 >。



您不需要尝试直接修改任何UI属性,而是需要使用方法调用 BeginInvoke of System.Windows.Threading.Dispatcher (对于Forms或WPF)或 System。 Windows.Forms.Control (仅限表格)。



您将在我过去的答案中找到有关工作原理和代码示例的详细说明:

Control.Invoke()vs. Control.BeginInvoke () [ ^ ],

问题与Treevi ew扫描仪和MD5 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何在vb中的其他线程上运行keydown事件.net [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



如果要将某些操作委托给某个不是a的线程UI线程,您必须自己实现类似的同步和数据传递机制。请参阅我的文章详细解释:线程的简单阻塞队列通信和线程间调用 [ ^ ]。



-SA
The question makes no sense just because you cannot change the label text from any thread except the UI thread. What is the UI thread? It relation to your label object, this is the thread which called Application.Run with the form which created all the controls owning your label. You can modify the label text (or anything at all) only in the same very thread. To trigger such change in any other thread, you can use the mechanism of invocation which is implemented for UI thread for both WPF and System.Windows.Forms.

Instead of trying to modify any UI properties directly, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

If you want to delegate some actions to some thread which is not a UI thread, you will have to implement a similar synchronization and data passing mechanism by yourself. Please see my article where it is explained in detail: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA


这篇关于修改线程操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆