multithreading - C# keep event handling thread alive in CPU friendly way -


i run 10 threads in parallel. each thread contains code handles serial port communication (using 'serialport' class). of features are:

  1. code handling event raised when rs232 device returns data.
  2. code handling timer events raised when rs232 device not return data within predefined time frame.

as can see each thread handles asynchronous events initialized , started thread itself. thread needs keep alive until events have been raised , processed. based on received data rs232 device thread knows when work done , thread can kill itself.

now question: avoid using infinite loop keep thread alive avoid using lot of cpu resources on nothing. idea how , avoiding thread blocks/stops itself?.

the efficient way keep 10 threads idle based on condition use waithandle.

the manualresetevent class allows signal when want continue execution. can signal multiple threads same handle.

class work {     public static void workmethod(object stateinfo)     {         console.writeline("work starting.");         ((manualresetevent)stateinfo).waitone();         console.writeline("work ending.");     } }      // example:     manualresetevent manualevent = new manualresetevent(false);     thread newthread = new thread(work.workmethod);     newthread.start(manualevent);      // terminate threads waiting on handle:     manualevent.set(); 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -