c# - Moving delegate-related function to a different thread -
we developing library in c# communicates serial port. have function given delegate. problem want run in different thread.
we tried creating new thread (called datafrombot) keep using follows (first line):
comport.datareceived += new serialdatareceivedeventhandler(comport_datareceived); datafrombot = new thread(comport_datareceived); datafrombot.start();
comport_datareceived defined as:
thread datafrombot; public void comport_datareceived(object sender, serialdatareceivedeventargs e) { ... }
the following errors occur:
error 3 best overloaded method match 'system.threading.thread.thread(system.threading.threadstart)' has invalid arguments c:...\ir52clow\communicationmanager.cs 180 27 ir52clow
error 4 argument '1': cannot convert 'method group' 'system.threading.threadstart' c:...\ir52clow\communicationmanager.cs 180 38 ir52clow
any ideas of how should convert compile? please note comport.datareceived (pay attention "." instead of "_") lies within system library , cannot modified.
thanks time! chris
every delegate has method called begininvoke(). when called method invokes function on new thread. dont have explicitly. eg.
public delegate string delegate1 ( int x);
delegate1 d1 = new delegate1(add);
d1.begininvoke(10, null, null);
here add method runs on secondary thread. primary thread continue operation , not wait secondary thread end. primary thread not come know when secondary thread has ended.
if need notification when method complete, can asynccallback class.
d1.begininvoke(10, asynccallback(addcomplete), null);
here when secondary thread completes execution invokes addcomplete() method on thread. primary thread not wait secondary thread.
check msdn.
Comments
Post a Comment