运行一次实例的问题
来源:互联网 发布:dota2怎么公开数据 编辑:程序博客网 时间:2024/04/30 04:39
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());
}
- 运行一次实例的问题
- 另外一种运行实例一次的方法
- 一次运行一个实例
- Cocos2d入门及第一次运行时遇到的问题
- jquery中动画(animal)只运行一次的问题!
- 一次socket长连接运行导致的性能问题
- 如何让程序只运行一次实例
- 如何让程序只运行一次实例
- VC程序只运行一次实例
- 一次只有一个实例能够运行
- 使程序一次运行一个实例
- VC程序只运行一次实例
- 一次测试环境运行正常,而生产环境运行失败的问题排查
- hello程序的一次运行
- PB运行的程序只能运行一次
- 增量备份的一次实例
- 增量备份的一次实例
- 几个可运行的,可以说明问题的Ajax实例
- 解决LookupDispatchAction使用中对中文信息支持的问题
- 如何取得MP3的总时间
- Web开发员的苦恼
- 软件狂人精心筛选的"正版破解"软件下载站大全
- JDK1.5中新的语言特征
- 运行一次实例的问题
- DOS下维护注册表的高级技巧
- 利用CSS控制表格颜色的交替显示
- 过年后重回公司
- 合并两个数组
- 一行代码引用资源文件
- 在jbpm3中使用Ioc
- ASP.NET下MVC设计模式的实现
- image与byte数组的转换[引用]