WinForm 应用程序禁止多个进程运行
方法一: 禁止多个进程运行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace 开启新的进程
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool flag;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out flag);
if (flag)
{
// 启用应用程序的可视样式
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); // 处理当前在消息队列中的所有 Windows 消息
Application.DoEvents();
Application.Run(new Form1()); // 释放 System.Threading.Mutex 一次
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "相同的程序已经在运行了,请不要同时运行多个程序!\n\n这个程序即将退出!",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
}
}
}
方法二: 禁止多个进程运行,并当重复运行时激活以前的进程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices; namespace 开启新的进程
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
System.Diagnostics.Process instance = RunningInstance();
if (instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
HandleRunningInstance(instance);
}
} /// <summary>
/// 获取当前正在运行的进程实例
/// </summary>
/// <returns></returns>
public static Process RunningInstance()
{
// 获取当前活动的进程
Process current = Process.GetCurrentProcess();
// 获取当前本地计算机上指定的进程名称的所有进程
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
// 忽略当前进程
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
return process;
}
}
} // 如果没有其他同名进程存在,则返回 null
return null;
} // 指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开
[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 = ; /// <summary>
/// 如果有另一个同名进程启动,则调用之前的实例
/// </summary>
/// <param name="instance"></param>
private static void HandleRunningInstance(Process instance)
{
// 确保窗体不是最小化或者最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
// 将之前启动的进程实例弄到前台窗口
SetForegroundWindow(instance.MainWindowHandle);
}
}
}
WinForm 应用程序禁止多个进程运行的更多相关文章
- C# Winform 禁止一个进程运行多次
禁止一个进程运行多次 using System; using System.Windows.Forms; namespace StartExe { static class Program { /// ...
- Winform防止程序重复运行
需求:1.点击“关闭”按钮时,程序最小化到托盘,并没有退出,这时再次运行程序,不会重复运行,而是显示已运行的程序:2.支持不同目录:3.支持修改名称. 代码(不支持修改名称,不支持不同目录): usi ...
- WinForm判断程序是否已经在运行,且只允许运行一个实例
我们开发WinFrom程序,很多时候都希望程序只有一个实例在运行,避免运行多个同样的程序,一是没有意义,二是容易出错. 为了更便于使用,笔者整理了一段自己用的代码,可以判断程序是否在运行,只运行一个实 ...
- 解决Winform应用程序中窗体背景闪烁的问题
本文转载:https://my.oschina.net/Tsybius2014/blog/659742 我的操作系统是Win7,使用的VS版本是VS2012,文中的代码都是C#代码. 这几天遇到一个问 ...
- 使用控制台调试WinForm窗体程序
.程序代码结构 .Win32DebuggerHelper.cs using System.Runtime.InteropServices; /* TODO:使用方法 Win32.AllocConsol ...
- 空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标
今天休息在家,由于天气热再加上疫情原因,就在家里呆着,空闲时想着,在很早以前(约3年前),产品人员跟我提了一个需求,那就是winform桌面程序的图标能否根据节日动态更换,这种需求在移动APP上还是比 ...
- Winform应用程序实现通用消息窗口
记得我之前发表过一篇文章<Winform应用程序实现通用遮罩层>,是实现了透明遮罩的消息窗口,功能侧重点在动图显示+消息提醒,效果看上去比较的炫,而本篇我又来重新设计通用消息窗口,功能重点 ...
- iOS开发:保持程序在后台长时间运行
iOS开发:保持程序在后台长时间运行 2014 年 5 月 26 日 / NIVALXER / 0 COMMENTS iOS为了让设备尽量省电,减少不必要的开销,保持系统流畅,因而对后台机制采用墓碑式 ...
- C程序演示产生僵死进程的过程
先抄录网上一段对僵死进程的描述: 僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...
随机推荐
- Numpy基础总结
一.文件读取 numpy.genfromtxt() 可以用来读取各种文件.常用语法大致如下: numpy.genfromtxt(fname, dtype=<type 'float'>, d ...
- centos6 内网可达,外网不可达 Network is unreachable
错误内容 [root@djx-2 yum.repos.d]# ping 3.0.82.21 connect: Network is unreachable [root@djx-2 yum.repos. ...
- [C语言]类型限定词const解析
作为C90增加的一个受限类型关键字,const赋予了它修饰的变量一个新属性——不变性,如果一个变量声明中带有关键字const,则无法通过赋值.增减运算来修改该变量的值. 一.指针与const结合 co ...
- 01 JDBC的问题
jdbc编程步骤: 1. 加载.注册数据库驱动 DriverManager 2. 创建并获取数据库链接 Connection 3. 创建jdbc statement/preparedState ...
- after_create and after_commit
A relational database, like mysql, provides transactions to wrap several operations in one unit, mak ...
- mysql中Access denied for user 'root'@'localhost' (using password:YES)错误
此错误主要是由于你的系统曾经装过MYSQL,在重装就会要求输入原来设定的密码 由于输入错误导致 解决办法见 上一篇博客 MYSQL安装时解决要输入current root passwo ...
- iOS 中的 armv7,armv7s,arm64,i386,x86_64 都是什么
在做静态库的时候以及引用静态库的时候经常会遇到一些关于真机模拟器不通用的情况,会报错找不到相应库导致编译失败, 这里简单记录一下各种设备支持的架构. iOS测试分为模拟器测试和真机测试,处理器分为32 ...
- 啰里吧嗦kafka
1.kafka是什么 kafka官网: http://kafka.apache.org/ kafka是一种高吞吐量的分布式发布订阅消息系统,用它可以在不同系统中间传递分发消息 2.zookeeper是 ...
- SQL Server Profiler小技巧——筛选请求
如果需要转载,请附上本文作者和原文链接:http://www.cnblogs.com/zeusro/p/4016228.html Microsoft SQL Server Profiler 是 SQL ...
- JPA注解@GeneratedValue
@GeneratedValue是JPA的标准用法, JPA提供四种标准用法,由@GeneratedValue的源代码可以看出. public enum GenerationType { TABLE, ...