tags:

views:

556

answers:

4

Hi,

I need to return an exit code of 1 after my Main functions ends. However I have an other thread that never ends (while(true)). So I managed to call Environment.Exit(1). But I got some exception when diposing com objects...

For several reasons I can't change the other thread code. What do you guys advocate to do ?

May I catch the exception coming from the com object disposing ? Do I have an other option for returning an exit code ?

Thx

A: 

Try using:

Environment.ExitCode = 1;

However; if the other thread is not a background thread, you will need it to exit in order to end the process (the exit code is meaningless until the process exits).

Marc Gravell
I already tried this but seems that the uncatched exception (occuring during the environment.exit) erases this value.
+1  A: 

I suggest you to: attach to ProcessExit event to do any last chance cleanup or... do a Thread.Abort on the thread to allow it to finish before exit.

AppDomain.CurrentDomain.ProcessExit +=
delegate(object sender, EventArgs e)
{
 Console.WriteLine("Process Exit");
};
Thread t1 = new Thread(new ThreadStart(delegate
{
try
{
 while (true)
 {
  Console.WriteLine("test 1");
  Thread.Sleep(500);
 }
}
finally
{
 Console.WriteLine("Terminating t1");
}
}));

Thread t2 = new Thread(new ThreadStart(delegate
{
try
{
 while (true)
 {
  Console.WriteLine("test 2");
  Thread.Sleep(500);
 }
}
finally
{
 Console.WriteLine("Terminating t2");
}
}));

t1.Start();
t2.Start();
Thread.Sleep(2000);
t2.Abort();
t2.Join();
Environment.Exit(1);
jmservera
Thread.Abort is widely regarded as bad practise- http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation. Also the question states that they can't change the other thread code.
RichardOD
@RicardOD: It is precisely because the worker thread can not be changed that you have to use Thread.Abort an not the volatile bool flag technique suggested in the article you link to. Also using Thread.Abort in a process shutdown is probably not so bad especially if the worker thread is catching the ThreadAbortException outside the while(true) loop.
Martin Brown
A: 

In fact, to be more precise, I got the folowing on my output:

  • first,I got the following error (on my main thread) catched during the disposal of my com object : "System.ArgumentNullException: Value cannot be null. Parameter name : obj at System.GC.SuppressFinalize(Object obj) at ...."
  • then I got the call of the AppDOmain.CurrentDomain.ProcessExit callback.
  • and finally this unctached error: Unhandled Exception: System.ArgumentNullException: Value cannot be nul. Parameter namer: obj at System.GC.SuppressFinalize(Object obj) at ....

This is the second error that set my exit code not to 0. I don't know if this error occured on my main thread or not. Do i have a way to catch all the uncatched exception coming from others thread ? Or a way to prevent my exit code not being 0 ?

A: 

you can kill proccess by writting this code Dim myProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess() myProcess.Kill()

or Environment.exit(1) is best method for kill all processes

http://www.zhakkas.com/affiliates/idevaffiliate.php?id=542