看板 C_Sharp 關於我們 聯絡資訊
我現在有一個winform程式, 我用以下的程式碼,避免重複執行同樣的程式, (若已經有同一個程式在執行了,直接關閉現在執行的這個) 但是,假設我要將原本縮小到工具列的程式, 喚醒到最上面來, 並讓使用者focus上去, 我該怎麼改寫這個程式呢? == //宣告於最外面,是公變數,避免程式重複執行的變數 private System.Threading.Mutex appMutex; // 防止程式重複執行 private void Form1_Load(object sender, EventArgs e) { Application.ApplicationExit += new EventHandler(Application_ApplicationExit); bool createNew; appMutex = new System.Threading.Mutex(true, "APName", out createNew); if (!createNew) { MessageBox.Show("You just execute the same program"); appMutex.Close(); appMutex = null; Close(); return; } else { MessageBox.Show("this is the first time execute"); } } private void Application_ApplicationExit(object sender, EventArgs e) { if (appMutex != null) { appMutex.ReleaseMutex(); appMutex.Close(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.169.231.3
mortleo:ShowWindowAsync、SetForegroundWindow 03/08 21:30
leicheong:其實... 用FileLock是最準確的方法了... 03/08 23:47
leicheong:產生一個檔案... 用exclusive access開啟, 開不到就離開 03/08 23:48
leicheong:那樣... 03/08 23:49