C# 禁止應(yīng)用程序多次啟動(dòng)的實(shí)例
通常我們的情況是,雙擊一個(gè)exe文件,就運(yùn)行一個(gè)程序的實(shí)體,再雙擊一次這個(gè)exe文件,又運(yùn)行這個(gè)應(yīng)用程序的另一個(gè)實(shí)體。就拿QQ游戲來(lái)說(shuō)吧,一臺(tái)電腦上一般只能運(yùn)行一個(gè)QQ游戲大廳。
那我們的程序也能像QQ游戲那里禁止多次啟動(dòng)嗎,答案是可以的,下面介紹下一個(gè)簡(jiǎn)單的實(shí)現(xiàn)方法,那就是Mutex(互斥)。
Mutex(mutual exclusion,互斥)是.Net Framework中提供跨多個(gè)線程同步訪問(wèn)的一個(gè)類(lèi)。它非常類(lèi)似了Monitor類(lèi),因?yàn)樗麄兌贾挥幸粋€(gè)線程能擁有鎖定。而操作系統(tǒng)能夠識(shí)別有名稱的互斥,我們可以給互斥一個(gè)唯一的名稱,在程序啟動(dòng)之前加一個(gè)這樣的互斥。這樣每次程序啟動(dòng)之前,都會(huì)檢查這個(gè)命名的互斥是否存在。如果存在,應(yīng)用程序就退出。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool createdNew;
//系統(tǒng)能夠識(shí)別有名稱的互斥,因此可以使用它禁止應(yīng)用程序啟動(dòng)兩次
//第二個(gè)參數(shù)可以設(shè)置為產(chǎn)品的名稱:Application.ProductName
//每次啟動(dòng)應(yīng)用程序,都會(huì)驗(yàn)證名稱為SingletonWinAppMutex的互斥是否存在
Mutex mutex = new Mutex(false, "SingletonWinAppMutex", out createdNew);
//如果已運(yùn)行,則在前端顯示
//createdNew == false,說(shuō)明程序已運(yùn)行
if (!createdNew)
{
Process instance = GetExistProcess();
if (instance != null)
{
SetForegroud(instance);
Application.Exit();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
/// <summary>
/// 查看程序是否已經(jīng)運(yùn)行
/// </summary>
/// <returns></returns>
private static Process GetExistProcess()
{
Process currentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if ((process.Id != currentProcess.Id) &&
(Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
{
return process;
}
}
return null;
}
/// <summary>
/// 使程序前端顯示
/// </summary>
/// <param name="instance"></param>
private static void SetForegroud(Process instance)
{
IntPtr mainFormHandle = instance.MainWindowHandle;
if (mainFormHandle != IntPtr.Zero)
{
ShowWindowAsync(mainFormHandle, 1);
SetForegroundWindow(mainFormHandle);
}
}
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
}
相關(guān)文章
C# Dynamic之:ExpandoObject,DynamicObject,DynamicMetaOb的應(yīng)用(上)
本篇文章對(duì)C#中ExpandoObject,DynamicObject,DynamicMetaOb的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析
這篇文章主要介紹了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,以實(shí)例形式較為詳細(xì)的講述了.NET Framework里面提供的三種Timer具體用法,需要的朋友可以參考下2014-10-10C#將制定目錄文件名轉(zhuǎn)換成大寫(xiě)的方法
這篇文章主要介紹了C#將制定目錄文件名轉(zhuǎn)換成大寫(xiě)的方法,涉及C#操作文件及字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04漢字轉(zhuǎn)拼音縮寫(xiě)示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫(xiě)示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法,可實(shí)現(xiàn)系統(tǒng)服務(wù)的啟動(dòng)和停止功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04