如何在线程中运行mouse_move事件...? [英] How can i run mouse_move event in a thread...?

查看:122
本文介绍了如何在线程中运行mouse_move事件...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序从电子设备绘制值.它在一秒钟内绘制10个值.在获取下一个值之前,它会等待100ms.等待由

My application plot values from an electronic device. It plots 10 values in one second. It waits for 100ms before fetching the next value. The waiting is given by

Thread.Sleep(100);

给出.这大部分工作正常.

我在MouseMove事件中显示了鼠标位置.但是要显示位置,我需要延迟一些时间.

因此,我想避免这种延迟.我尝试在类似

This much part is working fine.

I am showing the mouse position in MouseMove event. But to show the position it takes a delay which i have given before.

So i want to avoid that delay. I tried to run the MouseMove event in thread like

new Thread(chartControl1_MouseMove).Start();

的线程中运行MouseMove事件.但是它给出了以下错误:

1.最佳重载方法匹配"System.Threading.Thread.Thread(System.Threading.ThreadStart)"具有一些无效的参数.
2.参数"1":无法从方法组"转换为"System.Threading.ThreadStart"


有任何建议... ???

. But it gives the following errors:

1. The best overloaded method match for ''System.Threading.Thread.Thread(System.Threading.ThreadStart)'' has some invalid arguments.
2. Argument ''1'': cannot convert from ''method group'' to ''System.Threading.ThreadStart''


Any suggestions...???

推荐答案

我假设您没有为绘图代码使用单独的线程?
如您所见,请勿在主线程上使用Thread.Sleep,它只会阻止其他所有功能.
相反,您可以使用背景线程进行数据绘制(但要小心,因为您将需要钉住任何UI元素,因为您只能从创建它们的线程(UI线程)中访问它们),或使用Timer代替您的主要UI线程,并在计时器Tick事件中进行绘制.

后者可能是最好的最佳解决方案:睡眠不是您应该依靠它来进行定期更新的原因,因为连续的睡眠结束之间的间隔取决于处理器负载,而不是规则的间隔.
I assume that you are not using a separate thread for your plotting code?
Do not use Thread.Sleep on the main thread - all it will do is block everything else from working, as you have seen.
Instead, either use a background thread to do your data plots (but be careful, because you will need to pinvoke any UI elements as you can only access those from the thread that created them: the UI thread), or use a Timer instead on your main UI thread, and do your plot in the timer Tick event.

The latter is likely to best best solution: Sleep is not something you should rely on to give you regular updates as the interval between successive Sleeps ending is dependent on the processor load, rather than a regular interval.


对不起,这不是解决方案,因为您没有解释任何内容.

我只想向您解释基本的事情.问题的标题本身已经显示出您的误解:什么是运行事件".您应该了解chartControl1_MouseMove 不是事件或事件实例.这只是方法.现在,它可以是任何东西,但是方法的名称表明这是设计器创建的用于处理对象chartControl1的事件MouseMove的自动生成的方法. (顺便说一句,您永远不要使用这样的名称;自动生成的所有内容都应以某种方式在语义上进行重命名;这是一个很好的工具,可以从正确使用的符号中分辨出无人看管的符号.)

由于其签名,该方法不能作为线程方法运行.一个线程需要一个无参数的方法,静态的或实例化的.另一个签名是接受一个对象的签名.我建议永远不要以这种方式将参数传递给线程,因为必须进行类型转换.我基于传递"this"提出了一种更好的方法.这实际上意味着使用非静态(即实例)方法和线程包装器.查看我过去的解决方案如何将ref参数传递给线程 [ ^ ]和更改线程(生产者)启动后的参数 [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [
Sorry, this is not a solution, because you did not explain anything.

I just want to explain you the basic things. The title of the question itself already shows your misunderstanding: what is "run event". You should understand that chartControl1_MouseMove is not event or event instance. This is just the method. Now, it can be anything at all, but the name of the method suggests that this is an auto-generated method created by the Designer to handle the event MouseMove of the object chartControl1. (By the way, you should never use such names; everything auto-generated should be renamed somehow semantically; in this is a good tool to tell unattended symbol from the ones properly used.)

This method does not run as a thread method due to its signature. A thread needs a parameter-less method, static or instance one; another signature is the one accepting one object. I recommend never pass a parameter to the thread in this way, because of required type casting; I proposed much better method based on passing "this"; which is practically means using a non-static (i.e. instance) method and a thread wrapper. See my past solutions How to pass ref parameter to the thread[^] and change parameters of thread (producer) after it started[^].

Now, I have no idea why would you even think about doing something with the mouse event handler in a separate thread, but cannot make any sense. For just one thing, mouse event processing is an integral part of UI and could never be processed in a non-UI thread.

As to the communication with your electronic device, I would strongly recommend to get back to a separate thread. Only you should work with the IU using invocation mechanism, see below. As to the regular intervals: I don''t think you need strictly regular time intervals. In case you really need good accuracy in timing, timer won''t help you anyway, but working with the timer accurately is much more difficult. Threads are at least reliable. One thing about thread: for better (much better accuracy) don''t use System.Windows.Forms.Timer — it''s very bad. Use System.Threading.Timer or System.Timers.Timer.

For more about threading with UI, please see my past solutions:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^],
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA


这篇关于如何在线程中运行mouse_move事件...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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