只能运行一个实例的方法
对于系统:
1、
static void Main()
{
bool initiallyOwned = true;
bool isCreated;
Mutex m = new Mutex(initiallyOwned,"MyTest",out isCreated);
if (!(initiallyOwned && isCreated))
{
MessageBox.Show("已经有相同的实例在运行。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
else
{
Application.Run(new MainForm());
}
{
bool initiallyOwned = true;
bool isCreated;
Mutex m = new Mutex(initiallyOwned,"MyTest",out isCreated);
if (!(initiallyOwned && isCreated))
{
MessageBox.Show("已经有相同的实例在运行。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
Application.Exit();
}
else
{
Application.Run(new MainForm());
}
2、
static void Main()
{
int ProceedingCount = 0;
Process[] ProceddingCon = Process.GetProcesses();
foreach(Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)
{
ProceedingCount += 1;
}
}
if(ProceedingCount > 1)
{
MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new MainForm());
}
}
{
int ProceedingCount = 0;
Process[] ProceddingCon = Process.GetProcesses();
foreach(Process IsProcedding in ProceddingCon)
{
if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName)
{
ProceedingCount += 1;
}
}
if(ProceedingCount > 1)
{
MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new MainForm());
}
}
3、
private static bool CheckInstance()
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
{
return true;
}
else
{
return false;
}
}
{
string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
{
return true;
}
else
{
return false;
}
}
4、
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace PCBilling_Server
{
static class Program
{
public static bool bOnlyOneInstance = false;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bOnlyOneInstance = false;
Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace(@"\", "_"), out bOnlyOneInstance);
if (!bOnlyOneInstance)
{
MessageBox.Show("系统已经运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Environment.Exit(0);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Frm_Login());
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace PCBilling_Server
{
static class Program
{
public static bool bOnlyOneInstance = false;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bOnlyOneInstance = false;
Mutex mutex = new Mutex(true, Application.UserAppDataPath.Replace(@"\", "_"), out bOnlyOneInstance);
if (!bOnlyOneInstance)
{
MessageBox.Show("系统已经运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
System.Environment.Exit(0);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Frm_Login());
}
}
}
5、
原文:http://topic.csdn.net/t/20040412/14/2955011.html
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
public class MainForm : System.Windows.Forms.Form
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = GetRunningInstance();
if(instance == null)
{
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns></returns>
public static Process GetRunningInstance()
{
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;
}
return null;
}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance"></param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
public class MainForm : System.Windows.Forms.Form
{
private const int WS_SHOWNORMAL = 1;
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
public MainForm()
{
InitializeComponent();
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = GetRunningInstance();
if(instance == null)
{
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
}
}
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns></returns>
public static Process GetRunningInstance()
{
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;
}
return null;
}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance"></param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
对于窗体:
#region 不重复打开MDI子窗体
/// <summary>
/// 不重复打开MDI子窗体
/// 调用:this.OpenMDIWindow(typeof(窗体名).ToString());
/// </summary>
/// <param name="ChildTypeString">窗体名称(typeof(FormName).ToString())</param>
private void OpenMDIWindow(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current process assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(ChildTypeString);
// Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
// Show child form
if (obj != null)
{
this.Refresh();
//Form frmTip = LoadTip.ShowTipForm(this, "正在加载窗体");
myChild = obj as Form;
myChild.MdiParent = this;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus();
//frmTip.Close();
}
}
}
/// <summary>
/// Search mdi child form by specific type string
/// </summary>
/// <param name="ChildTypeString"></param>
/// <returns></returns>
private bool ContainMDIChild(string ChildTypeString)
{
Form myMDIChild = null;
foreach (Form f in this.MdiChildren)
{
if (f.GetType().ToString() == ChildTypeString)
{
// found it
myMDIChild = f;
break;
}
}
// Show the exist form
if (myMDIChild != null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
{
return false;
}
}
#endregion
/// <summary>
/// 不重复打开MDI子窗体
/// 调用:this.OpenMDIWindow(typeof(窗体名).ToString());
/// </summary>
/// <param name="ChildTypeString">窗体名称(typeof(FormName).ToString())</param>
private void OpenMDIWindow(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current process assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(ChildTypeString);
// Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
// Show child form
if (obj != null)
{
this.Refresh();
//Form frmTip = LoadTip.ShowTipForm(this, "正在加载窗体");
myChild = obj as Form;
myChild.MdiParent = this;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus();
//frmTip.Close();
}
}
}
/// <summary>
/// Search mdi child form by specific type string
/// </summary>
/// <param name="ChildTypeString"></param>
/// <returns></returns>
private bool ContainMDIChild(string ChildTypeString)
{
Form myMDIChild = null;
foreach (Form f in this.MdiChildren)
{
if (f.GetType().ToString() == ChildTypeString)
{
// found it
myMDIChild = f;
break;
}
}
// Show the exist form
if (myMDIChild != null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
{
return false;
}
}
#endregion
调用:
this.OpenMDIWindow(typeof(窗体名).ToString());