如何在线程中运行mouse

编程入门 行业动态 更新时间:2024-10-25 22:28:42
本文介绍了如何在线程中运行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参数传递给线程 [ ^ ]和更改线程(生产者)启动后的参数 [ ^ ]. 现在,我不知道您为什么还要考虑在单独的线程中使用鼠标事件处理程序来做一些事情,但却毫无意义.仅一件事,鼠标事件处理是UI不可或缺的一部分,决不能在非UI线程中进行处理. 关于与您的电子设备的通信,我强烈建议回到单独的线程.只有您应该使用调用机制来处理IU,请参见下文.关于定期间隔:我认为您不需要严格的定期间隔.万一您确实需要良好的计时精度,计时器无论如何都无法为您提供帮助,但是准确地使用计时器要困难得多.线程至少是可靠的.关于线程的一件事:为了获得更好(更好的准确性),请不要使用System.Windows.Forms.Timer-这是非常糟糕的.使用System.Threading.Timer或System.Timers.Timer. 有关使用UI进行线程化的更多信息,请参阅我过去的解决方案: Control.Invoke()与Control.BeginInvoke() [ ^ ], Treeview扫描仪和MD5的问题 [ ^ ], Control.Invoke()与Control.BeginInvoke() [ ^ ], Treeview Scanner和MD5的问题 [ ^ ].
—SA
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

本文发布于:2023-11-23 10:34:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   如何在   mouse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!