我的应用程序从电子设备绘制值.它在一秒钟内绘制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的问题 [ ^ ].
更多推荐
如何在线程中运行mouse
发布评论