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获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...
随机推荐
- [九省联考 2018]一双木棋chess
Description 题库链接 给出一个 \(n\times m\) 的棋盘,棋盘的每个格子有两个权值 \(A,B\) . Alice 和 Bob 轮流操作在棋盘上放棋子,一个格子能放棋子的前提条件 ...
- 资料汇总--Ajax中Put和Delete请求传递参数无效的解决方法(Restful风格)【转】
开发环境:Tomcat9.0 在使用Ajax实现Restful的时候,有时候会出现无法Put.Delete请求参数无法传递到程序中的尴尬情况,此时我们可以有两种解决方案:1.使用地址重写的方法传递参数 ...
- [转]Creating a custom ribbon for Outlook 2013, 2010 and toolbar for Outlook 2007, 2003 – C# sample
本文转自:https://www.add-in-express.com/creating-addins-blog/2013/05/21/outlook-ui-customization-ribbons ...
- Ionic3 UI组件之 ImageViewer
组件特性: 轻触图片可全屏查看 手势上下滑动可关闭全屏查看 点击导航箭头可关闭视图 双击查看全图,并可放大 参考地址:https://github.com/Riron/ionic-img-viewer ...
- Autofac--手动依赖注入
本文只适合初步接触 autoafc 的小白,大佬勿喷. autofac是一种很轻量的一个依赖注入容器 暂时先说一下手动注入的方式(因为使用自动注入遇到了坑,暂时先写一下手动注入) 1.先定义接口 pu ...
- .net面试题[转载]
1.简述private.protected.public.internal修饰符的访问权限. private:私有成员,在类的内部才可以访问. protected:保护成员,该类内部和继承类中可以访问 ...
- MVC登陆认证简单设置
首先,弄个基类 /// <summary> /// 所有控制器基类,里面重写了OnActionExecuted方法 /// </summary> public class Ba ...
- XCode 添加自定义framework运行时出现dyld: Library not loaded的解决方法
XCode添加自定义framework运行时出现dyld: Library not loaded的解决方法 在使用自定义的framework运行时,会出现如下的错误: dyld: Library no ...
- Java版分布式ID生成器技术介绍
分布式全局ID生成器作为分布式架构中重要的组成部分,在高并发场景下承载着分担数据库写瓶颈的压力. 之前实现过PHP+Swoole版,性能和稳定性在生产环境下运行良好.这次使用Java进行重写,目前测试 ...
- 虚拟机VMware workstations的网络设置
一般遇到虚拟机中上不了网的问题,可以这样解决: 1.在终端输入命令:ifconfig.--查看eth0接口上是否有IP地址. 发现eth0接口上没有ip地址. 2.输入cat /etc/sysconf ...