首頁  >  文章  >  後端開發  >  C#只允許啟動一個WinFrom進程的兩種方法

C#只允許啟動一個WinFrom進程的兩種方法

黄舟
黄舟原創
2016-12-21 14:43:441753瀏覽

方法一:只禁止多個進程運行

view plaincopy to clipboardPRint?
·········10········20········30····· ····40········50·······60·······70·······80········90 ·······130·······140·······150
[STAThread]   
public static void Main()   
{   
    bool ret;   
    System.Threading.Mu  
    System.Threading.Mu  
    if (ret)   
    {   
        System .Windows.Forms.Application.EnableVisualStyles();   //這兩行實作   XP   視覺風格      
       System.Windows.Forms.Application.Run(new Main()) ;   
        //   Main   為你程序中的主窗體,而如果是控制台程序不使用這句       );  
    else  
    {   
        MessageBox.Show(null, "有一個相同相同的應用程式和本程式的應用程式已經在運行,請不要同時執行多個本程式。
        Application.Exit();//退出程序      
    }   
}  oid;    System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application .ProductName, out ret);
    if (ret)
    {
        System.Windows 
        System.Windows.Forms.Application.DoEvents();
        System.Windows.Forms.Application.Run(new Main());
        //    tex.ReleaseMutex();
    }
    else
    {
        MessageBox.Show(null, "有一個和本程式相同的應用程式已經在運行,請不要同時執行多個本程式。nn這個程式即將退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning) ;
        //   提示訊息,可刪除。
        Application.Exit();//退出程式   
    }
}


方法二:禁止多個行程、並當重複執行時啟用程式的行程···10········20········30········40·······50········60· ·······70········80········90·······100·······110········ 120·······130·······140······150
[STAThread]   
public static void Main()       
    Process instance = RunningInstance();   
    if (instance == null)   
    {   
        視覺風格      
        System.Windows.Forms.Application.DoEvents();   
        //沒有   另一個        
        Application.Run(new  //還有   這個過程的另一個   實例   。
        HandleRunningInstance(實例);   
    }   
}   CurrentProcess();   
    Process[] 流程= Process.GetProcessesByName(current.ProcessName);   
    //循環遍歷   運行的   進程   具有相同   名稱      
    foreach(進程中的進程)   
    {   
        //忽略   當前   進程      
        if (process.Id != current.Id)   
        {   
            //確保   確保   該進程是從exe 文件運行的。      
            if (Assembly.GetExecutingAssembly().Location.Replace("/", "  {   
                //回復   其他   程序  而言則。      
                以退貨流程;   
      
    }   
    //未找到   其他   實例   ,且回傳   null。
    返回null;   
}
公共靜態void handlerunninginstance(process實例)
{
//確保視窗未被最小化或最大化showwindowAsync(instance.mainwindowhandle,Dshowlormal/   實例   為前景   窗口   
    SetForegroundWindow( 。  
private static extern bool SetForegroundWindow( IntPtr hWnd);   
private const int WS_SHOWNORMAL = 1;  
[STAThread]
public static void Main()
{
      
    流程實例 = RunningInstance();
    if (instance == null)
    {
        System.Windows.Forms.Application.EnableVisualStyles();   ////1 實作   XP ();
        //沒有   另一個   實例,並顯示   我們的   表單。   
        Application.Run(new Main());
    }
    else🎠    實例   。
        HandleRunningInstance(instance);
    }
}
public static Process RunningInstance()
{Š); Process.GetProcessesByName(current.ProcessName);
    //循環   透過   來運作有相同   名稱   
    foreach(進程中的流程)
    {
        //  current.Id)
        {
            ///確認   進程   從  
            if (Assembly.GetExecutingAssembly().Location.Replace("/", "\") ==            //返回        }
        }
    }
    / /沒有找到   其他   實例   ,並回傳   null。
    return null;
}
public static void HandleRunningInstance(Process; instance)
{
    //確保    WindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
    //設定   真實   實例   到前景   視窗
SetForegroundWindow( instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("3.Showll); IntPtr hWnd);
private const int WS_SHOWNORMAL = 1; 




 以上就是C#只允許啟動一個WinFrom進程的兩種方法的內容,更多相關內容請關注PHPP網絡(www.php.)!




陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn