public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if ( != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
[VB.NET]
Public Shared Function RunningInstance() As Process
Dim current As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)
'Loop through the running processes in with the same name
Dim process As Process
For Each process In processes
'Ignore the current process
If <> current.Id Then
'Make sure that the process is running from the exe file.
If [Assembly].GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then
'Return the other process instance.
Return process
End If
End If
Next process
'No other instance was found, return null.
Return Nothing
End Function 'RunningInstance如何确保只有一个应用程序实例在运行?
原创
©著作权归作者所有:来自51CTO博客作者biyusr216的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
如何使应用程序只有一个实例在运行
上网搜索了一下,发现大家都提到了一个方法,使用Mutex类,但是其中有个问题很多人没有遇到。就是如果项目文件很大的放掉。
java jvm 开发语言 搜索 Threading -
如何让应用程序只有一个实例在运行?
/设置真实例程为foreground window。//如果没有其它例程,就新建一个窗体。//确保例程从EXE文件运行。//返回另
单片机 嵌入式硬件 System 最小化 Assembly -
如何使应用程序只运行一个实例
要使应用程序产生的全局标志" 本例中产生的语句如下:#define o
null c 应用程序 互斥量 #define -
如何在应用程序中运行一个EXE程序?
可以使用System.Diagnostics名称空间下的Process类:[C#][VB.NET]
前端 服务器 javascript System 名称空间 -
Python异步编程:深入理解 async
异步模型是一种在单一线程内通过程序员显式控制的任务暂停与恢复,实现多个任务协作式穿插执行的并发编程范式,核心在于非阻塞操作与事件驱动。
python async 事件循环 ci 显式
















