简体   繁体   中英

Shutdown WPF application after n seconds of inactivity

如何在'n'秒不活动后关闭WPF应用程序?

A bit late, but I came up with this code, it restarts a timer on any input event:

  public partial class Window1 : Window {
    DispatcherTimer mIdle;
    private const long cIdleSeconds = 3;
    public Window1() {
      InitializeComponent();
      InputManager.Current.PreProcessInput += Idle_PreProcessInput;
      mIdle = new DispatcherTimer();
      mIdle.Interval = new TimeSpan(cIdleSeconds * 1000 * 10000);
      mIdle.IsEnabled = true;
      mIdle.Tick += Idle_Tick;
    }

    void Idle_Tick(object sender, EventArgs e) {
      this.Close();
    }

    void Idle_PreProcessInput(object sender, PreProcessInputEventArgs e) {
      mIdle.IsEnabled = false;
      mIdle.IsEnabled = true;
    }
  }

You'll need to define "activity", but basically you want to start a timer. Then every time there is some "activity" (whether it's mouse clicks or mouse moves etc.) the timer is reset.

Then in the timer when it reaches your limit just post an event to call the application close method.

There was a discussion in msdn social about this matter. Check it and please post what did work for you....

I paste you the code from the discussion (the one I think it will do what you need):

public partial class Window1 : Window
{
    private EventHandler handler;
    public Window1()
    {
        InitializeComponent();

        handler = delegate
        {
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(4);
            timer.Tick += delegate
            {
                if (timer != null)
                {
                    timer.Stop();
                    timer = null;
                    System.Windows.Interop.ComponentDispatcher.ThreadIdle -= handler;
                    MessageBox.Show("You get caught!");
                    System.Windows.Interop.ComponentDispatcher.ThreadIdle += handler;
                }

            };

            timer.Start();

            //System.Windows.Interop.ComponentDispatcher.ThreadIdle -= handler;
            Dispatcher.CurrentDispatcher.Hooks.OperationPosted += delegate
            {
                if (timer != null)
                {
                    timer.Stop();
                    timer = null;
                }
            };
        };

        ComponentDispatcher.ThreadIdle += handler;
    }
}
public MainWindow()
    {
        InitializeComponent();
        var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(10)};
        timer.Tick += delegate
        {
            timer.Stop();
            MessageBox.Show("Logoff trigger");
            timer.Start();
        };
        timer.Start();
        InputManager.Current.PostProcessInput += delegate(object s,  ProcessInputEventArgs r)
        {
            if (r.StagingItem.Input is MouseButtonEventArgs || r.StagingItem.Input is KeyEventArgs)
                timer.Interval = TimeSpan.FromSeconds(10);
        };
    }

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