[C#]
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 (process.Id != 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 process.Id <> 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
如果检测到已有窗体实例,将此实例显示出来参考:
[STAThread]
static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form2());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
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 (process.Id != 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;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1
分享到:
相关推荐
保证应用程序只有一个实例运行! 很值得下载看看!资源免费,大家分享!!
c++ vs2008 应用程序只有一个实例 简单的demo 启动程序
可以使用互斥体Mutex类型完成此功能。见如下代码: [STAThread]c_让应用程序只有一个实例运行
Delphi中防止运行一个应用程序的多个实例2个办法 弹出提醒 激活窗口 Delphi源代码3份。
如何使一个应用的多个实例运行在同一个进程空间
当应用程序已经在运行, 再次运行该应用程序时,通常只是把该应用程序的窗口提到前面来,把新的命令行参数传递给第一个运行实例,而第二个实例退出。这在传统的单进程多线程的手机 平台中,实现是简单而直接的,而在...
如何使应用程序只运行一个实例(11kb)
使用共享内存和信号量实现QT应用程序单实例的方法,并且支持根据需要弹出已经运行的程序窗口,解决了同类软件存在的问题,而且程序非常简单。
转自 :如何使应用程序只运行一个实例 VC ,
摘要:VC/C++源码,界面编程,应用程序 VC++将应用程序已运行的实例调至前台,源码运行截图如上所示。
项目9 应用程序设计实例 任务2 系统设计 【例1】根据应用程序设计系统分析,创建一个项目文件"产品管理.pjx"。 1.设计数据库和表 2.设计启动界面 3.设计登录界面 4.设计系统菜单 5.表单设计 6.报表设计 7....
c#应用程序如何只运行一个实例
是一个可以运行的JAVA应用程序 具有界面 有详细注解
一个交通灯程序,用FX2N32MT运行通过.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例一个交通灯程序,用FX2N32MT运行通过.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例一个交通灯...
vc++程序源代码,如何将应用程序已运行的实例调至前台
一个顺序控制,急停保持当前步,复位后继续当前步运行的程序.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例一个顺序控制,急停保持当前步,复位后继续当前步运行的程序.zip三菱PLC编程案例源码资料...
该文档详细描述了QT平台下,让应用程序只运行一个实例的三种方法,并附有相应的代码。
冷却水泵节能循环运行1.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例冷却水泵节能循环运行1.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例冷却水泵节能循环运行1.zip三菱PLC编程...
冷却水泵节能循环运行2.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例冷却水泵节能循环运行2.zip三菱PLC编程案例源码资料编程控制器应用通讯通信例子程序实例冷却水泵节能循环运行2.zip三菱PLC编程...
在我们的程序当中如果要实现类似《360软件管家》的功能,就要解决两个问题,首先是要判断该程序已有一个实例在运行,其次是要将已运行的应用程序实例激活,同时退出第二个应用程序实例。 对于第一个问题,我们可以...