/*************************************
* CopyRight (c) edzh.com
* Date --> 2006-1-25
* Coder --> yesun
*************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Threading;
using System.Xml;
namespace TestThread
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//多线程,只允许5个线程同时进行
public static int maxThreadCount = 4;
public static int currentThreadCount = 0;
public static int intCurrentThread = 0;
public Thread[] thread = new Thread[100];
//20s判断一次当前线程数
private System.Timers.Timer runable_Timer = new System.Timers.Timer(20*1000);
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 开始抓取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStrat_Click(object sender, System.EventArgs e)
{
//正在初始化线程
for(int i=1;i<=100;i++)
{
GetArticle ga = new GetArticle();
//传入相关参数
ga.url = txtUrl.Text.Replace("@pageid",i.ToString());
//把线程加入到数组中。。
Thread th = new Thread(new ThreadStart(ga.strat));
thread[tempInt++] = th;
}
//"共"+tempInt+"个线程保存队列中,正在启动线程,请稍候";
//声明委托
runable_Timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_CheckThread);
runable_Timer.Start();
}
void timer_CheckThread(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer initTimer = (System.Timers.Timer)sender;
initTimer.Stop();
//判断当前线程数,如果不足5个,则Join新的线程
if(currentThreadCount < maxThreadCount)
{
//表示当前线程可以加入线程,使得总执行线程为5个
try
{
if(intCurrentThread <= 100 && thread[intCurrentThread] != null && thread[intCurrentThread].ThreadState == ThreadState.Unstarted)
{
thread[intCurrentThread].Priority = ThreadPriority.Lowest;
currentThreadCount++;
thread[intCurrentThread].Name = "线程#"+(intCurrentThread+1);
//this.errorMsg.Text = thread[intCurrentThread].Name+"已开始执行";
thread[intCurrentThread].Start();
//当前线程数+1
intCurrentThread++;
}
}
catch
{
}
}
initTimer.Interval = 3 * 1000;
initTimer.Start();
}
}
/// <summary>
/// 抓取文章类
/// </summary>
public class GetArticle
{
/// <summary>
/// 开始抓取数据
/// </summary>
/// <param name="url">网址</param>
/// <param name="reg">正则表达式</param>
/// <returns></returns>
public void strat()
{
//Do Something。。。
//执行完毕,减小当前线程数
//this.parentForm.currentThreadCount--;
Form1.currentThreadCount--;
//this.parentForm.errorMsg.Text = Thread.CurrentThread.Name+" 执行完毕!";
}
}
* CopyRight (c) edzh.com
* Date --> 2006-1-25
* Coder --> yesun
*************************************/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
using System.Threading;
using System.Xml;
namespace TestThread
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//多线程,只允许5个线程同时进行
public static int maxThreadCount = 4;
public static int currentThreadCount = 0;
public static int intCurrentThread = 0;
public Thread[] thread = new Thread[100];
//20s判断一次当前线程数
private System.Timers.Timer runable_Timer = new System.Timers.Timer(20*1000);
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 开始抓取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStrat_Click(object sender, System.EventArgs e)
{
//正在初始化线程
for(int i=1;i<=100;i++)
{
GetArticle ga = new GetArticle();
//传入相关参数
ga.url = txtUrl.Text.Replace("@pageid",i.ToString());
//把线程加入到数组中。。
Thread th = new Thread(new ThreadStart(ga.strat));
thread[tempInt++] = th;
}
//"共"+tempInt+"个线程保存队列中,正在启动线程,请稍候";
//声明委托
runable_Timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_CheckThread);
runable_Timer.Start();
}
void timer_CheckThread(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer initTimer = (System.Timers.Timer)sender;
initTimer.Stop();
//判断当前线程数,如果不足5个,则Join新的线程
if(currentThreadCount < maxThreadCount)
{
//表示当前线程可以加入线程,使得总执行线程为5个
try
{
if(intCurrentThread <= 100 && thread[intCurrentThread] != null && thread[intCurrentThread].ThreadState == ThreadState.Unstarted)
{
thread[intCurrentThread].Priority = ThreadPriority.Lowest;
currentThreadCount++;
thread[intCurrentThread].Name = "线程#"+(intCurrentThread+1);
//this.errorMsg.Text = thread[intCurrentThread].Name+"已开始执行";
thread[intCurrentThread].Start();
//当前线程数+1
intCurrentThread++;
}
}
catch
{
}
}
initTimer.Interval = 3 * 1000;
initTimer.Start();
}
}
/// <summary>
/// 抓取文章类
/// </summary>
public class GetArticle
{
/// <summary>
/// 开始抓取数据
/// </summary>
/// <param name="url">网址</param>
/// <param name="reg">正则表达式</param>
/// <returns></returns>
public void strat()
{
//Do Something。。。
//执行完毕,减小当前线程数
//this.parentForm.currentThreadCount--;
Form1.currentThreadCount--;
//this.parentForm.errorMsg.Text = Thread.CurrentThread.Name+" 执行完毕!";
}
}