方法一: 禁止多个进程运行

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 应用程序禁止多个进程运行的更多相关文章

  1. C# Winform 禁止一个进程运行多次

    禁止一个进程运行多次 using System; using System.Windows.Forms; namespace StartExe { static class Program { /// ...

  2. Winform防止程序重复运行

    需求:1.点击“关闭”按钮时,程序最小化到托盘,并没有退出,这时再次运行程序,不会重复运行,而是显示已运行的程序:2.支持不同目录:3.支持修改名称. 代码(不支持修改名称,不支持不同目录): usi ...

  3. WinForm判断程序是否已经在运行,且只允许运行一个实例

    我们开发WinFrom程序,很多时候都希望程序只有一个实例在运行,避免运行多个同样的程序,一是没有意义,二是容易出错. 为了更便于使用,笔者整理了一段自己用的代码,可以判断程序是否在运行,只运行一个实 ...

  4. 解决Winform应用程序中窗体背景闪烁的问题

    本文转载:https://my.oschina.net/Tsybius2014/blog/659742 我的操作系统是Win7,使用的VS版本是VS2012,文中的代码都是C#代码. 这几天遇到一个问 ...

  5. 使用控制台调试WinForm窗体程序

    .程序代码结构 .Win32DebuggerHelper.cs using System.Runtime.InteropServices; /* TODO:使用方法 Win32.AllocConsol ...

  6. 空闲时间研究一个小功能:winform桌面程序如何实现动态更换桌面图标

    今天休息在家,由于天气热再加上疫情原因,就在家里呆着,空闲时想着,在很早以前(约3年前),产品人员跟我提了一个需求,那就是winform桌面程序的图标能否根据节日动态更换,这种需求在移动APP上还是比 ...

  7. Winform应用程序实现通用消息窗口

    记得我之前发表过一篇文章<Winform应用程序实现通用遮罩层>,是实现了透明遮罩的消息窗口,功能侧重点在动图显示+消息提醒,效果看上去比较的炫,而本篇我又来重新设计通用消息窗口,功能重点 ...

  8. iOS开发:保持程序在后台长时间运行

    iOS开发:保持程序在后台长时间运行 2014 年 5 月 26 日 / NIVALXER / 0 COMMENTS iOS为了让设备尽量省电,减少不必要的开销,保持系统流畅,因而对后台机制采用墓碑式 ...

  9. C程序演示产生僵死进程的过程

    先抄录网上一段对僵死进程的描述: 僵尸进程:一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中.这种 ...

随机推荐

  1. C/C++ -- Gui编程 -- Qt库的使用 -- 使用图片与动画

    QWidget工程 #include "mywidget.h" #include "ui_mywidget.h" #include <QLabel> ...

  2. 详解C#委托和事件(一)

    委托(Delegate)是安全封装方法的类型,类似于C和C++中的函数指针,与函数指针不同的是,委托是面向对象的.类型安全的和可靠的: 一.委托类型是CTS中五种基础类型之一,是一种引用类型,表示对具 ...

  3. android学习-IPC机制之ACtivity绑定Service通信

    bindService获得Service的binder对象对服务进行操作 Binder通信过程类似于TCP/IP服务连接过程binder四大架构Server(服务器),Client(客户端),Serv ...

  4. linux内核学习之保护模式(一)

    来源:http://blog.csdn.net/yishuige/article/details/50434746 这一章涉及intel8086系列cpu的保护模式编程,应该是学习内核编程,驱动编程及 ...

  5. Go 程序执行顺序

    在一个 go 程序中通常包含:包.常量.变量.init().main()等元素,如果同时存在多个包,包之间存在依赖关系,每个包中存在多个 init 函数,每个文件中存在多个 init 函数,那么问题来 ...

  6. java面试⑥框架部分

    2.5.1 什么是框架: 2.5.2 MVC模式 2.5.3 MVC框架 2.5.4 简单讲一下struts2的执行流程 2.5.5 Struts2中的拦截器,你都用它干什么? 2.5.6 简单讲一下 ...

  7. java面试①整体流程

    http://www.toutiao.com/i6463396763549041166/ 1.1 简单的自我介绍 我是xxx工作了xx年,在xx公司,做过xx项目, 1.2你简单介绍一下xxx项目 为 ...

  8. unity简单动画实现

    1:创建一个Sprite Render (player)的动画对象并添加脚本Player,点击主菜单“Window(视窗)→Animation(动画窗口)”Animation面板(选中需要动画的对象) ...

  9. bootstrap 报错

    1. bootstrap table 加载出错,前台报错 Cannot read property 'colspan' of undefined { title : '设备类型', field : ' ...

  10. .net core 2.2 部署CentOS7(3)安装Xshell操控CentOS7

    目录: .net core 2.2 部署CentOS7(1)安装虚拟机 .net core 2.2 部署CentOS7(2)给虚拟机安装CentOS7 .net core 2.2 部署CentOS7( ...