Moving delegate-related function to a different thread

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-28T11:26:08Z Indexed on 2010/04/28 11:33 UTC
Read the original article Hit count: 396

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about com