博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# WINFORM判断程序是否运行,且只能运行一个实例
阅读量:7055 次
发布时间:2019-06-28

本文共 1619 字,大约阅读时间需要 5 分钟。

判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种,

方法1:线程互斥

static class Program     {
private static System.Threading.Mutex mutex; /// /// 应用程序的主入口点。 /// [STAThread] static void Main() {
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mutex = new System.Threading.Mutex(true, "OnlyRun"); if (mutex.WaitOne(0, false)) {
Application.Run(new MainForm()); } else {
MessageBox.Show("程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Application.Exit(); } } }

方法2:

这种检测进程的名的方法,并不绝对有效。因为打开第一个实例后,将运行文件改名后,还是可以运行第二个实例。

static class Program     {
/// /// 应用程序的主入口点。 /// [STAThread] static void Main() {
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // get the name of our process string p = System.Diagnostics.Process.GetCurrentProcess().ProcessName; // get the list of all processes by that name System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(p); // if there is more than one process if (processes.Length > 1) {
MessageBox.Show("程序已经在运行中", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Application.Exit(); } else {
Application.Run(new MainForm()); } } }

 

转载于:https://www.cnblogs.com/wifi/articles/2479988.html

你可能感兴趣的文章
Percona XtraDB Cluster 的一些使用限制(PXC 5.7)
查看>>
mysql 源代码目录及安装目录介绍
查看>>
iOS下使用SHA1WithRSA算法加签源码
查看>>
要是喜欢刷题或者练技术,可以看看这些书
查看>>
凌晨的纠结
查看>>
iOS-加载数据的实现-MJRefresh
查看>>
Project Euler Problem 34 Digit factorials
查看>>
POJ NOI MATH-7648 蓄水池水管问题
查看>>
HDU2072 单词数(解法二)
查看>>
js 分页
查看>>
高性能网站架构的思考 (转)
查看>>
圆形背景的TextView
查看>>
从0开始学架构(四)
查看>>
Sets 比赛时想错方向了。。。。 (大数不能处理负数啊)
查看>>
实测java 与php运行速度比较
查看>>
『字典树 trie』
查看>>
『The Captain 最短路建图优化』
查看>>
三元运算符判断分数类型
查看>>
通过QC远程运行QTP脚本,QTP自动崩溃关闭的解决方法
查看>>
HTML资源定位器-URL
查看>>