将委托相关的函数移动到不同的线程

发布于 2024-08-30 13:29:42 字数 908 浏览 5 评论 0原文

我们正在用 C# 开发一个与串行端口通信的库。我们有一个赋予委托的函数。问题是我们希望它在不同的线程中运行。

我们尝试创建一个新线程(称为 DatafromBot),但继续使用它,如下所示(第一行):

comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

DatafromBot = new Thread(comPort_DataReceived);
DatafromBot.Start();

comPort_DataReceived 定义为:

Thread DatafromBot;
public void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) {  ... }

发生以下错误:

错误3 最好的重载方法 匹配“System.Threading.Thread.Thread(System.Threading.ThreadStart)” 有一些无效的 参数 C:...\IR52cLow\CommunicationManager.cs 180 27 IR52cLow

错误 4 参数“1”:无法转换 从“方法组”到 'System.Threading.ThreadStart' C:...\IR52cLow\CommunicationManager.cs 180 38 IR52cLow

我们应该如何转换它以使其编译有什么想法吗? 请注意,comPort.DataReceived(注意“.”而不是“_”)位于系统库中,无法修改。

We are developing a library in C# that communicates with the serial port. We have a function that is given to a delegate. The problem is that we want it to be run in a different thread.

We tried creating a new thread (called DatafromBot) but keep using it as follows (first line):

comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);

DatafromBot = new Thread(comPort_DataReceived);
DatafromBot.Start();

comPort_DataReceived is defined as:

Thread DatafromBot;
public void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) {  ... }

The following errors occur:

Error 3 The best overloaded method
match for 'System.Threading.Thread.Thread(System.Threading.ThreadStart)'
has some invalid
arguments C:...\IR52cLow\CommunicationManager.cs 180 27 IR52cLow

Error 4 Argument '1': cannot convert
from 'method group' to
'System.Threading.ThreadStart' C:...\IR52cLow\CommunicationManager.cs 180 38 IR52cLow

Any ideas of how we should convert this to get it to compile?
Please note that comPort.DataReceived (pay attention to "." instead of "_") lies within a system library and cannot be modified.

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

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

发布评论

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

评论(3

抚笙 2024-09-06 13:29:42

每个Delegate都有一个名为BeginInvoke()的方法。调用此方法时,会在新线程上调用该函数。所以你不必明确地这样做。
例如,

public delegate string delegate1(int x);

Delegate1 d1 = new Delegate1(Add);

D1.BeginInvoke(10, null, null);

这里 Add 方法在辅助线程上运行。主线程继续其操作,不等待辅助线程结束。主线程不会知道辅助线程何时结束。

如果您需要在方法完成时收到通知,那么您可以使用 AsyncCallback 类。

D1.BeginInvoke(10, AsyncCallback(AddComplete), null);

这里,当辅助线程完成执行时,它会在其线程上调用 AddComplete() 方法。
主线程不等待辅助线程。

检查 MSDN。

Every Delegate has a method called BeginInvoke(). When called this method invokes the function on new thread. So you don't have to do it explicitly.
For example

public delegate string delegate1(int x);

Delegate1 d1 = new Delegate1(Add);

D1.BeginInvoke(10, null, null);

Here Add method runs on a secondary thread. Primary thread continue with its operation and does not wait for the secondary thread to end. The primary thread will not come to know when the secondary thread has ended.

If you need notification when the method is complete, then you can you AsyncCallback class.

D1.BeginInvoke(10, AsyncCallback(AddComplete), null);

Here when secondary thread completes execution it invokes AddComplete() method on its thread.
The primary thread does not wait for the secondary thread.

Check MSDN.

知足的幸福 2024-09-06 13:29:42

这是不行的,当 SerialPort 接收到东西时,就会触发 DataReceived 事件。您不能只是启动一个线程并希望从端口读取一些内容。

更重要的是,DataReceived 事件已经在单独的线程上运行。 SerialPort 类使用线程池线程来运行事件。使用您自己的线程从端口读取数据是可以的,但您不想使用 DataReceived。您只需使用阻塞调用,任何 ReadXxx() 方法都符合条件。

This is not okay, the DataReceived event is triggered when the SerialPort receives something. You cannot just start a thread and hope that there will be something to read from the port.

More to the point, the DataReceived event already runs on a separate thread. The SerialPort class uses a threadpool thread to run the event. Using your own thread to read data from the port is okay but then you don't want to use DataReceived. You simply use the blocking calls, any of the ReadXxx() methods qualify.

夏末染殇 2024-09-06 13:29:42
new Thread(() => comPort_DataReceived(null, null));

您还可以传递参数 sender 和 eventargs:

var e = new SerialDataReceivedEventArgs();
new Thread(() => comPort_DataReceived(comPort, e));
new Thread(() => comPort_DataReceived(null, null));

You could also pass the arguments sender and eventargs:

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