/// <summary> /// 执行停止前的操作 /// </summary> private void ExecStopping(object manual) { //执行停止之前的操作 this.BeforeStopped((bool)manual); this.runStatus = SpiderRunStatus.Stopped; //销毁线程 this.DestroyThreads(this.requestThreads); this.DestroyThreads(this.processThreads); SetConsoleTitle(); //执行停止之后的操作 this.AfterStopped(); }
/// <summary> /// 构造函数 /// </summary> private SpiderBase() { this.urlQueue = new UrlQueue(); this.contentQueue = new ContentQueue(); this.requestThreads = new List<Thread>(); this.processThreads = new List<Thread>(); this.runStatus = SpiderRunStatus.UnStarted; this.pauseCalled = false; this.completed = false; this.startTimespan = new TimeSpan(); this.startTime = DateTime.MinValue; this.runtime = new SpiderRuntime(); }
private SpiderRuntime runtime; //运行时信息 #endregion #region constructors /// <summary> /// 构造函数 /// </summary> private SpiderBase() { this.urlQueue = new UrlQueue(); this.contentQueue = new ContentQueue(); this.requestThreads = new List <Thread>(); this.processThreads = new List <Thread>(); this.runStatus = SpiderRunStatus.UnStarted; this.pauseCalled = false; this.completed = false; this.startTimespan = new TimeSpan(); this.startTime = DateTime.MinValue; this.runtime = new SpiderRuntime(); }
/// <summary> /// 启动爬虫 /// </summary> public void Start() { if (this.runStatus != SpiderRunStatus.UnStarted) { Console.WriteLine("Spider was started,and you can not restart it again!"); return; } if (Environment.UserInteractive) { Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("##########################################################################"); Console.WriteLine("## ##"); Console.WriteLine("## SpiderLib, Author:[email protected] ##"); string _title = this.settings.Name; if (_title.Length > 60) { _title = _title.Substring(0, 60) + "......"; } int _len = _title.Length; if (_len % 2 == 0) { _len += (70 - _len) / 2; } else { _len += (70 - _len - 1) / 2; } _title = _title.PadLeft(_len, ' ').PadRight(70, ' '); Console.WriteLine("##{0}##", _title); Console.WriteLine("## ##"); Console.WriteLine("##########################################################################"); Console.WriteLine(""); Console.ResetColor(); } //执行开始之前的操作 this.BeforeStarting(); if (null != this.settings.StartUrl) { this.urlQueue.Add(this.settings.StartUrl); } Thread initThread = new Thread(new ThreadStart(this.urlManager.Init)); initThread.Start(); this.runStatus = SpiderRunStatus.Initializing; //执行初始化阶段的操作 this.OnInitializing(); //等待初始化线程完成 while (!initThread.IsAlive) ; initThread.Join(); //执行初始化完成后的操作 this.OnInitialized(); //Manage Thread this.managerThread = new Thread(new ThreadStart(this.ManageThreads)); this.managerThread.IsBackground = true; this.managerThread.Name = "ManagerThread"; this.runStatus = SpiderRunStatus.Running; this.managerThread.Start(); //阻塞,等待ManagerThread启动 while (!this.managerThread.IsAlive) ; //开始时刻 this.startTime = DateTime.Now; this.startTimespan = new TimeSpan(this.startTime.Ticks); //执行启动时刻的操作 this.OnStarted(); }
/// <summary> /// 启动爬虫 /// </summary> public void Start() { if (this.runStatus != SpiderRunStatus.UnStarted) { Console.WriteLine("Spider was started,and you can not restart it again!"); return; } if (Environment.UserInteractive) { Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("##########################################################################"); Console.WriteLine("## ##"); Console.WriteLine("## SpiderLib, Author:[email protected] ##"); string _title = this.settings.Name; if (_title.Length > 60) { _title = _title.Substring(0, 60) + "......"; } int _len = _title.Length; if (_len % 2 == 0) { _len += (70 - _len) / 2; } else { _len += (70 - _len - 1) / 2; } _title = _title.PadLeft(_len, ' ').PadRight(70, ' '); Console.WriteLine("##{0}##", _title); Console.WriteLine("## ##"); Console.WriteLine("##########################################################################"); Console.WriteLine(""); Console.ResetColor(); } //执行开始之前的操作 this.BeforeStarting(); if (null != this.settings.StartUrl) { this.urlQueue.Add(this.settings.StartUrl); } Thread initThread = new Thread(new ThreadStart(this.urlManager.Init)); initThread.Start(); this.runStatus = SpiderRunStatus.Initializing; //执行初始化阶段的操作 this.OnInitializing(); //等待初始化线程完成 while (!initThread.IsAlive) { ; } initThread.Join(); //执行初始化完成后的操作 this.OnInitialized(); //Manage Thread this.managerThread = new Thread(new ThreadStart(this.ManageThreads)); this.managerThread.IsBackground = true; this.managerThread.Name = "ManagerThread"; this.runStatus = SpiderRunStatus.Running; this.managerThread.Start(); //阻塞,等待ManagerThread启动 while (!this.managerThread.IsAlive) { ; } //开始时刻 this.startTime = DateTime.Now; this.startTimespan = new TimeSpan(this.startTime.Ticks); //执行启动时刻的操作 this.OnStarted(); }