[STAThread]
public static void Main()
{
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (ret)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.Run(new Main());
// Main 为你程序的主窗体,如果是控制台程序不用这句
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
}

方法二:禁止多个进程运行,并当重复运行时激活以前的进程

[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.DoEvents();
//There isn't another instance, show our form.
Application.Run(new Main());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
} public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow(instance.MainWindowHandle);
} [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 = 1;

引用:https://www.cnblogs.com/lidj/archive/2012/07/06/2579897.html

C#只允许启动一个WinFrom进程的更多相关文章

  1. winfrom程序只启动一个exe进程

    private static void KillProcess() { Process process1 = Process.GetCurrentProcess(); //获得当前计算机系统内某个进程 ...

  2. C# WinForm中如何让当前应用程序只允许启动一个实例

    我们在WinForm开发中,很多情况下是需要只允许让用户运行一个实例,那么代码其实很简单.只需要修改Program.cs文件,代码如下 static class Program { /// <s ...

  3. C#/WPF 仅启动一个进程实例

    如何实现仅启动一个 WPF 进程实例,并在打开第二个时,自动唤起之前打开的进程. 1 代码入口 在 App.xaml.cs 文件中,重写 OnStartup 方法,并添加 Mutex 进程锁. /// ...

  4. WinForm程序,实现只启动一个实例

    前言:在我们做的软件中,当点击图标运行时,正常的需求是只需要启动一个软件的实例,这是非常重要的一点,不然就显得我们的软件非常的山寨,笔者在工作中经常遇到同事没有注意这一点,看是不重要,实则非常的重要, ...

  5. 从内存中加载并启动一个exe

    windows似乎只提供了一种启动进程的方法:即必须从一个可执行文件中加载并启动.而下面这段代码就是提供一种可以直接从内存中启动一个exe的变通办法.用途嘛, 也许可以用来保护你的exe,你可以对要保 ...

  6. flask使用debug模式时,存在错误时,会占用设备内存直至服务重启才释放;debug模式会开启一个守护进程(daemon process)

    函数调用顺序flask的app.py的run-->werkzeug的serving.py的run_simple-->调用werkzeug的debug的__init__.py里的类Debug ...

  7. C#只启动一个进程的代码

    把写内容过程中经常用到的内容做个收藏,如下的内容是关于C#只启动一个进程的内容.public partial class App : Application { protected override ...

  8. WinFrom 只启动一个exe,并且获得焦点

    只启动一个exe方法: using System; using System.Collections.Generic; using System.Runtime.InteropServices; us ...

  9. 【.net 深呼吸】启动一个进程并实时获取状态信息

    地球人和火星人都知道,Process类既可以获取正在运行的进程,也可以启动一个新的进程.在79.77%应用场合,我们只需要让目标进程顺利启动就完事了,至于它执行了啥,有没有出错,啥时候退出就不管了. ...

  10. init进程 && 解析Android启动脚本init.rc && 修改它使不启动android && init.rc中启动一个sh文件

    Android启动后,系统执行的第一个进程是一个名称为init 的可执行程序.提供了以下的功能:设备管理.解析启动脚本.执行基本的功能.启动各种服务.代码的路径:system/core/init,编译 ...

随机推荐

  1. jquery实现轮播图切换

    这个是我模仿网易云的音乐界面写的轮播图,主要实现的功能有 1.每隔4秒图片和对应的背景颜色一起切换 2.点击翻页会跳转到相对应的图片以及背景上 3.点击左右翻页,实现顺序切换 <1>HTM ...

  2. 第123篇: JS函数属性与方法

    好家伙,本篇为<JS高级程序设计>第十章"函数"学习笔记 ECMAScript 中的函数是对象,因此有属性和方法. 1.函数属性 每个函数都有两个属性:length 和 ...

  3. 求求你别再用OkHttp调用API接口了,快来试试这款HTTP客户端库吧

    引言 在日常业务开发中,我们时常需要使用一些其他公司的服务,调用第三方系统的接口,这时就会涉及到网络请求,通常我们可以使用HttpClient,OkHttp等框架去完成网络请求.随着RESTful A ...

  4. Kubernetes:Pod 端口映射

    本文为作者的 Kubernetes 系列电子书的一部分,电子书已经开源,欢迎关注,电子书浏览地址: https://k8s.whuanle.cn[适合国内访问] https://ek8s.whuanl ...

  5. Java final 关键字使用

    1 package com.bytezreo.finaltest; 2 3 /** 4 * 5 * @Description final 关键字使用 6 * @author Bytezero·zhen ...

  6. sort自定义排序字符串('1-1','2-1','3-2'此类)

    对数组排序 ['2-3','2-1','1-4','3-2','1-1','2-2','3-1'] 直接使用原生sort 对对象排序 [{a:'2-3'},{a:'2-1'},{a:'1-4'},{a ...

  7. Zabbix“专家坐诊”第183期问答汇总

    问题一 Q:老师,请问一下zabbix采集的数据怎么过滤,获取数据是nottime=20:30 notafter=3,怎么过滤出netafter=3 ?谢谢. A:过滤器设置如下图. 问题二 Q:大佬 ...

  8. axios post xml data方法

    axios#request(config) axios#get(url[,config]) axios#delete(url[,config]) axios#head(url[,config]) ax ...

  9. SpringBoot单次执行任务,退出异常NoClassDefFoundError: ch/qos/logback/classic/spi/ThrowableProxy

    背景 使用SpringBoot 运行一次性作业,用于初始化 问题:直接使用System.exit退出时,遇到异常:NoClassDefFoundError: ch/qos/logback/classi ...

  10. python 读取串口数据常用函数及实例分析

    前记: 人生苦短,我用python,python在做一些算法验证和接口验证方面,的确是非常的好用.读取串口经常用到,这里就做个总结,给自己和周围的人做个备忘吧. 函数解析: 初始化串口数据: impo ...