[英]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.