locked
C# Thread class, SerialPort class RRS feed

  • Question

  • I am creating a thread in my component to execute a piece of code(method) which sends/ receives data/ command to/ from serial port( I am using SerialPort class).

     

    I start the thread using Start() method. Is it compulsory to end the thread programmatically?

    Thursday, July 5, 2007 6:41 AM

Answers

  • It depends on what the thread is doing. 

     

    For my worker threads, I usually put them into an endless loop, having them check a boolean flag at each iteration (ie.. bool _stop)..

     

    When i want them to stop (ie.. shutting down, etc), I set the stop flag to true and do a Join() on their thread.

     

    This lets me cleanly stop my worker threads, without resorting to kills, aborts, etc.

     

    As a general rule, you should stop all your threads when stopping your application.

    Thursday, July 5, 2007 7:35 AM