Moving delegate-related function to a different thread
- by Chris
Hello everybody. 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.
Thanks for your time!
Chris