可能重複:
What is the correct way to create a single instance application?確保只有一個應用程序實例
我有一個WinForms應用程序,它通過下面的代碼啓動一個閃屏:
Hide();
bool done = false;
// Below is a closure which will work with outer variables.
ThreadPool.QueueUserWorkItem(x =>
{
using (var splashForm = new SplashScreen())
{
splashForm.Show();
while (!done)
Application.DoEvents();
splashForm.Close();
}
});
Thread.Sleep(3000);
done = true;
的上面是主窗體的代碼隱藏,並從加載事件處理程序調用。
但是,如何確保一次只加載一個應用程序實例?在主窗體的加載事件處理程序中,我可以檢查進程列表是否在系統上(通過GetProcessesByName(...)),但有沒有更好的方法?
使用.NET 3.5。
你應該叫'Application.Run(splashForm)',而不是'的DoEvents()'循環。 – SLaks