using System;
using System.Threading;
using System.Data;
class Test
{
// Create a new Mutex. The creating thread does not own the
// Mutex.
private static Mutex mut = new Mutex();
DataTable ds=new DataTable();
static void Main()
{
// Create the threads that will use the protected resource.
for(int i = 0; i < 100; i++)
{
http dd = new http();
//this.chuli = true; dd.DownFinished += new http.DownFinishedEventHandler(CleanAndFillData2); dd.DownFinished += new http.DownFinishedEventHandler(FillData); dd.URL = "http://www.psdncom/odds.aspx?page=" +i.ToString();
try
{
Thread t = new Thread(new ThreadStart(dd.Getit));
t.Start();
Thread.Sleep(1000);
}
catch
{ }
}
// The main thread exits, but the application continues to
// run until all foreground threads have exited.
}
public void FillData(string url, string tempcontent)
{
//这里出问题,提示:由于出现被放弃的 mutex,等待过程结束。
mut.WaitOne();
try
{
lock (this.ds)
{
...... this.ds.Rows.Add(row1.ItemArray);
}
}
catch (Exception ex)
{
Wlogini(ex.ToString());
}
mut.ReleaseMutex();
}
} class http()
{
........
} 我想抓取一个几个网页的数据,并填到一个表中,但经常报错,也就抓不全了,不知如何改??请不吝赐教
解决方法:
在获取到mutex后, 一定要调用相应的ReleaseMutex方法