我有一個應用程序都在WPF中完成,使用MvvM/Prism/Unity和Remote作爲數據源。使用DispatcherTimer的空閒時間 - 不工作
我需要一個基本的東西,贏得表單非常簡單,只需在幾分鐘後檢查應用程序是否空閒,如果空閒,則鎖定應用程序並顯示登錄屏幕。
經過谷歌搜索後,我發現一個解決方案使用DllImport,另一個使用純Wpf方法。
我不知道我在實施Wpf方式後(請檢查下面的代碼),它只在我登錄到應用程序後才起作用,如果我打開並點擊簡單的texbox或點擊搜索,空閒方法沒有被解僱,看起來像是在後臺掛起了一些東西,使得Wpf空閒例程認爲它在做不好的事情。
我該如何檢查所有與可能app相關的服務/方法/等等? callstack對我來說並沒有多大的好處。我擔心,或者我沒有以正確的方式調用遠程服務,或者我在PropChanged事件/ observablecollections/prop等道具上執行了錯誤...
是否有更好的方式來使用100%Wpf結構?
private void CheckIdleTime()
{
handler = delegate
{
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += delegate
{
if (timer != null)
{
timer.Stop();
timer = null;
System.Windows.Interop.ComponentDispatcher.ThreadIdle -= handler;
Console.WriteLine("IDLE! Lets logoff!");
this.LockApplication();
Console.WriteLine("logoff fired");
System.Windows.Interop.ComponentDispatcher.ThreadIdle += handler;
}
};
timer.Start();
Dispatcher.CurrentDispatcher.Hooks.OperationPosted += delegate
{
if (timer != null)
{
timer.Stop();
timer = null;
}
};
};
ComponentDispatcher.ThreadIdle += handler;
}
感謝揚,我的主要問題是,一些在我的應用程序,我不消耗/以正確的方式釋放,使得一些對象停留在內存中,所以當我用正確的方法檢查背景中是否有東西在運行當它們應該是FALSE時,它們返回TRUE。我使用了傳統的ImportDll例程,並且工作正常。乾杯 – 2Fast4YouBR 2012-01-16 19:12:09