471,120 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,120 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. private void Delay() 
  2. {
  3.  //Thread waiting for the output stream of the process
  4.  DateTime expireTime = DateTime.Now.AddSeconds(30);
  5.  Thread outputThread = new Thread(new ThreadStart(ReadOutput));
  6.  isApplicationTimedOut = false;
  7.  outputThread.Start();
  8.  while (outputThread.IsAlive) 
  9.  { 
  10.      if(DateTime.Compare(expireTime,  DateTime.Now)<0) 
  11.      {
  12.              outputThread.Abort();
  13.              isApplicationTimedOut = true;
  14.      }
  15.      Application.DoEvents();
  16.  }
  17. }
  18.  
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
Apr 18 '08 #1
1 1175
Plater
7,872 Expert 4TB
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.
Apr 21 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Jeff | last post: by
31 posts views Thread by AlexeiOst | last post: by
2 posts views Thread by Charles A. Lackman | last post: by
20 posts views Thread by Doug Thews | last post: by
10 posts views Thread by Fernando Rodríguez | last post: by
7 posts views Thread by Doug Thews | last post: by
18 posts views Thread by Urs Vogel | last post: by
6 posts views Thread by mehdi | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.