如何在不活動'n'秒後關閉WPF應用程序?n秒鐘不活動後關閉WPF應用程序
2
A
回答
14
有點晚了,但我想出了這個代碼,它將重新啓動一個計時器上的任何輸入事件:
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;
}
}
1
1
在msdn social有關於此事的討論。檢查它,請張貼的內容爲你所做的工作....
我粘貼從討論的代碼(一個我認爲它會做你的需要):
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;
}
}
+1
ComponentDispatcher.ThreadIdle正在被正確觸發,但Dispatcher.CurrentDispatcher.Hooks.OperationPosted正在抽取事件,即使我最小化實質上停止計時器的Window。 – Raj 2010-03-02 13:27:50
0
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);
};
}
+0
如果要在某些預處理後啓用自動註銷事件,請將此代碼行保留在主窗口構造函數或表單加載事件中。 – sam 2014-08-26 22:52:19
相關問題
- 1. 應用程序運行一秒鐘,然後立即關閉
- 2. 幾秒鐘後在Windows Mobile應用程序上關閉MessageBox(C#)
- 3. 蝙蝠文件5分鐘不活動後關閉程序
- 4. 自動關閉WPF應用程序
- 5. 第二秒後關閉活動
- 6. 幾秒鐘後應用程序崩潰
- 7. 幾秒鐘後應用程序崩潰
- 8. WPF dialogBox不關閉應用程序
- 9. Application.Current.Shutdown(-1)不關閉WPF應用程序
- 10. iPhone應用程序應該去背景,然後關閉後幾分鐘的不活動
- 11. pynsq:關閉'n'秒
- 12. ACM1602B-FL-YBW在幾秒鐘後關閉
- 13. 內置EXE幾秒鐘後關閉
- 14. WPF應用程序在一段時間後自動關閉
- 15. Google玻璃關閉二級活動關閉應用程序
- 16. 爲什麼按下活動按鈕後應用程序關閉?
- 17. 如何關閉所有後臺活動應用程序?
- 18. 屏幕關閉後保持應用程序活動
- 19. 超時後關閉掃描器應用程序/活動
- 20. 應用程序關閉後,返回到相同的活動
- 21. n秒後關閉組合框
- 22. 。用java啓動的.msi文件在幾秒鐘後關閉。
- 23. NSURLErrorDomain代碼= -1004幾秒鐘後,應用程序啓動
- 24. UWP應用程序在啓動後幾秒鐘內崩潰
- 25. 當1分鐘後關閉RDP(X鍵),它會關閉所有應用程序
- 26. 大約40分鐘不活動後關閉SingleConnectionDataSource連接
- 27. 如何在關閉wpf應用程序時關閉所有後臺進程
- 28. 保持WPF應用程序活着關閉
- 29. VBA MSGBOX關閉在不到一秒鐘
- 30. Android應用程序在關閉主要活動時不會關閉
最近我發現了一個問題:當文本框聚焦並且鼠標懸浮在窗口上時,PreProcessInput事件不斷髮射。當您將鼠標移動到窗口外或將焦點從文本框移開時,它可以正常工作。這是我做錯了什麼嗎? – PVitt 2010-07-29 09:22:16
僅供參考:我試過你的解決方案,並且無法準確理解何時引發'PreProcessInput'事件。你可以在這裏找到我的問題:http://stackoverflow.com/questions/4963135/wpf-inactivity-and-activity – 2011-02-11 13:14:35
@Martin,我不知道,WPF對象模型太複雜了。我認爲你必須通過PreProcessInputEventArgs對象和轉儲信息來了解它來自哪裏。然後你也可以過濾它。 – 2011-02-11 13:28:37