如何检查我的程序是否已在运行? [英] How can I check if my program is already running?

查看:42
本文介绍了如何检查我的程序是否已在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过这种方式:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;

namespace mws
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                if (IsApplicationAlreadyRunning() == true)
                {
                    MessageBox.Show("The application is already running");
                }
                else
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
            }
            catch (Exception err)
            {
                Logger.Write("error " + err.ToString());
            }
        }
        static bool IsApplicationAlreadyRunning()
        {
            string proc = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(proc);
            if (processes.Length > 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

但是我遇到了一些问题.

But I'm getting some problems.

首先,当我在Visual Studio中加载项目,然后运行程序时,它正在检测项目的vshost.exe文件,例如:My project.vshost

First, when I'm loading my project in Visual Studio and then running my program it's detecting the vshost.exe file of my project for example: My project.vshost

而且我希望它仅在找到.exe的情况下才检测我的程序是否正在运行,例如:我的project.exe而不是vshost.

And I want that it will detect if my program is running only when I'm running the program only if it find the .exe for example: My project.exe not the vshost.

推荐答案

使用互斥锁.

static class Program {
    static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
    [STAThread]
    static void Main() {
        if(mutex.WaitOne(TimeSpan.Zero, true)) {
            try
            {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
            }
            finally
            {
             mutex.ReleaseMutex();
            }
        } else {
            MessageBox.Show("only one instance at a time");
        }
    }
}

如果我们的应用程序正在运行,则WaitOne将返回false,并且您将收到一个消息框.

If our app is running, WaitOne will return false, and you'll get a message box.

正如@Damien_The_Unbeliever正确指出的那样,您应该为所编写的每个应用程序更改互斥量的Guid!

As @Damien_The_Unbeliever pointed out correctly, you should change the Guid of the mutex for each application you write!

来源: http://sanity-free.org/143/csharp_dotnet_single_instance_application.html

这篇关于如何检查我的程序是否已在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆