将委托相关的函数移动到不同的线程
我们正在用 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 IR52cLowError 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个
Delegate
都有一个名为BeginInvoke()
的方法。调用此方法时,会在新线程上调用该函数。所以你不必明确地这样做。例如,
这里
Add
方法在辅助线程上运行。主线程继续其操作,不等待辅助线程结束。主线程不会知道辅助线程何时结束。如果您需要在方法完成时收到通知,那么您可以使用
AsyncCallback
类。这里,当辅助线程完成执行时,它会在其线程上调用
AddComplete()
方法。主线程不等待辅助线程。
检查 MSDN。
Every
Delegate
has a method calledBeginInvoke()
. When called this method invokes the function on new thread. So you don't have to do it explicitly.For example
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.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.
这是不行的,当 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.
您还可以传递参数 sender 和 eventargs:
You could also pass the arguments sender and eventargs: