c#只允许运行一个程序(转载)
2010-01-24 17:59
302 查看
******************************************
* ****************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
namespace Jackch
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
///
//只允许运行一个程序
[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;
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//First f = new First();
//f.Show();
Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
MessageBox.Show("你已经运行了该程序!");
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);
}
}
}
* ****************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
namespace Jackch
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
///
//只允许运行一个程序
[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;
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//First f = new First();
//f.Show();
Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
MessageBox.Show("你已经运行了该程序!");
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);
}
}
}
相关文章推荐
- C#程序只允许运行一个实例的解决方案
- 实现同时只允许运行一个程序实例
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 实现同时只允许运行一个程序实例
- C# 程序只允许运行一个实例的正确代码
- C#程序只允许运行一个实例的解决方案
- 仅允许程序运行一个实例代码实现
- C#_WinForm程序只允许运行一个实例
- 一种只允许程序的一个实例运行的方法
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- [转]实现同时只允许运行一个程序实例
- Wince 程序只允许同时运行一个
- 解决C#程序只允许运行一个实例的几种方法详解
- delphi程序只允许运行一个实例的三种方法
- 用C#给程序加启动画面并只允许一个应用程序实例运行
- 只允许程序运行一个实例
- 【转】delphi程序只允许运行一个实例的三种方法:
- C#程序只允许运行一个实例的解决方案
- 允许一个程序在widnow上运行
- 转载:C#只允许运行应用程序的一个实例的正确实现方法