110,536
社区成员
发帖
与我相关
我的任务
分享
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());
}
}
}
}
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}
当 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);
}
}
}