简体   繁体   English

如何使用C#在运行时获取我的应用程序的PID

[英]how to get PID of my app at runtime using C#

My app checks at startup if any other instance of the same is running already, if yes then it will close all other instances. 我的应用程序在启动时会检查是否有其他实例正在运行,如果是,则它将关闭所有其他实例。 For this I tried using Process.GetProcessByName("AppName") function and store all the process with AppName in processes[] array. 为此,我尝试使用Process.GetProcessByName(“ AppName”)函数,并将所有带有AppName的进程存储在processes []数组中。 Now i want to find the PID of current instance so that i can close all other instances of my app (which obviously have same name but different PIDs). 现在,我想查找当前实例的PID,以便可以关闭我的应用程序的所有其他实例(显然,它们具有相同的名称,但具有不同的PID)。 But i am unable to find that even after lot of googling. 但是即使经过大量谷歌搜索,我也找不到。 Also how can i find the PID of an instance of my app which i have created with Process.Start("AppName.exe") function called from inside AppName.exe 另外,我如何找到使用AppName.exe内部调用的Process.Start(“ AppName.exe”)函数创建的我的应用程序实例的PID

Why don't you just check equality with your current process? 您为什么不只检查当前流程的平等性?

var processes = Process.GetProcessByName("AppName");

foreach (var p in processes)
{
    if (p != Process.GetCurrentProcess())
       p.CloseMainWindow(); 
}

If you're interested in closing other instances of your app, why not do the opposite and prevent multiple instances from opening in the first place? 如果您有兴趣关闭应用程序的其他实例,为什么不做相反的事情,并防止多个实例首先打开? Using EventWaitHandle can do this thusly: 使用EventWaitHandle可以做到这一点:

bool created;
var eve = new System.Threading.EventWaitHandle(
    false,
    EventResetMode.AutoReset,
    "MyAppHandle",
    out created);
if(!created)
{
    eve.Set();
    Environment.Exit(-1); // Always use an exit error code if you're expecting to call from the console!
}

The handle parameter, "MyAppHandle" in this case, will be shared across the entire system, thus meaning not only will the out created paramete be false on secondary instaces, but you can use eve.Set() to cause the handle to fire acorss application. 在这种情况下,句柄参数“ MyAppHandle”将在整个系统中共享,因此,不仅意味着在次要实例上, out created参数将为假,而且您可以使用eve.Set()引起句柄触发应用。 Set up a listening thread and this can allow a message loop to display a message when you attempt to open second instance. 设置侦听线程,当您尝试打开第二个实例时,这可以允许消息循环显示消息。

Task.Run(() =>
{
    while(true)
    {
        eve.WaitOne();
        // Display an error here
    }
}

OK, given problems with my other solution, see the following 好的,鉴于我的其他解决方案有问题,请参阅以下内容

In order to hook in between processes, you need some form of IPC. 为了在进程之间进行切换,您需要某种形式的IPC。 To use the simplicty of shared handles between EventWaitHandles, you could make each program listen for a cancellation flag. 要使用EventWaitHandles之间共享句柄的简单性,可以使每个程序侦听取消标志。

public static EventWaitHAndle CancellationEvent =
    new EventWaitHandle(
        false,
        EventResetMode.AutoReset,
        "MyAppCancel");
private object lockObject = new object();

And later... 然后...

Task.Run(() =>
{
    while(true)
    {
        CancellationEvent.WaitOne();
        lock(lockObject)
            if(!thisIsCalling)    // static bool to prevent this program from ending itself
                Environment.Exit(0);
    }
}

And then call the cancellation like so 然后像这样调用取消

lock(lockObject)
{
    thisIsCalling = true;
    CancellationEvent.Set();
    thisIsCalling = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中获取启动的App的PID - How to get PID of started App in C# 如何使用其名称获取 window 服务的 pid 并检查进程是否在任务管理器中运行? 使用 C# - How to get pid of window service using its name and check if the process is running in task manager or not? using C# 有没有办法从使用C#阻止文件的进程中获取PID? - Is there a way to get the PID from a process that is blocking a file using C#? 如何以可用速度在c#中获取PID的子代 - How to get a PID's children in c# at a usable speed 如何在C#中获取特定chromedriver的PID-有多个实例 - How to get PID of the certain chromedriver in c# - there are multiple instances 如何使用运行我的 Windows App Store 应用程序的 C# 获取当前目录? - How do I get the current directory using C# that my Windows App Store app is running in? UWP如何获取使用启动器触发的应用程序的PID? - UWP How to get PID for app triggred using Launcher? C# 如何在运行时获得管理员权限 - C# How to get admin privileges AT RUNTIME 如何获取谁在C#asp.net中使用我的REST API的Android应用名称 - how to get android app name who is using my rest api in c# asp.net 我将如何获得等效的C#代码,以其当前状态重新创建运行时对象? - How would I get the equivalent C# code that will recreate my runtime object in its current state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM