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

using System;
using System.Collections.Generic;
using System.Windows.Forms; namespace DuoYeMianIE
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
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(); //这两行实现 XP 可视风格
System.Windows.Forms.Application.Run(new LamBrowser());
// Main 为你程序的主窗体,如果是控制台程序不用这句
mutex.ReleaseMutex();
}
else
{
MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
}
}
}
//方法二:禁止多个进程运行,并当重复运行时激活以前的进程

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection; namespace DuoYeMianIE
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
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.
System.Windows.Forms.Application.Run(new LamBrowser());
}
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 = ;
}
}

C#限制程序只能运行一個实例 (防多开)的更多相关文章

  1. 问题:C#控制台;结果:C#限制程序只能运行一個实例 (防多开)

    C# Console类的具体用法 作者: 字体:[增加 减小] 类型:转载 时间:2013-03-08 这篇文章主要介绍C# Console类的具体用法,需要的朋友可以参考下   Console.Wr ...

  2. Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作

    Oracle 远程访问配置   服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...

  3. 同一个PC只能运行一个应用实例(考虑多个用户会话情况)

    原文:同一个PC只能运行一个应用实例(考虑多个用户会话情况) class Program { private static Mutex m; [STAThread] static void Main( ...

  4. C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper

    前言 在开发应用程序时,通常只让程序运行一个实例.所以,就要判断程序是否已经运行. 下面是我自己在项目中使用到,封装好的帮助类.有 普通的 C# 应用程序 和 Windows CE 和 Windows ...

  5. c# 程序只能运行一次(多次运行只能打开同一个程序)

    转自:https://social.msdn.microsoft.com/Forums/zh-CN/6398fb10-ecc2-4c03-ab25-d03544f5fcc9/2291420309357 ...

  6. C#只能运行一个实例程序的方法

    互斥进程(程序), 简单点说,就是在系统中只能有该程序的一个实例运行. 现在很多软件都有这功能,如Maxthon 可以设置为"只允许打开一个窗体",还有Bitcomet等. 我也是 ...

  7. C# WINFORM判断程序是否运行,且只能运行一个实例(转)

    判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种, 方法1:线程互斥 static class Program { private static System.Threading. ...

  8. winform判断程序是否运行,且只能运行一个实例

    前言 判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种. 目前使用的是第一种方法. 方法1:线程互斥 static class Program { private static S ...

  9. 让程序只运行一个实例(Delphi篇)(三种方法,其中使用全局原子的方法比较有意思)

    Windows 下一个典型的特征就是多任务,我们可以同时打开多个窗口进行操作,也可以同时运行程序的多个实例,比如可以打开许多个资源管理器进行文件的移动复制操作.但有时出于某种考虑(比如安全性),我们要 ...

随机推荐

  1. js实现简单易用的上下无缝滚动效果

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. HDFS简介【全面讲解】

    http://www.cnblogs.com/chinacloud/archive/2010/12/03/1895369.html [一]HDFS简介HDFS的基本概念1.1.数据块(block)HD ...

  3. #include #import @class 的一些用法区别

    从网上查了一些资料,整理了一下,发现很多都说的比较详尽,下面摘录自网络 说一下#import同class之间的区别 在ios中我们经常会在.h和.m中引入一些类啊等等一般用的是#import来进行声明 ...

  4. 装载:关于拉格朗日乘子法与KKT条件

    作者:@wzyer 拉格朗日乘子法无疑是最优化理论中最重要的一个方法.但是现在网上并没有很好的完整介绍整个方法的文章.我这里尝试详细介绍一下这方面的有关问题,插入自己的一些理解,希望能够对大家有帮助. ...

  5. Redis常用操作

    一.string类型的常用命令 set key1 com #一个key对应一个value,多次复制,会覆盖前面的value setnx key1 zhangsan #如果key1不存在则创建key1, ...

  6. DAG上的动态规划之嵌套矩形

    题意描述:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽, 矩形(a,b)可以嵌套在矩形(c,d)当且仅当a<c且b<d, 要求选出尽量多的矩形排成一排,使得除了最后一个外, ...

  7. hive权限管理之实践

    一.实践心得 主要参考这个连接,里面说得也挺详细的.http://www.aboutyun.com/thread-12549-1-1.html 总结如下: 1.若赋予用户某个表的权限,查用户在该表所属 ...

  8. 作业:用HTML制作邮箱登陆界面

    <body leftmargin="200" rightmargin="200"> <font size="45" > ...

  9. Linux 下WordPress FTP帐号解决办法

    自己用Ubuntu搭建WordPress后在更换主题时提示需要输入FTP帐号和密码,解决办法主要是把WordPress主目录的权限所有者弄为Apache: 找到apache服务所使用的用户名和用户组 ...

  10. kuangbin_UnionFind C (HDU 1213)

    过程模板 扫一下一共有几棵树 输出 #include <iostream> #include <string> #include <cstdio> #include ...