C#避免程序二次启动并接收参数
2015-10-28 16:57
471 查看
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
namespace gis
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//如果有参数,执行以下内容
if (args.Length > 0)
{
MessageBox.Show(args[0]);
}
bool createdNew;
//系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
//第二个参数可以设置为产品的名称:Application.ProductName
//每次启动应用程序,都会验证名称为Gis的互斥是否存在
Mutex mutex = new Mutex(false, "Gis", out createdNew);
//如果已运行,则在前端显示
//createdNew == false,说明程序已运行
if (!createdNew)
{
Process instance = GetExistProcess();
if (instance != null)
{
SetForegroud(instance);
Application.Exit();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
Application.Run(new Form1());
else
Application.Run(new Form1(args));
}
/// <summary>
/// 查看程序是否已经运行
/// </summary>
/// <returns></returns>
private static Process GetExistProcess()
{
Process currentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if ((process.Id != currentProcess.Id) &&
(Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
{
return process;
}
}
return null;
}
/// <summary>
/// 使程序前端显示
/// </summary>
/// <param name="instance"></param>
private static void SetForegroud(Process instance)
{
IntPtr mainFormHandle = instance.MainWindowHandle;
if (mainFormHandle != IntPtr.Zero)
{
ShowWindowAsync(mainFormHandle, 1);
SetForegroundWindow(mainFormHandle);
}
}
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
namespace gis
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//如果有参数,执行以下内容
if (args.Length > 0)
{
MessageBox.Show(args[0]);
}
bool createdNew;
//系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
//第二个参数可以设置为产品的名称:Application.ProductName
//每次启动应用程序,都会验证名称为Gis的互斥是否存在
Mutex mutex = new Mutex(false, "Gis", out createdNew);
//如果已运行,则在前端显示
//createdNew == false,说明程序已运行
if (!createdNew)
{
Process instance = GetExistProcess();
if (instance != null)
{
SetForegroud(instance);
Application.Exit();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
Application.Run(new Form1());
else
Application.Run(new Form1(args));
}
/// <summary>
/// 查看程序是否已经运行
/// </summary>
/// <returns></returns>
private static Process GetExistProcess()
{
Process currentProcess = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
{
if ((process.Id != currentProcess.Id) &&
(Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))
{
return process;
}
}
return null;
}
/// <summary>
/// 使程序前端显示
/// </summary>
/// <param name="instance"></param>
private static void SetForegroud(Process instance)
{
IntPtr mainFormHandle = instance.MainWindowHandle;
if (mainFormHandle != IntPtr.Zero)
{
ShowWindowAsync(mainFormHandle, 1);
SetForegroundWindow(mainFormHandle);
}
}
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
}
}
相关文章推荐
- C#创建和调用WebService
- C# 中实现快捷键的的方式
- C# 发送邮件
- .net-c# 获取机器码
- C# 连接 Exchange 发送邮件
- :c#的remoting里,CallContext.GetData获得的对象老是空的?该怎么处理
- 支付宝Payto接口的c#.net实现(二)
- 支付宝Payto接口的c#.net实现 (一)
- C#获取网页源代码
- 在C#中提取网页的超链接的方法
- 未能创建Microsoft Visual C# 2008 编译器(解决方案)
- C#语言基础
- C#委托实现工厂规则注入
- C#3.0新特性之扩展方法介绍
- C#开源资源大汇总
- C#窗体——四则运算
- 使用VS2010 C#开发ActiveX控件
- 关于Foreach是如何实现的问题
- C#中关闭第一个Form窗口,打开另一个新窗口方法
- c#网页方式获取新浪微博的微博数据