运行一次实例的问题
2005-02-21 15:33
232 查看
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
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;
}
static void Main()
{
//判断之前是否已经打开了相同的进程
//new Bestzone.MIS.Report.F1BookContainer();
bool alreadyExist = false;
try
{
System.Diagnostics.Process curP = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.Process p in ps)
{
if(p.ProcessName.Equals(curP.ProcessName) && p.Id != curP.Id)
{
alreadyExist = true;
}
}
}
catch(System.PlatformNotSupportedException ex){ex.ToString();}
catch(System.InvalidOperationException ex){ex.ToString();}
//如果已经存在,则放弃本进程
if(alreadyExist)
{
return;
}
Application.Run(new MainForm());
}
相关文章推荐
- 一次socket长连接运行导致的性能问题
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 最大子数组问题-暴力求解-c++代码实现及运行实例结果
- 雇佣问题(hireassistant)-c++代码实现及运行实例结果
- 雇佣问题随机排列数组(permuteBySorting)-c++代码实现及运行实例结果
- 雇佣问题原址排列给定数组(randomize In Place)-c++代码实现及运行实例结果
- 在线雇佣问题(onlineMaximumHireAssistant)-c++代码实现及运行实例结果
- React Native官方实例UIExplorer项目(for android)运行遇到的问题
- 最大子数组问题-c++代码实现及运行实例结果
- .net利用程序集的GUID解决程序只能运行一次的问题
- 在网上找了个java小游戏的实例,敲完后发现没有main,自己加了个后,无法运行请问是什么问题 [ Java SE]
- 以不同用户身份运行程序,/savecred只需要输入一次密码(GetTokenByName取得EXPLORER.EXE的令牌,然后调用CreateProcessAsUser,而且使用LoadUserProfile解决另存文件的问题)good
- web项目Log4j日志输出路径配置问题 问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文
- WINFORM 只能运行一个实例问题
- Windows下Cygwin环境的Hadoop安装(3)- 运行hadoop中的wordcount实例遇到的问题和解决方法
- 解决任务计划程序未启动任务,因为相同任务的实例正在运行的问题
- 最简单的0-1背包问题c++代码实例及运行结果
- JavaScript只可以运行一次问题,那位大大帮忙看下
- C#让程序只运行一次实例
- win7环境Eclipse中运行JAVA项目读取HDFS(hadoop-2.7.2)中的文件实例与问题汇总