/// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Process instance = RunningInstance(); if (instance == null) { System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.DoEvents(); Application.Run(new Login.Login()); if (staff != null) { Application.EnableVisualStyles(); Application.Run(new MainForm()); } } else { HandleRunningInstance(instance); } } /// <summary> /// 从当前进程中判断是否存在已经运行的进程 /// </summary> /// <returns></returns> public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); foreach (Process process in processes) { if (process.Id != current.Id) { 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); 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);
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Diagnostics;namespace WindowsApplication230 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [DllImport("user32")] static extern int SetForegroundWindow(IntPtr hwnd); [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Process[] PS = Process.GetProcessesByName("WindowsApplication230");
OpenMutex().....
致于调出正在运行的进程,如果有窗口还好办,如果没有窗口中,就麻烦了。。
网上有具体的例子;
//判斷互斥體是否存在。
bool Running = !mutex.WaitOne(0, false);
if (!Running)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); //默認開啟頁面
Application.Run(new frmMain());
}
else
{ MessageBox.Show("應用程序已啟動!");
}
要报错,报的错误为:在应用程序中创建第一个 IWin32Window 对象之前,必须调用 SetCompatibleTextRenderingDefault。
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{ Process instance = RunningInstance();
if (instance == null)
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.DoEvents(); Application.Run(new Login.Login());
if (staff != null)
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
else
{
HandleRunningInstance(instance);
}
} /// <summary>
/// 从当前进程中判断是否存在已经运行的进程
/// </summary>
/// <returns></returns>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
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);
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);
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;namespace WindowsApplication230
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary> [DllImport("user32")]
static extern int SetForegroundWindow(IntPtr hwnd); [STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Process[] PS = Process.GetProcessesByName("WindowsApplication230");
if (PS.Length > 1)
SetForegroundWindow(Process
.GetProcessesByName("WindowsApplication230")[1].MainWindowHandle);
else
Application.Run(new Form1());
}
}
}
在Program.cs中添加如下代码:
Boolean createdNew; //返回是否赋予了使用线程的互斥体初始所属权
System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName", out createdNew); //同步基元变量
if (createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
{
Application.Run(new Form1()); /s/这句是系统自动写的
instance.ReleaseMutex();
}
else
{
MessageBox.Show("已经启动了一个程序,请先退出!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
Application.Exit();
}
当 process.start("DoctorManage .exe");的时候
判断启动的线程 是否被调用过
如果已经存在 将已经存在的线程调出 最大或最小using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace DoctorManage
{
static class Program
{
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);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Process instance = GetRunningInstance();
if (instance == null)
{
Application.Run(new MianFrm());
}
else
{
HandleRunningInstance(instance);
} }
/// <summary>
/// 获取应用程序的实例,没有其它的例程,返回Null
/// </summary>
/// <returns>返回当前Process实例</returns>
public static Process GetRunningInstance()
{
//获取当前进程
Process currentProcess = Process.GetCurrentProcess();
string currentFileName = currentProcess.MainModule.FileName;
//创建新的 Process 组件的数组,并将它们与本地计算机上共享指定的进程名称的所有进程资源关联。
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
//遍历正在有相同名字运行的进程
foreach (Process process in processes)
{
if (process.MainModule.FileName == currentFileName)
{
if (process.Id != currentProcess.Id)//排除当前的进程
return process;//返回已启动的进程实例
}
}
return null;
}
/// <summary>
/// 获取窗口句柄
/// </summary>
/// <param name="instance">Process进程实例</param>
public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//设置真实例程为foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
}
}
1using System;
2using System.Collections.Generic;
3using System.Windows.Forms;
4using System.Diagnostics;
5using System.Runtime.InteropServices;
6
7namespace WindowsApplication2
8{
9 static class Program
10 {
11 /**//// <summary>
12 /// 应用程序的主入口点。
13 /// </summary>
14
15 [DllImport("User32.dll")]
16 private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
17 [DllImport("User32.dll")]
18 private static extern bool SetForegroundWindow(IntPtr hWnd);
19 private const int WS_SHOWNORMAL = 1;
20
21 [STAThread]
22 static void Main()
23 {
24 //得到正在运行的例程
25 Process instance = RunningInstance();
26 if (instance == null)
27 {
28 //如果没有其它例程,就新建一个窗体
29 Application.EnableVisualStyles();
30 Application.SetCompatibleTextRenderingDefault(false);
31 Application.Run(new Form1());
32 }
33 else
34 {
35 //处理发现的例程
36 HandleRunningInstance(instance);
37 }
38 }
39 /**//// <summary>
40 /// 得到正在运行的进程
41 /// </summary>
42 /// <returns></returns>
43 public static Process RunningInstance()
44 {
45 Process current = Process.GetCurrentProcess();
46 Process[] processes = Process.GetProcessesByName(current.ProcessName);
47
48 //遍历正在有相同名字运行的进程
49 foreach (Process process in processes)
50 {
51 //忽略现有的进程
52 if (process.Id != current.Id)
53 {
54 //确保进程从EXE文件运行
55 if (process.MainModule.FileName == current.MainModule.FileName)
56 {
57 // 返回另一个进程实例
58 return process;
59 }
60 }
61 }
62 //没有其它的进程,返回Null
63 return null;
64 }
65 /**//// <summary>
66 /// 处理正在运行的进程,也就是将其激活
67 /// </summary>
68 /// <param name="instance">要处理的进程</param>
69 public static void HandleRunningInstance(Process instance)
70 {
71 //确保窗口没有被最小化或最大化
72 ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
73 //设置真实进程为foreground window
74 SetForegroundWindow(instance.MainWindowHandle);
75 }
76 }
77}
returen;
else
正常启动。。
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;namespace jamonitor
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
int iProcessNum = 0; foreach (Process singleProc in Process.GetProcesses())
{
if (singleProc.ProcessName == Process.GetCurrentProcess().ProcessName)
{
iProcessNum += 1;
}
}
if (iProcessNum == 2)
{
MessageBox.Show("【系统提示】:程序已经在运行中……", "单点登陆", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (iProcessNum > 2)
{
return;
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new fmLogin());
}
}
}
}