0

I have a console app that sometimes does NOT exit when I issue the following 2 lines:

        File.AppendAllText(@"\\bbd\FLog", "Halting: " + DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + "\n");
        System.Environment.Exit(0);

The halting message IS written to disk, but the application is still alive in taskmanager.

The application does use a threading timer:

GetRecordsTimer = new System.Threading.Timer(new TimerCallback(GetRecordsCallBack), null, 60000, 0);

That is declared as:

public static System.Threading.Timer GetRecordsTimer;

BUT my understanding of Environment.Exit(0) is that is kills everything (all threads) and then exits.

Note: not only is the application still alive, but my log file FLog appears to be in use...

Could it be that AppendAllText is somehow not finished if I exit immediately afterwards?

ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • Environment.Exit does not terminate threads, it just terminates your application. – Tony Abrams Jun 12 '19 at 14:51
  • 1
    Please create a [mre]. – CodeCaster Jun 12 '19 at 14:52
  • @TonyAbrams If some threads are running, the application is not terminated, is it? – GSerg Jun 12 '19 at 14:52
  • @TonyAbrams Terminating a process results in the termination of every thread in that process. If those threads cannot be terminated, the process cannot be, either. –  Jun 12 '19 at 14:53
  • 2
    Its a [known problem](https://stackoverflow.com/questions/18036863/why-does-environment-exit-not-terminate-the-program-any-more) in Windows that came back a few times. But not so likely in a console mode app, next likely cause is crummy anti-malware with a "deep scan" feature. – Hans Passant Jun 12 '19 at 15:54

1 Answers1

0

My thought based on what you are describing is that the network write operation to \\bbd is not completing.

If you want to log and exit, but exit is more important than logging, perhaps try something like this:

ThreadPool.QueueUserWorkItem(state => {
    Thread.Sleep(2000);
    Environment.Exit(0);
});
File.AppendAllText(@"\\bbd\FLog", "Halting: " + DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + "\n");

This way, your process will exit in 2 seconds no matter what; even if your log operation never completes.

A more elegant solution might quit as soon as the log operation completes OR after a timeout, but my code is just for an example

Tim
  • 5,940
  • 1
  • 12
  • 18
  • I like that. I understand it might be better to use System.Diagnostics.Process.GetCurrentProcess().Kill(), instead of Environment.Exit() if not on main thread? Do you agree? – ManInMoon Jun 12 '19 at 15:08
  • Environment.Exit() documentation states: "Exit terminates an application immediately, even if other threads are running." `Process.Kill` will probably do the same thing, but it is intended to kill *a* process, whereas `Environment.Exit()` is intended to kill the *current process*. I would favor `Environment.Exit()`, less moving parts. – Tim Jun 12 '19 at 15:23
  • Understood. Could the fact I am immediately exiting after the AppendAllText be somehow causing the file not to be released? – ManInMoon Jun 12 '19 at 15:25