winform判断程序是否运行,且只能运行一个实例
前言
判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种。
目前使用的是第一种方法。
方法1:线程互斥
static class Program
{
private static System.Threading.Mutex mutex;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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());
}
}
}
winform判断程序是否运行,且只能运行一个实例的更多相关文章
- C# WINFORM判断程序是否运行,且只能运行一个实例(转)
判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种, 方法1:线程互斥 static class Program { private static System.Threading. ...
- WinForm判断程序是否已经在运行,且只允许运行一个实例
我们开发WinFrom程序,很多时候都希望程序只有一个实例在运行,避免运行多个同样的程序,一是没有意义,二是容易出错. 为了更便于使用,笔者整理了一段自己用的代码,可以判断程序是否在运行,只运行一个实 ...
- C#判断程序是否以管理员身份运行,否则以管理员身份重新打开
/// <summary> /// 判断程序是否是以管理员身份运行. /// </summary> public static bool IsRunAsAdmin() { Wi ...
- 判断程序是否在VMWare内运行
现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害.但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机 ...
- WinForm 应用程序禁止多个进程运行
方法一: 禁止多个进程运行 using System; using System.Collections.Generic; using System.Linq; using System.Window ...
- winform只能有一个实例运行且打开已运行窗口
static class Program { private static Mutex onlyOne; [STAThread] static void Main() { onlyOne = new ...
- winform判断chrome是否正在最前端运行
/// <summary> /// 获取系统当前活动窗口 /// </summary> /// <returns></returns> [DllImpo ...
- 【转】winform 程序实现一次只能打开一个该程序
ref: http://www.jb51.net/article/17747.htm //在程序的main函数中加入以下代码 bool createdNew; System.Threading.Mut ...
- windows下程序启动检查,只启动一个实例
问题来源:http://bbs.csdn.net/topics/390998279?page=1#post-398983061 // Only_once.cpp : 定义控制台应用程序的入口点. // ...
随机推荐
- Linux安装禅道教程
环境: centos7 64位 禅道11.2 Linux一键安装包64位 下载: 禅道下载地址: http://dl.cnezsoft.com/zentao/11.2/ZenTaoPMS.11.2.s ...
- transient关键字的作用以及几个疑问的解决
目录 1.从Serilizable说到transient 2.序列化属性对象的类需要实现Serilizable接口? 3.不想被序列化的字段怎么办? 4.ArrayList里面的elementData ...
- 学习关注:学习C++的前景
许多朋友都有这样的疑问,学习C/++能做什么?有什么好处?薪资怎么样,下面就为大家来解答! 像前面的文章有提到过,计算机专业位列热门专业第一门,可想前景如何.C语言C++属于高级语言,适合人类编写,现 ...
- linux下定时任务的简单示例
1.方式一:用sleep实现定时任务: 1.1 编辑shell脚本,如下sleep_aa.sh: #!/bin/bash t1=`date "+%Y-%m-%d %H:%M:%S" ...
- 简单RTSCamera实现
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TopCamer ...
- js预解析练习
分析下面两段代码 console.log(num)//undefined var num = 10 f()//可以执行 //f2()不可以执行 console.log(f)//function con ...
- Django REST Framework JWT 用户的登录注册
安装配配置 pip install djangorestframework-jwt 配置setting ########### 1.在INSTALLED_APPS中加入'rest_framework. ...
- moviepy音视频剪辑:headblur的参数r_blur卷积核以及fx、fy、r_zone的功能作用及用途
☞ ░ 前往老猿Python博文目录 ░ 在moviepy1.03版本中,headblur的调用语法为:headblurbak(clip,fx,fy,r_zone,r_blur=None) 其中参数f ...
- pycharm 2018.2.4过期-激活处理方式(Axure8.0版本到期)
参考文章:https://blog.csdn.net/HALEN001/article/details/81137092 第一种方法亲测可以 大致步骤: 1.2018.8.15更新最新破解补丁Jetb ...
- Mac上特殊方式启动Parallels Desktop 16.0.0 (48916)
背景: mbp升级到Big Sur版本,Parallels Desktop使用到是16.0.0 (48916)版本,然而由于兼容性问题,Parallels Desktop截止当前无法正常使用. 网上搜 ...