0

0

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

黄舟

黄舟

发布时间:2016-12-21 14:43:44

|

2153人浏览过

|

来源于php中文网

原创

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

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)!

CodeBuddy
CodeBuddy

腾讯云AI代码助手

下载

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
java入门学习合集
java入门学习合集

本专题整合了java入门学习指南、初学者项目实战、入门到精通等等内容,阅读专题下面的文章了解更多详细学习方法。

19

2026.01.29

java配置环境变量教程合集
java配置环境变量教程合集

本专题整合了java配置环境变量设置、步骤、安装jdk、避免冲突等等相关内容,阅读专题下面的文章了解更多详细操作。

15

2026.01.29

java成品学习网站推荐大全
java成品学习网站推荐大全

本专题整合了java成品网站、在线成品网站源码、源码入口等等相关内容,阅读专题下面的文章了解更多详细推荐内容。

17

2026.01.29

Java字符串处理使用教程合集
Java字符串处理使用教程合集

本专题整合了Java字符串截取、处理、使用、实战等等教程内容,阅读专题下面的文章了解详细操作教程。

2

2026.01.29

Java空对象相关教程合集
Java空对象相关教程合集

本专题整合了Java空对象相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.29

clawdbot ai使用教程 保姆级clawdbot部署安装手册
clawdbot ai使用教程 保姆级clawdbot部署安装手册

Clawdbot是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

38

2026.01.29

clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址
clawdbot龙虾机器人官网入口 clawdbot ai官方网站地址

clawdbot龙虾机器人官网入口:https://clawd.bot/,clawdbot ai是一个“有灵魂”的AI助手,可以帮用户清空收件箱、发送电子邮件、管理日历、办理航班值机等等,并且可以接入用户常用的任何聊天APP,所有的操作均可通过WhatsApp、Telegram等平台完成,用户只需通过对话,就能操控设备自动执行各类任务。

22

2026.01.29

Golang 网络安全与加密实战
Golang 网络安全与加密实战

本专题系统讲解 Golang 在网络安全与加密技术中的应用,包括对称加密与非对称加密(AES、RSA)、哈希与数字签名、JWT身份认证、SSL/TLS 安全通信、常见网络攻击防范(如SQL注入、XSS、CSRF)及其防护措施。通过实战案例,帮助学习者掌握 如何使用 Go 语言保障网络通信的安全性,保护用户数据与隐私。

14

2026.01.29

俄罗斯Yandex引擎入口
俄罗斯Yandex引擎入口

2026年俄罗斯Yandex搜索引擎最新入口汇总,涵盖免登录、多语言支持、无广告视频播放及本地化服务等核心功能。阅读专题下面的文章了解更多详细内容。

902

2026.01.28

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
C# 教程
C# 教程

共94课时 | 7.9万人学习

C 教程
C 教程

共75课时 | 4.3万人学习

C++教程
C++教程

共115课时 | 14.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号