我如何在线程中运行 mouse_move 事件...?

发布于 2024-11-28 13:21:12 字数 479 浏览 1 评论 0原文

我的应用程序绘制来自电子设备的值。它在一秒钟内绘制 10 个值。它会等待 100ms,然后再获取下一个值。等待由 Thread.Sleep(100); 给出,这部分工作正常。

我在 MouseMove 事件中显示鼠标位置。但要显示位置需要延迟,我之前已经给出过。

所以我想避免这种延迟。我尝试在像 new Thread(chartControl1_MouseMove).Start(); 这样的线程中运行 MouseMove 事件。但它给出了以下错误:

  1. 最佳重载方法匹配 “System.Threading.Thread.Thread(System.Threading.ThreadStart)”有一些无效参数。
  2. 参数“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:

  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...???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烏雲後面有陽光 2024-12-05 13:21:13

MouseMove 事件需要一些参数。但线程启动没有通过它们。假设该方法具有无效参数。

MouseMove event takes some arguments. But the thread start isnt passing them. its assuming that the method has void params.

守望孤独 2024-12-05 13:21:13

这是因为我采用的方法 chartControl1_MouseMove 被声明为

public void chartControl1_MouseMove(object sender,MouseEventArgs e)

无法转换为 ThreadStart 委托,该委托采用的形式是

public delegate void ThreadStart()

为了使这项工作有效,您需要创建一个它自己的方法具有 void 返回类型并且没有参数,例如

private void foo()

并将代码行更改为:

new Thread(foo).Start();

这样它至少应该编译。然后,您必须在 foo 方法内实现所需的行为。

This is because your method chartControl1_MouseMove which I take is declared as

public void chartControl1_MouseMove(object sender,MouseEventArgs e)

can not be converted to to a ThreadStart delegate which takes the form of

public delegate void ThreadStart()

to make this work you will need to create a method of its own with void return type and no parameters like

private void foo()

and change your line of code to:

new Thread(foo).Start();

That way it should at least compile. You then have to implement the desired behaviour inside the foo method.

叹沉浮 2024-12-05 13:21:13

您正在使用需要参数的方法。要么将该方法更改为不带参数,要么使用工作项:

ThreadPool.QueueUserWorkItem(o => Method(m));

您需要将方法 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:

ThreadPool.QueueUserWorkItem(o => Method(m));

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.

玩心态 2024-12-05 13:21:13

使用parameterizedThreadStart或更好地将mouse_move处理程序的主体移动到其他方法并在线程上运行该方法。

Use parameterizedThreadStart or better move the body of mouse_move handler to some other method and run that method on thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文