C#如何判断程序已经启动
2009-09-01 12:01
666 查看
一、
class MutexExample
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool flag=false;
System.Threading.Mutex mutex=new System.Threading.Mutex(true,"MutexExample",out flag);
if(flag) {
Console.Write("Running");
}
else {
Console.Write("Another is Running");
}
Console.ReadLine();
}
二、
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ServerEpos
{
public class DuplexCheck
{
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
private static extern int ShowWindow(
System.IntPtr hWnd,
int nCmdShow
);
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
private static extern bool SetForegroundWindow(
System.IntPtr hWnd
);
private static System.Threading.Mutex mutexObject;
private const int SW_NORMAL = 1;
private DuplexCheck() { }
/// <summary>
/// 已经打开的线程判断
/// </summary>
public static bool IsOpenMutex()
{
OperatingSystem os = Environment.OSVersion;
string appName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
// 你可以开始不同类型的产品,在相同的程序。
//appName = appName + "_" + Emico.Common.Gadget.ProductInfo.GetProductType();
//if ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 5))
//{
// // 在Win2000或者以后
// // 不是一台机器,和桌面独家控制。
// appName = @"Local\" + appName;
// //appName = @"Global\" + appName;
//}
try
{
// 生成一个互斥
mutexObject = new System.Threading.Mutex(false, appName);
}
catch (System.ApplicationException)
{
// 多推出全球互斥
ShowPrevProcess();
return true;
}
if (mutexObject.WaitOne(0, false) == false)
{
// 当你不能得到一个互斥
// 多重启动
mutexObject.Close();
ShowPrevProcess();
return true;
}
return false;
}
/// <summary>
/// 释放互斥。
/// </summary>
public static void ReleaseMutex()
{
mutexObject.ReleaseMutex();
mutexObject.Close();
}
/// <summary>具有相同名称运行的进程,以激活主窗口。</summary>
/// <returns>如果已经运行如此。否则,假的。</returns>
public static bool ShowPrevProcess()
{
Process hThisProcess = Process.GetCurrentProcess();
Process[] hProcesses = Process.GetProcessesByName(hThisProcess.ProcessName);
int iThisProcessId = hThisProcess.Id;
foreach (Process hProcess in hProcesses)
{
if (hProcess.Id != iThisProcessId)
{
ShowWindow(hProcess.MainWindowHandle, SW_NORMAL);
SetForegroundWindow(hProcess.MainWindowHandle);
return true;
}
}
return false;
}
}
}
class MutexExample
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool flag=false;
System.Threading.Mutex mutex=new System.Threading.Mutex(true,"MutexExample",out flag);
if(flag) {
Console.Write("Running");
}
else {
Console.Write("Another is Running");
}
Console.ReadLine();
}
二、
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ServerEpos
{
public class DuplexCheck
{
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
private static extern int ShowWindow(
System.IntPtr hWnd,
int nCmdShow
);
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
private static extern bool SetForegroundWindow(
System.IntPtr hWnd
);
private static System.Threading.Mutex mutexObject;
private const int SW_NORMAL = 1;
private DuplexCheck() { }
/// <summary>
/// 已经打开的线程判断
/// </summary>
public static bool IsOpenMutex()
{
OperatingSystem os = Environment.OSVersion;
string appName = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
// 你可以开始不同类型的产品,在相同的程序。
//appName = appName + "_" + Emico.Common.Gadget.ProductInfo.GetProductType();
//if ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 5))
//{
// // 在Win2000或者以后
// // 不是一台机器,和桌面独家控制。
// appName = @"Local\" + appName;
// //appName = @"Global\" + appName;
//}
try
{
// 生成一个互斥
mutexObject = new System.Threading.Mutex(false, appName);
}
catch (System.ApplicationException)
{
// 多推出全球互斥
ShowPrevProcess();
return true;
}
if (mutexObject.WaitOne(0, false) == false)
{
// 当你不能得到一个互斥
// 多重启动
mutexObject.Close();
ShowPrevProcess();
return true;
}
return false;
}
/// <summary>
/// 释放互斥。
/// </summary>
public static void ReleaseMutex()
{
mutexObject.ReleaseMutex();
mutexObject.Close();
}
/// <summary>具有相同名称运行的进程,以激活主窗口。</summary>
/// <returns>如果已经运行如此。否则,假的。</returns>
public static bool ShowPrevProcess()
{
Process hThisProcess = Process.GetCurrentProcess();
Process[] hProcesses = Process.GetProcessesByName(hThisProcess.ProcessName);
int iThisProcessId = hThisProcess.Id;
foreach (Process hProcess in hProcesses)
{
if (hProcess.Id != iThisProcessId)
{
ShowWindow(hProcess.MainWindowHandle, SW_NORMAL);
SetForegroundWindow(hProcess.MainWindowHandle);
return true;
}
}
return false;
}
}
}
相关文章推荐
- Control的Invoke和BeginInvoke C#如何判断程序已经启动
- c# WinFo判断当前程序是否已经启动或存在的几种方式
- 如何判断程序已经启动?
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)
- c#读取进程列表判断程序是否已经启动(转)
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- c#读取进程列表判断程序是否已经启动(转)
- C#如何判断我的程序已经有一个实例正在运行
- c# 程序如何判断自身已经混淆
- C# WinForm 判断程序是否已经在运行,且只允许运行一个实例,附源码
- 如何在C#中启动一个外部程序?
- c#判断数据库服务器是否已经启动的方法
- 两个小知识:C#如何设置开机启动时自动执行程序|C# WinForm打开超链接
- 程序A启动了程序B,并判断程序B是否已经退出
- 使用C#判断数据库服务器是否已经启动
- 如何判断某个事件已经绑定了某个事件处理程序?
- C#调用Exe文件的方法及如何判断程序调用的exe已结束
- 使用C#判断数据库服务器是否已经启动
- .net中判断该应用程序是否已经启动,防止重复启动,监控程序启动是否正常