using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp { class Program { [DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)] public static extern Int32 GetCurrentWin32ThreadID(); static void Main(string[] args) { int maxConnectionCount = 1; string[] urlarr = File.ReadAllLines(@"D:sitecheckurllist.txt"); int num = urlarr.Length; var SiteCheck = new SiteCheck(); SiteCheck.Run(urlarr, num, maxConnectionCount); } } public class SiteCheck { Random rand = new Random(); Queue<int> queueUrl = new Queue<int>(); int EachCount = 1; int Cur = 0; int LoadData(int Max) { if (Cur >= Max) { //when Cur>=Max,return Console.WriteLine("Reached max value!"); return -1; } Console.WriteLine("Loading data..."); for (int i = Cur; i < EachCount + Cur; i++) { Thread.Sleep(10); Console.Write(i + " "); //insert data into Queue<Book> queueUrl.Enqueue(i); } Console.WriteLine(""); Cur += EachCount; Console.WriteLine("Data loaded successful,{0}...", queueUrl.Count); return Cur; } public void Run(string[] urlarr, int Max, int maxConnectionCount) { int cur = LoadData(Max); Process process = new Process(); Console.WriteLine("Tasks have started..."); do { if (cur == -1) { break; } else { cur = queueUrl.Count; } Task[] tasks = new Task[maxConnectionCount]; Stopwatch sw = new Stopwatch(); sw.Start(); LogHelper.WriteLog(typeof(Program), string.Format("StartTaskTime taskCount{0}:", maxConnectionCount) + DateTime.Now.ToString()); for (int i = 0; i < maxConnectionCount; i++) { process = new Process(); tasks[i] = Task.Factory.StartNew(() => { process.StartInfo.FileName = string.Format(@"D:siteCheckpublish{0}lphash.exe", i); //准备启动lphash process.StartInfo.Arguments = urlarr[cur - maxConnectionCount + i]; process.Start(); process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; }); Thread.Sleep(500); } process.WaitForExit(); sw.Stop(); LogHelper.WriteLog(typeof(Program), string.Format("endTaskTime taskCount{0}:", maxConnectionCount) + DateTime.Now.ToString()); cur =LoadData(Max); } while (queueUrl.Count > 0); //when Cur>=Max,break the while loop Console.WriteLine("All tasks have completed."); Console.ReadKey(); } } }