位置: 风云在线 >> 学院 >> 网络编程 >> NET编程 >> 正文
如何让应用程序只有一个实例在运行?
MyForm.cs
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
public class OneInstnace
{
[STAThread]
public static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run (new Form());
}
else
{
//处理发现的例程
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为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;
}
做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
public class OneInstnace
{
[STAThread]
public static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run (new Form());
}
else
{
//处理发现的例程
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为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;
}
做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
娱乐花边图库
GOOGLE提供的广告
相关文章
都市健康网推荐文章
[完美护肤] 2007物美价廉护肤品大盘点
[减肥方法] 13个方法能帮你加速减肥
[老人疾病] 糖尿病与壮阳
[失眠] 临床上治疗失眠的方法
[局部减肥] 简单高效轻松美臀运动
[肝病] 治疗性疫苗疗“肝”效果好
[肝病] 夫妻生活如何预防乙肝?
[健身常识] 简单动作塑惹眼胸肌
[白血病] 慢性白血病
[女性心理] 看不惯别人也许是自己的错
[美食烹饪] 液种面包的制作(图)
[靓丽彩妆] 腮红技法塑造少女形象
[美食烹饪] 百合烹佳肴
[糖尿病] 合理锻炼才是养生关键!
[减肥方法] 减肥食品你吃我吃大家吃
热门电影观看
风云壁纸 每天更新

东方美丽性感女孩系列祼体美女图片高清晰...
共 1 张
人妻熟女豪乳泳装CG漫画
共 28 张
沙滩美女写真,龙泽乃南写真图片
共 7 张
张筱雨美女
共 20 张
黄圣依图片,黄圣依合成图,黄圣依视频,黄...
共 1 张
草地图片,绿色草地grasslands|meadow
共 20 张
桂林山水风光,大自然风景网页制作大宝库...
共 1 张
章子怡美女写真:脱光衣服为广告封面...
共 1 张
Windows Vista超大宽屏壁纸1920*1200像素...
共 1 张
清纯女孩海滩泳装性感图片(2)
共 40 张

