How can I find whether the current thread is exited or not?  
Author Message
Prabu.





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

Is there any property or method is available for checking whether the current threading is exited or not. If so can you place provide me the code snippet and some of the useful tutorial & samples links for learning about this concept.




Visual C#6  
 
 
Sibusiso





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

Maybe this would help :- http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=564684&SiteID=1

 
 
Prabu.





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

I am not dealing with multiple processes. I am dealing with a multiple functions in same application. Here is my usage scenario.

  • I have been creating a project that checks whether all the projects in given directory gets build successfully or not. The recursion function will be evaluated unto its leaf folder of the given path. While running all the processes, I need to give the option to stop the testing process. That’s why I am using thread concept for this purpose.
  • I need if all the processes are getting successful then****should go to the main method and the process will continue from main method itself. But after exiting the thread, the focus does not go to the mail method. Please help.

Thanks in advance.



 
 
Evan Mulawski





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

if (threadMain.ThreadState != ThreadState.Running) //Not running

{

//Next step

}

OR

if (!threadMain.IsAlive) //Not alive

{

//Next step

}

Does this help



 
 
Sibusiso





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

Hi I think this is what you are looking for :-


using System;

using System.Threading;

public class ThreadWork

{
public static void DoWork()
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Working thread...");
Thread.Sleep(2000);
}
}
}

class ThreadTest

{
public static void Main()
{
ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
Thread myThread = new Thread(myThreadDelegate);
myThread.Start();
for (int i = 0; i < 3; i++)
{
Console.WriteLine("In main.");
Thread.Sleep(2000);
}
}
}

Hope this helps



 
 
Peter Ritchie





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

If one thread depends on the completion of one or more other threads, the thread join pattern is meant to handle this:

Thread thread1 = new Thread(new ThreadStart(someMethod));
Thread thread2 = new Thread(new ThreadStart(someOtherMethod));
//...
thread1.Start();
thread2.Start();
// have these two threads "join" the current thread:
// this ensures the current thread blocks until these two threads have completed.
thread1.Join();
thread2.Join();

Now, I don't recommend you do this on a GUI thread as you GUI will be non-responsive until those two threads are completed (a bad thing). In that case I recommend you use the BackgroundWorker class for your background work and subscribe to the RunWorkerCompleted event--otherwise you'll have to implement your own form of executing a delegate on the GUI thread.

 
 
Peter Ritchie





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

if (threadMain.ThreadState != ThreadState.Running) //Not running

{

//Next step

}

If a running thread has called a Wait method (WaitOne,WaitAny,WaitAll, etc.) or called Sleep, its state will not be ThreadState.Running, it will be ThreadState.WaitSleepJoin.

 
 
James Curran





PostPosted: Visual C# General, How can I find whether the current thread is exited or not? Top

Is there any property or method is available for checking whether the current threading is exited or not.

Asking if the "current thread" is exited, is very much like asking "Am I dead " If you can ask the question, you are alive.