0

I need to ensure that an app is closed just before Midnight.

The code below generally works, but every week or so it stays running.

What might be casuing Environment.Exit(0) NOT to work?

As you can see from FmLog, it does get to write the "Halting" message:

[GetRecordsCallBack] Start: 10-07-2019 23:53:44
[GetRecordsCallBack] Start: 10-07-2019 23:54:44
[GetRecordsCallBack] Start: 10-07-2019 23:55:44
[GetRecordsCallBack] Start: 10-07-2019 23:56:44
[GetRecordsCallBack] Halting: 10-07-2019 23:56:44



    public static void GetRecordsCallBack(object source)
    {
        Console.WriteLine("[GetRecordsCallBack] Start: " + DateTime.UtcNow.ToString("HH:mm:ss"));
        File.AppendAllText(@"\\bb\FmLog", "[GetRecordsCallBack] Start: " + DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + "\n");
        GetRecords();
        if (DateTime.UtcNow.TimeOfDay > new TimeSpan(23, 56, 00))
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                Thread.Sleep(2000);
                Environment.Exit(0);
                //System.Diagnostics.Process.GetCurrentProcess().Kill();
            });
            GetRecordsTimer.Change(Timeout.Infinite, Timeout.Infinite);


            File.AppendAllText(@"\\bb\FmLog", "[GetRecordsCallBack] Halting: " + DateTime.UtcNow.ToString("dd-MM-yyyy HH:mm:ss") + "\n");

            return;
        }

        GetRecordsTimer = new System.Threading.Timer(new TimerCallback(GetRecordsCallBack), null, 60000, 0);
    }
ManInMoon
  • 6,795
  • 15
  • 70
  • 133
  • By which mechanism are you keeping the main thread from exiting? Wouldn't it be better to signal to whatever is blocking this thread? Abruptly pulling the plug on an app rather than a graceful shutdown is always going to be a bit messy, and `Environment.Exit` is a crude hammer blow when it is placed anywere other than the very last line of code that your application executes after everything else has gracefully shutdown. – spender Jul 11 '19 at 12:14
  • https://stackoverflow.com/a/692371/14357 – spender Jul 11 '19 at 12:15
  • I believe I am: GetRecordsTimer.Change(Timeout.Infinite, Timeout.Infinite); – ManInMoon Jul 11 '19 at 12:17
  • 1
    Are you sure that everytime you call this logic your GetRecords completes in less than 4 minutes? – Steve Jul 11 '19 at 12:17
  • Yes, because it gets to write the "Halting" message – ManInMoon Jul 11 '19 at 12:19
  • @ManInMoon That just looks like you're changing a timer's timings. If I create a timer in my `Main()`, I don't expect the application to stay open unless I actively prevent the main thread from quitting. How do you do this? – spender Jul 11 '19 at 12:19
  • Can you set your thread to be background instead of foreground (per spender first comment) https://www.jitendrazaa.com/blog/microsoft/csharp/background-and-foreground-thread-in-c/ – estinamir Jul 11 '19 at 12:21
  • @spender You may be on to something. The app stays open because I open a QuickFix session. I am not specifically closing it - so perhaps that is causing the problem. – ManInMoon Jul 11 '19 at 12:23

0 Answers0