[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. Windows开发环境如何启用Directory.Build.props版本号集中管理

    每个产品一个根目录 Directory.Build.props的工作模式和NuGet.Config不同,Directory.Build.props不能继承.当dotnet restore工作的时候,会 ...

  2. 运行Study.Trade模块的Web.Unified.Host

    1.把Web.Host的项目设置为启动项目 上一篇文章报错,因为npm功能没有安装,导致Web.Unified.Host的wwwroot下没有libs目录. 2.默认是在IIS Express中承载 ...

  3. UG474

    为了对工程的资源利用率进行优化,我们首先需要知道当前工程对资源的利用率情况.在Vivado下,我们可以查看工程的资源利用率情况,在下面这张图中,其罗列出了整个工程所使用的资源情况.首先,下面我们需要一 ...

  4. 使用Order By NULL 解决 group by后自动排序,优化Sql性能

    使用Order By NULL 解决 group by后自动排序,优化Sql性能 对于 Group by 后的结果,Mysql搜索引擎会将结果按照Group by 的字段按照升序,自动排序,例如: t ...

  5. Java 关于继承小练习

    1 package com.bytezero.inherit; 2 3 /* 4 * 面向对象的特征二:继承性 5 * 6 * 一.继承性的好处: 7 * ①减少了代码的冗余,提供了代码的复用性 8 ...

  6. Vue3学习(二十三)- 保存文档内容正常显示

    写在前面 情人节已经接近尾声了,虽然跟我没什么关系,但是我还是很渴望,能遇到一个良人相伴一生. 现在时间: 内心异常平静,相对吵闹我更喜欢安静的晚上,没人打扰,enjoy自己独处的时间! 保存内容显示 ...

  7. ADS1299芯片datasheet 重点解析

    一 START和DRDY的关系 start必须要至少提前拉高2个时钟,才会产生DRDY信号,这个非常关键,也是重心所在.很多遗漏的就不会有DRDY信号出来了. 二 START和DRDY的时序图 sta ...

  8. 华为sound x智能音箱初体验

    外观颜值   在这个网红遍地的年代,好看的皮囊是那么的重要.很多东西,买与不买,只是你在电脑的橱上看它一眼.颜值对一个消费电子产品来说,在这个虚拟的互联网世界中是那么的重要.sound x的初次看来, ...

  9. ESP8266 下安装esptool.py并使用esptool刷机神助手

    一 前记 在使用ESP8266模块时,通常会用到一些刷机软件.官方提供了nodemcu_flasher.ESPFlashDownloadTool.ESP8266Flasher等下载工具,但是缺少更底层 ...

  10. opencv-python笔记(1)

    openCV笔记 图像 灰度图像 ​ 0代表纯黑色,255代表白色(类似亮度) 通道拆分 通过索引拆分: import cv2 lena=cv2.imread("lenacolor.png& ...