protected void InitComponent() { Scheduler.Init(this); if (Downloader == null) { Downloader = new HttpClientDownloader(); } Downloader.SetThreadNum(ThreadNum); if (Pipelines.Count == 0) { Pipelines.Add(new FilePipeline()); } if (ThreadPool == null || ThreadPool.IsShutdown) { ThreadPool = new CountableThreadPool(ThreadNum); } if (StartRequests != null) { Parallel.ForEach(StartRequests, new ParallelOptions() { MaxDegreeOfParallelism = 100 }, request => { Scheduler.Push((Request)request.Clone(), this); }); ClearStartRequests(); Logger.InfoFormat("Push Request to Scheduler success."); } if (!_registConsoleCtrlHandler) { Console.Title = Identify; Console.CancelKeyPress += Console_CancelKeyPress; _registConsoleCtrlHandler = true; //根据控制台标题找控制台 int windowHandler = FindWindow(null, Identify); //找关闭按钮 IntPtr closeMenu = GetSystemMenu((IntPtr)windowHandler, IntPtr.Zero); int SC_CLOSE = 0xF060; //关闭按钮禁用 RemoveMenu(closeMenu, SC_CLOSE, 0x0); } }
public void InitComponent() { if (_init) { #if NET_CORE Logger.Info($"Component already init.", true); #else Logger.Info("Component already init."); #endif return; } Console.CancelKeyPress += ConsoleCancelKeyPress; Scheduler.Init(this); if (Downloader == null) { //Downloader = new HttpClientDownloader(); } Downloader.ThreadNum = ThreadNum; if (Pipelines.Count == 0) { Pipelines.Add(new FilePipeline()); } if (ThreadPool == null) { ThreadPool = new CountableThreadPool(ThreadNum); } if (StartRequests != null) { if (StartRequests.Count > 0) { Parallel.ForEach(StartRequests, new ParallelOptions() { MaxDegreeOfParallelism = 100 }, request => { Scheduler.Push((Request)request.Clone(), this); }); ClearStartRequests(); #if NET_CORE Logger.Info("Push Request to Scheduler success.", true); #else Logger.Info("Push Request to Scheduler success."); #endif } else { #if NET_CORE Logger.Info("Push Zero Request to Scheduler.", true); #else Logger.Info("Push Request to Scheduler success."); #endif } } Task.Factory.StartNew(() => { if (ShowConsoleStatus) { IMonitorableScheduler monitor = Scheduler as IMonitorableScheduler; if (monitor != null) { while (true) { try { if (Stat == Status.Running && !_waitingToExit) { Console.WriteLine( $"Left: {monitor.GetLeftRequestsCount(this)} Total: {monitor.GetTotalRequestsCount(this)} AliveThread: {ThreadPool.ThreadAlive} ThreadNum: {ThreadPool.ThreadNum}"); } } catch { // ignored } Thread.Sleep(2000); } } } }); _init = true; }