[英]How to determine if windows restarted?
我希望我的 WPF 應用程序只運行一次。 我對此沒有任何問題。
我的問題是如何確定 windows 當前是否重新啟動?
您可以將文件寫入磁盤,然后立即使用 MoveFileEx 將其標記為“重新啟動時刪除”:
所以在偽代碼中:
if(File.Exists(CheckFileName))
return false; // already ran once
else {
// write out the check file
using(checkFile = File.Create(CheckFileName, ...)) {
// and mark it as delete on boot
MoveFileEx(checkFile.SafeHandle,
null,
MOVEFILE_DELAY_UNTIL_REBOOT);
return true; // ok to run
}
}
您可以檢查並存儲系統正常運行時間以及上次運行時間,並將其與當前正常運行時間進行比較。
一些偽代碼:
DateTime computerLastStarted = Now - Uptime;
if (computerLastStarted > storedComputerLastStarted + or - tollerance) {
storedComputerLastStarted = computerLastStarted;
StartProgram();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.