首页 后端开发 C#.Net教程 C#只允许启动一个WinFrom进程的两种方法

C#只允许启动一个WinFrom进程的两种方法

Dec 21, 2016 pm 02:43 PM

方法一:只禁止多个进程运行

view plaincopy to clipboardPRint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
[STAThread]   
public static void Main()   
{   
    bool ret;   
    System.Threading.Mutex mutex = new System.Threading.Mutex(true, application.ProductName, out ret);   
    if (ret)   
    {   
        System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格      
        System.Windows.Forms.Application.DoEvents();   
        System.Windows.Forms.Application.Run(new Main());   
        //   Main   为你程序的主窗体,如果是控制台程序不用这句      
        mutex.ReleaseMutex();   
    }   
    else  
    {   
        MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);   
        //   提示信息,可以删除。      
        Application.Exit();//退出程序      
    }   
}  
[STAThread]
public static void Main()
{
    bool ret;
    System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
    if (ret)
    {
        System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
        System.Windows.Forms.Application.DoEvents();
        System.Windows.Forms.Application.Run(new Main());
        //   Main   为你程序的主窗体,如果是控制台程序不用这句   
        mutex.ReleaseMutex();
    }
    else
    {
        MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        //   提示信息,可以删除。   
        Application.Exit();//退出程序   
    }
}


方法二:禁止多个进程运行,并当重复运行时激活以前的进程 

view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
[STAThread]   
public static void Main()   
{   
    //Get   the   running   instance.      
    Process instance = RunningInstance();   
    if (instance == null)   
    {   
        System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格      
        System.Windows.Forms.Application.DoEvents();   
        //There   isn't   another   instance,   show   our   form.      
        Application.Run(new Main());   
    }   
    else  
    {   
        //There   is   another   instance   of   this   process.      
        HandleRunningInstance(instance);   
    }   
}   
public static Process RunningInstance()   
{   
    Process current = Process.GetCurrentProcess();   
    Process[] processes = Process.GetProcessesByName(current.ProcessName);   
    //Loop   through   the   running   processes   in   with   the   same   name      
    foreach (Process process in processes)   
    {   
        //Ignore   the   current   process      
        if (process.Id != current.Id)   
        {   
            //Make   sure   that   the   process   is   running   from   the   exe   file.      
            if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)   
            {   
                //Return   the   other   process   instance.      
                return process;   
            }   
        }   
    }   
    //No   other   instance   was   found,   return   null.    
    return null;   
}   
public static void HandleRunningInstance(Process instance)   
{   
    //Make   sure   the   window   is   not   minimized   or   maximized      
    ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);   
    //Set   the   real   intance   to   foreground   window   
    SetForegroundWindow(instance.MainWindowHandle);   
}   
[DllImport("User32.dll")]   
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);   
[DllImport("User32.dll")]   
private static extern bool SetForegroundWindow(IntPtr hWnd);   
private const int WS_SHOWNORMAL = 1;  
[STAThread]
public static void Main()
{
    //Get   the   running   instance.   
    Process instance = RunningInstance();
    if (instance == null)
    {
        System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
        System.Windows.Forms.Application.DoEvents();
        //There   isn't   another   instance,   show   our   form.   
        Application.Run(new Main());
    }
    else
    {
        //There   is   another   instance   of   this   process.   
        HandleRunningInstance(instance);
    }
}
public static Process RunningInstance()
{
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);
    //Loop   through   the   running   processes   in   with   the   same   name   
    foreach (Process process in processes)
    {
        //Ignore   the   current   process   
        if (process.Id != current.Id)
        {
            //Make   sure   that   the   process   is   running   from   the   exe   file.   
            if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
            {
                //Return   the   other   process   instance.   
                return process;
            }
        }
    }
    //No   other   instance   was   found,   return   null. 
    return null;
}
public static void HandleRunningInstance(Process instance)
{
    //Make   sure   the   window   is   not   minimized   or   maximized   
    ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
    //Set   the   real   intance   to   foreground   window
    SetForegroundWindow(instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1; 

 以上就是C#只允许启动一个WinFrom进程的两种方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用 C# 的活动目录 使用 C# 的活动目录 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

C# 中的随机数生成器 C# 中的随机数生成器 Sep 03, 2024 pm 03:34 PM

C# 随机数生成器指南。在这里,我们讨论随机数生成器的工作原理、伪随机数和安全数的概念。

C# 中的访问修饰符 C# 中的访问修饰符 Sep 03, 2024 pm 03:24 PM

C# 中的访问修饰符指南。我们已经讨论了 C# 中访问修饰符的简介类型以及示例和输出。

C# 数据网格视图 C# 数据网格视图 Sep 03, 2024 pm 03:32 PM

C# 数据网格视图指南。在这里,我们讨论如何从 SQL 数据库或 Excel 文件加载和导出数据网格视图的示例。

C# 序列化 C# 序列化 Sep 03, 2024 pm 03:30 PM

C# 序列化指南。这里我们分别讨论C#序列化对象的介绍、步骤、工作原理和示例。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在这里,我们讨论 C# 中模式的介绍和前 3 种类型,以及其示例和代码实现。

C# 中的质数 C# 中的质数 Sep 03, 2024 pm 03:35 PM

C# 素数指南。这里我们讨论c#中素数的介绍和示例以及代码实现。

C# 中的阶乘 C# 中的阶乘 Sep 03, 2024 pm 03:34 PM

C# 阶乘指南。这里我们讨论 C# 中阶乘的介绍以及不同的示例和代码实现。

See all articles