调用2 次方式为
matchRegex(GetPageHtml(html), html);
currentlevel += 1;
foreach (string url in jybList1)
{
matchRegex(GetPageHtml(url), url);
}
老大嫌慢,让我加入多线程.但是小弟对多线程这一块知之甚少啊!!!
看了看文档, 尝试了一下 ParameterizedThreadStart ,但是无从下手啊!!
在CSDN下载了差不多20个 "多线程 请求" 这种Demo,但是感觉帮助甚少!
特此,希望大佬们给予帮助!觉得悬赏少,随时可以加好吧!!!!!
Q:284052495
我测试日志模块的例子你可以参考下
namespace Test
{
class Program
{
static void Main(string[] args)
{
for(int i = 0; i < 100; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(AutoWrite));
thread.IsBackground = true;
thread.Start(i);
}
Console.ReadKey();
}
private static void AutoWrite(object i)
{
string str = i.ToString();
while (true)
{
string content = String.Format("这是第{0}个线程的日志内容",str);
int j = int.Parse(str) / 8;
string filename = "T" + j.ToString();
LogFactory.GetInstance().WriteLog(content,filename);
Thread.Sleep(20);
}
}
}
}
设置一个全局的Thread的List,用来存放创建的线程,另外创建个线程安全的列表存放url
然后你main或者按钮事件啥的,就for创建线程,再加进上面的Thread的List
System.Collections.Concurrent.ConcurrentQueue workList = new System.Collections.Concurrent.ConcurrentQueue();
//导入数据用workList.Enqueue(url);
List threadList=new List();
for(int i = 0; i < n; i++)
{
Thread t = new Thread(ThreadMethod);
threadList.Add(t);
t.Start();
void ThreadMethod(){
string url;
workList.TryDequeue(out url);//取一条URL
剩下就是调用你的方法了
}
关闭程序的时候,记得把所有的线程都停掉,否则会一直不停的
比如在窗体的关闭事件里加上
private void DisposeThread()
{
if (threadList != null)
{
for (int i = threadList.Count-1; i >=0; i--)
{
if(threadList[i]!=null)
{
threadList[i].Abort();
threadList[i] = null;
}
threadList.RemoveAt(i);
}
}
}
。。。。没加代码模式,尖括号里的所有内容都被删了,坑