using System; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Diagnostics; using System.Reflection; public class OneInstnace { [STAThread] public static void Main() { //Get the running instance. Process instance = RunningInstance(); if (instance == null) { //There isn 't another instance, show our form. Application.Run (new Form()); } else { //There is another instance of this process. HandleRunningInstance(instance); } } public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name foreach (Process process in processes) { //Ignore the current process if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace( "/ ", "\\ ") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } public static void HandleRunningInstance(Process instance) { //Make sure the window is not minimized or maximized ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //Set the real intance to 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; }
或者 // Refer: [C#]public Mutex(bool initiallyOwned, string name, // out bool createdNew) static void Main() { /* Assure single instance: */ bool isFirst; System.Threading.Mutex mtx = new System.Threading.Mutex (false, "mango.2004-01-14 ", out isFirst); if (isFirst) { Application.Run(new Form1()); } }
tryProcess[] pary = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); (Application_ThreadException); if (pary.Length > 1) { MessageBox.Show("已经有相同的程序在运行!"); return; }
using System.Diagnostics;
using System.Diagnostics;Process[] pary = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); if (pary.Length > 1) { MessageBox.Show("已经有相同的程序在运行!"); return; }
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection; public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn 't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace( "/ ", "\\ ") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to 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;
}
// Refer: [C#]public Mutex(bool initiallyOwned, string name,
// out bool createdNew)
static void Main()
{
/* Assure single instance: */
bool isFirst;
System.Threading.Mutex mtx = new System.Threading.Mutex
(false, "mango.2004-01-14 ", out isFirst);
if (isFirst)
{
Application.Run(new Form1());
}
}
process[] 获得所有进程
进程名相等`````则messboxshow我都不好意思 发出来
还有没有别的办法?
例如:
在program里还没run的时候,搜索所有的激活状态窗口,找到自己那个了就给他发消息,让那个窗口弹个框"老子我已经打开了"然后关闭程序。要是找不到就说明没有,那就启动呗。 可以监听一个本地网络端口,然后开启的时候再去监听,会得到已经被占用,然后用client端发消息问:你是我自己嘛? 你自己的server就会:是。 然后关闭。
当然如果你监听成功了,那就开启呗。 只看你敢不敢想。
(Application_ThreadException); if (pary.Length > 1)
{
MessageBox.Show("已经有相同的程序在运行!");
return;
}
if (pary.Length > 1)
{
MessageBox.Show("已经有相同的程序在运行!");
return;
}
/// <summary>
/// 单实例设置
/// </summary>
private void MutexRun()
{
bool bRun;
m_mt = new Mutex(true, "TestRun", out bRun);
if (!bRun)
{
IntPtr Hander = CommonGenerator.FindWindow(null, "窗口标题");
if (Hander != IntPtr.Zero)
{
// 在这里提示用户,同时将唯一的窗口置为最前方
CommonGenerator.SetForegroundWindow(Hander);
}
m_mt.Close();
Environment.Exit(1);
return;
}
}
加上是不是一种方式呢
Process[] pary = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)一种思路罢了,不是简单的互斥可以解决的
m_mt = new Mutex(true, "Global\\TestRun", out bRun);
if (!bRun)
/// <summary>
/// 检测程序是否已经运行
/// </summary>
/// <returns>bool true:未运行 false:已经运行</returns>
static bool IsRun()
{
Process[] prs = Process.GetProcesses();
Process thisone = Process.GetCurrentProcess();
foreach (Process pr in prs)
{
if (thisone.ProcessName == pr.ProcessName && thisone.Id != pr.Id)
{
return false;
}
}
return true;
}
这样不会的
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
mutex = new Mutex(true, "hygl");
if (mutex.WaitOne(0, false))
{
}
else
{
MessageBox.Show("程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
}
创建一个全局的mutex,就可以解决多用户登录的问题。
前缀 "Global\\"
private static System.Threading.Mutex _mutex;
[STAThread]
static void Main()
{
_mutex = new System.Threading.Mutex(false, "Global\\MyApplication");
//Mutex所有权取得
if (_mutex.WaitOne(0, false) == false)
{
MessageBox.Show("应用程序已经启动过了。");
return;
}
Application.Run(new Form1());
}
哦!gomoku 已经说了~
[STAThread]
static void Main()
{
_mutex = new System.Threading.Mutex(false, "Global\\MyApplication");
//Mutex所有权取得
if (_mutex.WaitOne(0, false) == false)
{
MessageBox.Show("应用程序已经启动过了。");
return;
}
Application.Run(new Form1());
}
System.Threading.Mutex run = new System.Threading.Mutex(true, Environment.UserName+"single84123131", out runone);
if (runone)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); run.ReleaseMutex(); //关键
}
else
{
//MessageBox.Show("已经运行一个实例!"); }