我如何在线程中运行 mouse_move 事件...?
我的应用程序绘制来自电子设备的值。它在一秒钟内绘制 10 个值。它会等待 100ms,然后再获取下一个值。等待由 Thread.Sleep(100); 给出,这部分工作正常。
我在 MouseMove 事件中显示鼠标位置。但要显示位置需要延迟,我之前已经给出过。
所以我想避免这种延迟。我尝试在像 new Thread(chartControl1_MouseMove).Start();
这样的线程中运行 MouseMove 事件。但它给出了以下错误:
- 最佳重载方法匹配 “System.Threading.Thread.Thread(System.Threading.ThreadStart)”有一些无效参数。
- 参数“1”:无法从“方法组”转换为“System.Threading.ThreadStart”
任何建议......???
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);
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();
. But it gives the following errors:
- The best overloaded method match for
'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has some invalid arguments.- Argument '1': cannot convert from 'method group' to 'System.Threading.ThreadStart'
Any suggestions...???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MouseMove 事件需要一些参数。但线程启动没有通过它们。假设该方法具有无效参数。
MouseMove event takes some arguments. But the thread start isnt passing them. its assuming that the method has void params.
这是因为我采用的方法
chartControl1_MouseMove
被声明为无法转换为
ThreadStart
委托,该委托采用的形式是为了使这项工作有效,您需要创建一个它自己的方法具有 void 返回类型并且没有参数,例如
并将代码行更改为:
这样它至少应该编译。然后,您必须在 foo 方法内实现所需的行为。
This is because your method
chartControl1_MouseMove
which I take is declared ascan not be converted to to a
ThreadStart
delegate which takes the form ofto make this work you will need to create a method of its own with void return type and no parameters like
and change your line of code to:
That way it should at least compile. You then have to implement the desired behaviour inside the
foo
method.您正在使用需要参数的方法。要么将该方法更改为不带参数,要么使用工作项:
您需要将方法
chartControl1_MouseMove(object sender, MouseEventArgs e)
转换为包含一个参数的方法,或者您可以恢复使用任务反而。一般来说,最好将工作代码放在自己的方法中,并让事件成为事件(如果在表单中,则已经异步调用)。然后,您从事件中调用工作方法(将其排队)。与线程相比,使用线程池有很多优点,请参阅这篇文章。
You are using a method that requires parameters. Either change the method to have no parameters, or use a worker item:
You need to convert your method
chartControl1_MouseMove(object sender, MouseEventArgs e)
to something that contains one argument, or you can revert to using Tasks instead. In general, it's often better to place your working code in its own method and let the event be the event (which is already called asynchronously if in a form). Then you call the working method (queue it) from the event.There are advantages of using a thread pool over threads, see this post.
使用parameterizedThreadStart或更好地将mouse_move处理程序的主体移动到其他方法并在线程上运行该方法。
Use parameterizedThreadStart or better move the body of mouse_move handler to some other method and run that method on thread.