如何使用C#运行多个可执行文件实例

时间:2017-02-27 20:23:50

标签: c# multithreading backgroundworker

我一直在努力寻找一种更好的方法来运行用C#.NET编写的EXE的多个实例。

我已经有了一个正常工作的代码,但它的应用程序需要改进。 这是我到目前为止所拥有的。

public void startProcess(int Threads)
        {
            Random random = new Random();
            Thread.Sleep(random.Next(200, 800)); 
            for (int i = 1; i <= Threads; i++)
            {                  
                //Proxy Settings Goes There.......
                Client.find_super_proxy();
                string proxy = Client.super_proxy_ip;
                if (proxy.Length <= 8)
                {
                    Client clnt = new Client();
                    clnt.logout();
                    clnt.login();
                    Client.find_super_proxy();
                    proxy = Client.super_proxy_ip;
                }

                ProcessStartInfo info = new ProcessStartInfo(Path.Combine(Application.StartupPath, "Browse.exe"))
                {
                    Arguments = string.Concat(new object[] { proxy.ToString(), " ", HttpUtility.UrlEncode(txtURL.Text), " ", this.nuInner.Value, " ", random.Next((int)this.nuDelay.Value, (int)this.nuDelay1.Value), " ", this.nuTimeOut.Value, " ", HttpUtility.UrlEncode(userAgent), " ", HttpUtility.UrlEncode(referrer) })
                };
                Process process2 = new Process
                {
                    StartInfo = info,
                    EnableRaisingEvents = true
                };
                process2.Start();
            }

        }

然后我在计时器中调用这个方法。

    private void timer2_Tick(object sender, EventArgs e)
    {
        runningthreads = Process.GetProcessesByName("Browse").Length;

        if ( runningthreads < (int)nuThreads.Value )
        {
            int newThread =  (int)nuThreads.Value - runningthreads;
            Random random = new Random();
            Thread.Sleep(random.Next(200, 800));
            startProcess(newThread);

        }

    }

这段代码搞砸了我一直在考虑在BackgroundWorker中使用它的整个程序,并做了一些改进。

0 个答案:

没有答案