110,536
社区成员
发帖
与我相关
我的任务
分享
private void Form1_Load(object sender, EventArgs e)
{
Process[] ProcessColection = Process.GetProcessesByName(Application.CompanyName);
if (ProcessColection.Length >= 1)
{
MessageBox.Show("程序已经运行!");
Thread.Sleep(1000);
System.Environment.Exit(1);
}
}
static bool AnotherIsRunning()
{
bool flag;
var mutex = new Mutex(true, "abc", out flag);
return !flag;
}
返回true表示有正在运行的程序否则是没有 private void Form1_Load(object sender, EventArgs e)
{
Process[] ProcessColection = Process.GetProcessesByName(Application.CompanyName);
if (ProcessColection.Length >= 1)
{
...
}
}
方法应该很多,不过我个人认为:通过获取进程名的方式判断是比较笨的一种,这么做比较复杂,开发成本较高
通常我是这样做的 :程序运行后,创建一个.pid文件,并锁住这个文件,如果失败了,就提示说进程在运行。
这样不行吧,如果我把程序分别放在两个目录.就可以再次创建文件了.因为文件不存在应该是要新建的嘛.还是判断进程实例比较好.具体 方法度娘那大把ProcessColection.Length > 1
#region 检测当前窗体是否处于运行状态
private static Mutex mutex = null;
/// <summary>
/// 检测当前窗体是否处于运行状态
/// </summary>
private static void GlobalMutex()
{
// 是否第一次创建mutex
bool newMutexCreated = false;
string mutexName = "Global\\" + "Allen.Tool";//系统名称,Global为全局,表示即使通过通过虚拟桌面连接过来,也只是允许运行一次
try
{
mutex = new Mutex(false, mutexName, out newMutexCreated);
}
catch (Exception ex)
{
Console.Write(ex.Message);
System.Threading.Thread.Sleep(1000);
Environment.Exit(1);
}
// 第一次创建mutex
if (newMutexCreated)
{
Console.WriteLine("程序已启动");
//todo:此处为要执行的任务
}
else
{
MessageBox.Show("检测到另一个窗口已处于运行状态,不能重复运行。", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Threading.Thread.Sleep(1000);
Environment.Exit(1);//退出程序
}
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
Process[] ProcessColection = Process.GetProcessesByName(Application.CompanyName);
if (ProcessColection.Length >= 1)
{
...
}
}
方法应该很多,不过我个人认为:通过获取进程名的方式判断是比较笨的一种,这么做比较复杂,开发成本较高
通常我是这样做的 :程序运行后,创建一个.pid文件,并锁住这个文件,如果失败了,就提示说进程在运行。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool createdNew;
//系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次
//第二个参数可以设置为产品的名称:Application.ProductName
//每次启动应用程序,都会验证名称为SingletonWinAppMutex的互斥是否存在
Mutex mutex = new Mutex(false, "SingletonWinAppMutex", out createdNew);
//如果已运行,则在前端显示
//createdNew == false,说明程序已运行
if (!createdNew)
{
Process instance = GetExistProcess();
if (instance != null)
{
SetForegroud(instance);
Application.Exit();
return;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
/// <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;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
namespace TestProcessCount
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
int processCount = 0;
Process[] pa = Process.GetProcesses();//获取当前进程数组。
foreach (Process PTest in pa)
{
if (PTest.ProcessName == Process.GetCurrentProcess().ProcessName)
{
processCount += 1;
}
}
if (processCount > 1)
{
//如果程序已经运行,则给出提示。并退出本进程。
DialogResult dr;
dr = MessageBox.Show( Process.GetCurrentProcess().ProcessName+"程序已经在运行!", "退出程序", MessageBoxButtons.OK, MessageBoxIcon.Error);
//可能你不需要弹出窗口,在这里可以屏蔽掉
return; //Exit;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmBrowser());
}
}
}