Thread is not terminating even after Thread.Abort operation...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • masaniparesh
    New Member
    • Apr 2008
    • 23

    #1

    Thread is not terminating even after Thread.Abort operation...

    Hi,

    In my C# program i am termination thread by thread.Abort when times out occur. But i figured out that even after thread.Abort operation the thread is being alive for the random time. I have given the code snippet for this below.
    Code:
    private void Delay() 
    {
     //Thread waiting for the output stream of the process
     DateTime expireTime = DateTime.Now.AddSeconds(30);
     Thread outputThread = new Thread(new ThreadStart(ReadOutput));
     isApplicationTimedOut = false;
     outputThread.Start();
     while (outputThread.IsAlive) 
     { 
         if(DateTime.Compare(expireTime,  DateTime.Now)<0) 
         {
                 outputThread.Abort();
                 isApplicationTimedOut = true;
         }
         Application.DoEvents();
     }
    }
    Is it possible to make thread to stop say after 30 seconds in any case. My basic requirement is thread should get terminated at the 30th Second. It should not run for 31st second.

    Could anyone please tell me how can i terminate thread for sure.

    Thanks
    Last edited by masaniparesh; Apr 18 '08, 03:01 PM. Reason: misspelled statement
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What does your ReadOutput function look like? Thread.Abort just throws an exception in the thread function, and it is possible to ignore it on accident.
    I would recomend the timing check be done IN the thread and not outside of it.
    You are rather defeating the purpose of a thread by sitting in a loop waiting for it to end.

    Comment

    Working...