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获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...
随机推荐
- neo4j 查询
match (p: Node {name:"城关镇"}) return p p 代表的是一个变量,Node为插入neo4j时节点类型,name后面加节点的名称
- IOC给程序带来的好处
IOC(inversion of control)的中文解释是“控制反转”或者“依赖注入”,它的实现目的是:我们可以通过配置文件来控制程序的流程,达到程序代码的优化.初学者往往弄不清楚为什么我们可以通 ...
- SpringMVC源码阅读:核心分发器DispatcherServlet
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将介绍SpringMVC的核 ...
- Spring配置Quartz任务调度、及 ThreadPool 线程池
ONE.除了引入 Spring 相关的 jar 包,还要引入 Quartz 的 jar 包 <dependency> <groupId>org.springframework& ...
- [转]Global exception handling in Web API 2.1 and NLog
本文转自:https://stackoverflow.com/questions/25865610/global-exception-handling-in-web-api-2-1-and-nlog ...
- Ionic3 UI组件之 ImageLoader
ImageLoader:通过后台线程加载图片(异步)并缓存.类似于Glide或者Picasso. 组件特性: 后台线程下载图片,下载速度更快,不使用webview的资源: 缓存图像.图像将在您下次显示 ...
- 【原】Spring整合Shiro基础搭建[3]
1.前言 上个Shiro Demo基础搭建是基于官方的快速入门版本,没有集成其他框架,只是简单的通过Main方法来执行Shiro工作流程,并测试一下比较核心的函数:但在企业开发中一般都会集成Sprin ...
- 山东第四届省赛C题: A^X mod P
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3232 Problem C:A^X mod P Time Limit: 5 Sec Memor ...
- 聊聊MyBatis缓存机制【美团-推荐】
聊聊MyBatis缓存机制 2018年01月19日 作者: 凯伦 文章链接 18778字 38分钟阅读 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用My ...
- 浅谈白鹭Egret
浅谈白鹭Egret 最近在做一个移动项目,技术选型的时候接触到了白鹭,简单了解了之后觉得挺合适的,最终就选择了这个引擎. 为什么会选择白鹭引擎呢? 我看上他主要有一下几点: 1 ...