判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种, 方法1:线程互斥 static class Program { private static System.Threading.Mutex mutex; /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetComp…
前言 判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种. 目前使用的是第一种方法. 方法1:线程互斥 static class Program { private static System.Threading.Mutex mutex; /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); App…
我们开发WinFrom程序,很多时候都希望程序只有一个实例在运行,避免运行多个同样的程序,一是没有意义,二是容易出错. 为了更便于使用,笔者整理了一段自己用的代码,可以判断程序是否在运行,只运行一个实例,而且能实现当程序在运行时,再去双击程序图标,直接呼出已经运行的程序. 下面看代码,只需在程序的入口文件中加如下代码即可: static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] s…
/// <summary> /// 判断程序是否是以管理员身份运行. /// </summary> public static bool IsRunAsAdmin() { WindowsIdentity id = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(id); return principal.IsInRole(WindowsBuiltInRole.Admini…
现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害.但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机下不能使用的功能屏蔽掉. 判断程序是否在VMWare虚拟机内,可以用以下代码来完成: function IsRunInVMWare(out ErrMsg: string): Boolean;beginResult := False;try    asm      push     edx     …
方法一: 禁止多个进程运行 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; Syst…
ref: http://www.jb51.net/article/17747.htm //在程序的main函数中加入以下代码 bool createdNew; System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName", out createdNew); if (createdNew) { Application.Run(new LoginForm()); instance.Release…
static class Program { private static Mutex onlyOne; [STAThread] static void Main() { onlyOne = new Mutex(true, Process.GetCurrentProcess().ProcessName); if (onlyOne.WaitOne(0, false)) { Application.Run(new mainForm()); } else { MessageBox.Show("应用程序…
/// <summary> /// 获取系统当前活动窗口 /// </summary> /// <returns></returns> [DllImport("User32.DLL")] static extern IntPtr GetForegroundWindow(); /// <summary> /// 获取指定窗体的标题 /// </summary> /// <param name="Win…
问题来源:http://bbs.csdn.net/topics/390998279?page=1#post-398983061 // Only_once.cpp : 定义控制台应用程序的入口点. // //请参考<<windows核心编程>> #include "StdAfx.h" #include <iostream> #include <windows.h> using namespace std; #define MUTEX_NAM…