12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Reflection;
- using System.Diagnostics;
- using System.Threading;
- namespace TaskbarGroups
- {
- static class Program
- {
- /// <summary>
- /// 全局窗体List,便于相互跳转
- /// </summary>
- public static List<Form> formList = new List<Form>();
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- static void Main()
- {
- // 只运行一个实例
- Process instance = RunningInstance();
- if ( instance ==null){
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new frmMain());
- }else
- {
- // todo 已启动则打开form
- //Application.EnableVisualStyles();
- }
- }
- /// <summary>
- /// 检测是否已经启动了一个Application实例
- /// </summary>
- /// <returns>返回进程id,没有启动返回null</returns>
- private static Process RunningInstance()
- {
- 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;
- }
- }
- }
- // 没有其它的例程,返回null
- return null;
- }
- }
- }
|