private void mutexHandleCloseTimer_Tick(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("WeChat");
            Console.WriteLine("WeChat进程数:" + processes.Length);
            // 添加新进程
            foreach (Process p in processes)
            {
                int i = 0;
                for (i = 0; i < wechatProcesses.Count; i++)
                {
                    WechatProcess wechatProcess = wechatProcesses[i];
                    if (wechatProcess.Proc.Id == p.Id)
                    {
                        break;
                    }
                }
                if (i == wechatProcesses.Count)
                {
                    wechatProcesses.Add(new WechatProcess(p));
                }
            }
            // 关闭所有存在互斥句柄的进程
            int num = 0;

            for (int i = wechatProcesses.Count - 1; i >= 0; i--)
            {
                WechatProcess wechatProcess = wechatProcesses[i];
                if (!wechatProcess.MutexClosed)
                {
                    wechatProcess.MutexClosed = ProcessUtil.CloseMutexHandle(wechatProcess.Proc);
                    Console.WriteLine("进程:" + wechatProcess.Proc.Id + ",关闭互斥句柄:" + wechatProcess.MutexClosed);
                }
                else
                {
                    if (wechatProcess.Proc.HasExited)
                    {
                        // 移除不存在的线程
                        wechatProcesses.RemoveAt(i);
                    }
                    else
                    {
                        num++;
                    }
                }
            }
            lblProcNum.Text = "当前微信数量:" + num.ToString();
        }
Example #2
0
 private void btnStart_Click(object sender, EventArgs e)
 {
     if (File.Exists(txtPath.Text))
     {
         Process[] processes = Process.GetProcessesByName("WeChat");
         ProcessUtil.CloseMutexHandle(processes);
         // 启动多个实例
         for (int i = 0; i < startNum.Value; i++)
         {
             //var t = new Task(() =>
             //{
             //    Process newInstance = Process.Start(txtPath.Text);
             //    newInstance.WaitForInputIdle();
             //    ProcessUtil.CloseMutexHandle(newInstance);
             //});
             //t.Start();
             Process newInstance = Process.Start(txtPath.Text);
             //newInstance.WaitForInputIdle();
             //ProcessUtil.CloseMutexHandle(newInstance);
         }
     }
 }
 private void btnCloseAllMutex_Click(object sender, EventArgs e)
 {
     Process[] processes = Process.GetProcessesByName("WeChat");
     ProcessUtil.CloseMutexHandle(processes);
 }