狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C# WinForms 開(kāi)發(fā)中防止同一應(yīng)用運(yùn)行多個(gè)實(shí)例

admin
2024年7月23日 15:40 本文熱度 1124

在開(kāi)發(fā) WinForms 應(yīng)用程序時(shí),有時(shí)需要防止同一個(gè)應(yīng)用程序的多個(gè)實(shí)例同時(shí)運(yùn)行。這種需求在某些情況下非常重要,例如,當(dāng)你需要確保某個(gè)資源(如文件或數(shù)據(jù)庫(kù))只被一個(gè)應(yīng)用實(shí)例訪問(wèn)時(shí)。

本文將介紹幾種防止同一應(yīng)用運(yùn)行多個(gè)實(shí)例的方法,提供詳細(xì)的代碼示例,并輸出為 Markdown 格式。

方法一:使用 Mutex 類(lèi)

Mutex(互斥量)是一個(gè)同步基元,它可以用于跨線程和進(jìn)程同步。通過(guò)創(chuàng)建一個(gè)命名互斥量,可以防止應(yīng)用運(yùn)行多個(gè)實(shí)例。

示例代碼

namespace SingleInstanceApp{    internal static class Program    {        private static Mutex mutex = null;        /// <summary>        ///  The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            const string mutexName = "MyApp";            bool isOwned;
           mutex = new Mutex(true, mutexName, out isOwned);
           if (!isOwned)            {                MessageBox.Show("應(yīng)用程序已經(jīng)在運(yùn)行中。", "多實(shí)例檢測(cè)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                return;            }            // To customize application configuration such as set high DPI settings or default font,            // see https://aka.ms/applicationconfiguration.            ApplicationConfiguration.Initialize();            Application.Run(new Form1());
           GC.KeepAlive(mutex);        }    }}

在上述代碼中,我們使用 Mutex 類(lèi)創(chuàng)建了一個(gè)系統(tǒng)全局命名的互斥體 mutexName。如果應(yīng)用程序已經(jīng)在運(yùn)行,則 isOwned 將為 false,應(yīng)用會(huì)顯示一條消息并退出。

方法二:使用 Process 類(lèi)

通過(guò) Process 類(lèi)檢查當(dāng)前是否已經(jīng)有同名進(jìn)程在運(yùn)行,也可以防止多個(gè)實(shí)例的運(yùn)行。

示例代碼

using System.Diagnostics;
namespace SingleInstanceApp{    internal static class Program    {        /// <summary>        ///  The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            if (IsAlreadyRunning())            {                MessageBox.Show("應(yīng)用程序已經(jīng)在運(yùn)行中。", "多實(shí)例檢測(cè)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                return;            }            // To customize application configuration such as set high DPI settings or default font,            // see https://aka.ms/applicationconfiguration.            ApplicationConfiguration.Initialize();            Application.Run(new Form1());        }
       static bool IsAlreadyRunning()        {            string currentProcessName = Process.GetCurrentProcess().ProcessName;            Process[] processes = Process.GetProcessesByName(currentProcessName);            return processes.Length > 1;        }    }}

此方法通過(guò) Process.GetProcessesByName 方法獲取當(dāng)前運(yùn)行的同名進(jìn)程。如果長(zhǎng)度大于1,說(shuō)明此時(shí)已有另一個(gè)實(shí)例在運(yùn)行。

方法三:使用 Windows API

還有一種方法是利用 Windows API 創(chuàng)建一個(gè)命名事件,檢查該事件是否已經(jīng)存在。

示例代碼

using System.Diagnostics;using System.Runtime.InteropServices;
namespace SingleInstanceApp{    internal static class Program    {        const string UniqueEventName = "Global\\MyApp";
       [DllImport("kernel32", SetLastError = true)]        static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
       [DllImport("kernel32.dll")]        static extern uint GetLastError();        /// <summary>        ///  The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            IntPtr handle = CreateEvent(IntPtr.Zero, false, false, UniqueEventName);            if (handle == IntPtr.Zero || GetLastError() == 183) // ERROR_ALREADY_EXISTS (183)            {                MessageBox.Show("應(yīng)用程序已經(jīng)在運(yùn)行中。", "多實(shí)例檢測(cè)", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                return;            }
           // To customize application configuration such as set high DPI settings or default font,            // see https://aka.ms/applicationconfiguration.            ApplicationConfiguration.Initialize();            Application.Run(new Form1());        }    }}

上述代碼使用了 CreateEvent API 創(chuàng)建一個(gè)命名事件,并通過(guò) GetLastError 檢查事件是否已經(jīng)存在(錯(cuò)誤代碼 183 表示該事件已存在)。

CreateEvent 是一個(gè) Windows API 函數(shù),用于創(chuàng)建或打開(kāi)一個(gè)命名的或未命名的事件對(duì)象。事件對(duì)象在進(jìn)程間和線程間同步中非常有用。

GetLastError() 函數(shù)是用于檢索擴(kuò)展的錯(cuò)誤信息的函數(shù)。它通常與其他 Windows API 函數(shù)一起使用,這些函數(shù)不返回明確的錯(cuò)誤代碼,但是如果調(diào)用失敗,可以通過(guò) GetLastError() 獲取詳細(xì)的錯(cuò)誤信息。

總結(jié)

以上介紹了三種在 WinForms 開(kāi)發(fā)中防止同一應(yīng)用運(yùn)行多個(gè)實(shí)例的方法:

  1. 使用 Mutex 類(lèi)。

  2. 使用 Process 類(lèi)。

  3. 使用 Windows API。

每種方法都有其優(yōu)點(diǎn)和適用場(chǎng)景,開(kāi)發(fā)者可根據(jù)具體需求選擇合適的方法來(lái)實(shí)現(xiàn)多實(shí)例檢測(cè)功能。希望此文對(duì)你有所幫助,歡迎提出任何問(wèn)題或建議。


該文章在 2024/7/23 22:28:12 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車(chē)隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類(lèi)企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷(xiāo)售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved