判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种,

方法1:线程互斥

static class Program
{
private static System.Threading.Mutex mutex; /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); mutex = new System.Threading.Mutex(true, "OnlyRun");
if (mutex.WaitOne(, false))
{
Application.Run(new MainForm());
}
else
{
MessageBox.Show("程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
}
}

方法2:

这种检测进程的名的方法,并不绝对有效。因为打开第一个实例后,将运行文件改名后,还是可以运行第二个实例。

static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); // get the name of our process
string p = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
// get the list of all processes by that name
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(p);
// if there is more than one process
if (processes.Length > )
{
MessageBox.Show("程序已经在运行中", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
else
{
Application.Run(new MainForm());
}
}
}
if (System.Diagnostics.Process.GetProcessesByName("程序进程中的名称").ToList().Count > )
{
//存在
}
else
{
//不存在
}

利用反放射获取当前应用程序的全路径:

public static string GetPath()
{
return System.Reflection.Assembly.GetExecutingAssembly().Location;
}

原文:

http://www.cnblogs.com/JuneZhang/archive/2012/03/22/2412121.html

http://www.lai18.com/content/953909.html

C# WINFORM判断程序是否运行,且只能运行一个实例(转)的更多相关文章

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

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

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

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

  3. C#判断程序是否以管理员身份运行,否则以管理员身份重新打开

    /// <summary> /// 判断程序是否是以管理员身份运行. /// </summary> public static bool IsRunAsAdmin() { Wi ...

  4. 判断程序是否在VMWare内运行

    现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害.但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机 ...

  5. WinForm 应用程序禁止多个进程运行

    方法一: 禁止多个进程运行 using System; using System.Collections.Generic; using System.Linq; using System.Window ...

  6. 【转】winform 程序实现一次只能打开一个该程序

    ref: http://www.jb51.net/article/17747.htm //在程序的main函数中加入以下代码 bool createdNew; System.Threading.Mut ...

  7. winform只能有一个实例运行且打开已运行窗口

    static class Program { private static Mutex onlyOne; [STAThread] static void Main() { onlyOne = new ...

  8. winform判断chrome是否正在最前端运行

    /// <summary> /// 获取系统当前活动窗口 /// </summary> /// <returns></returns> [DllImpo ...

  9. windows下程序启动检查,只启动一个实例

    问题来源:http://bbs.csdn.net/topics/390998279?page=1#post-398983061 // Only_once.cpp : 定义控制台应用程序的入口点. // ...

随机推荐

  1. Spring发送带附件邮件

    下面是一个例子使用Spring通过Gmail SMTP服务器来发送电子邮件附件.为了包含附件的电子邮件,你必须使用 Spring的JavaMailSender及MimeMessage 来代替 Mail ...

  2. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析和开发示例

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  3. Java ClassLoader加载机制理解

    今天看到了一篇介绍Java ClassLoader加载机器的文章, 才发觉一直来自己的肤浅, 好好地给补了一课, 不得不存档! 原文地址: http://www.blogjava.net/lhulcn ...

  4. linux创建swap分区

    创建交换分区 root@zabbix-server:~# mkdir /swap root@zabbix-server:~# cd /swap/ root@zabbix-server:/swap# l ...

  5. Android 新浪博客分享问题总结

     分类: android(33)  版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 我遇到BUG 1    sso package or singn error Bug2 ...

  6. CentOS安装mysql*.rpm提示conflicts with file from package的解决的方法

    CentOS 6.5下安装MySql 5.6 解压文件:tar xvf MySQL-5.6.19-1.linux_glibc2.5.x86_64.rpm-bundle.tar 释放出下面文件: MyS ...

  7. (转)找回vss超级管理员密码

    原文:http://www.cnblogs.com/446557021/archive/2011/01/05/1926213.html 如果忘记了VSS管理员密码,打开vss数据库所在的文件夹,打开d ...

  8. OpenShift 如何获取bearer Token以便进行各种API调用

    Openshift 需要通过bearer token的方式和API进行调用,比如基于Postman就可以了解到,输入bearer token后 1.如何获取Bearer Token 但Bearer T ...

  9. structure needs cleaning

    If you're attempting to run xfs_repair, getting the error message that suggests mounting the filesys ...

  10. 在Spark中尽量少使用GroupByKey函数(转)

    原文链接:在Spark中尽量少使用GroupByKey函数 为什么建议尽量在Spark中少用GroupByKey,让我们看一下使用两种不同的方式去计算单词的个数,第一种方式使用reduceByKey  ...