简体   繁体   中英

How can I wait on a thread to finish before windows logoff

I have written an WPF application which does some processing on the background. When the application closes, I want to wait until the processing is finished before the process stops. This code runs perfectly when I close the application by closing the form. But when I leave the application open end log off out of my windows session or reboot the machine the application seems to terminate on the SemaphoreSlim.Wait(). The strange thing is no errors are logged in the event log. I tried capturing the error with try catch, but the error is still uncaught.

Here is my code:

  public partial class NotifyWindow : Window
  {
    StreamWriter _stream;

    public NotifyWindow()
    {
      InitializeComponent();

      _stream = File.AppendText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"));

      WriteLine("startup");
    }

    private void WriteLine(string line)
    {
      var text = string.Format("{0}    {1}", DateTime.Now, line);

      lock (_stream)
      {
        _stream.WriteLine(text);
        _stream.Flush();
      }
    }

    SemaphoreSlim _locker = new SemaphoreSlim(0);

    private void CloseMe()
    {
      WriteLine(string.Format("CloseMe started"));
      Thread.Sleep(1000);

      WriteLine(string.Format("release lock"));
      _locker.Release();

      WriteLine(string.Format("CloseMe finished"));
    }

    protected override void OnClosing(CancelEventArgs e)
    {
      WriteLine(string.Format("OnClosing started"));

      base.OnClosing(e);

      var t = new Thread(CloseMe);
      t.Start();

      WriteLine("Waiting on lock.");
      _locker.Wait();
      WriteLine("Waiting on lock finished.");

      WriteLine(string.Format("OnClosing finished"));
    }
  }

This results in the the following log

When application is closed by closing the window
24-4-2017 13:57:29    startup
24-4-2017 13:57:30    OnClosing started
24-4-2017 13:57:30    Waiting on lock.
24-4-2017 13:57:30    CloseMe started
24-4-2017 13:57:31    release lock
24-4-2017 13:57:31    CloseMe finished
24-4-2017 13:57:31    Waiting on lock finished.
24-4-2017 13:57:31    OnClosing finished

When application is closed by windows logoff
24-4-2017 13:57:43    startup
24-4-2017 13:57:47    OnClosing started
24-4-2017 13:57:47    Waiting on lock.
24-4-2017 13:57:47    CloseMe started

Is there a way I can wait for the code on the thread to finish before closing the application? I tried various ( How to wait for thread to finish with .NET? ) thread wait alternative but all seem to exhibit the same behavior.

I am using .net 4.6.2 and I reproduced the problem on windows 7 and windows 10.

TIA,

Bart Vries

There is an Application.SessionEnding event that occurs when the user ends the Windows session by logging off or shutting down the operating system. You could try to handle this one.

You application cannot really control what the operating system does though. The user may still kill your application before your code has finisihed and there is not much you can do about this I am afraid.

问题是您首先在事件关闭中调用base.close,您应该取消窗体的关闭。在线程作业完成后,您调用this.close,但是这次您将参数exit设置为true(定义参数)

Thank you all for your feedback. Based on your feedback I found a workaround by not using any OS locking mechanisms. This seems to be working.

Here is the code:

public partial class NotifyWindow : Window
{
  StreamWriter _stream;
  volatile int _running = 0;

  public NotifyWindow()
  {
    InitializeComponent();

    _stream = File.AppendText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"));

    WriteLine("startup");
  }

  private void WriteLine(string line)
  {
    var text = string.Format("{0}    {1}", DateTime.Now, line);

    lock (_stream)
    {
      _stream.WriteLine(text);
      _stream.Flush();
    }
  }

  private void CloseMe()
  {
    WriteLine(string.Format("CloseMe started"));
    Thread.Sleep(1000);

    WriteLine(string.Format("release lock"));
    _running = 1;

    WriteLine(string.Format("CloseMe finished"));
  }

  protected override void OnClosing(CancelEventArgs e)
  {
    WriteLine(string.Format("OnClosing started"));

    base.OnClosing(e);

    var t = new Thread(CloseMe);
    t.Start();

    WriteLine("Waiting on lock.");

    while(_running == 0)
    {
      Thread.Sleep(50);
      WriteLine("Waiting for 50ms.");
    }

    WriteLine("Waiting on lock finished.");

    WriteLine(string.Format("OnClosing finished"));
  }
}

Thanks you all for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM