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()
{
........
}      我想抓取一个几个网页的数据,并填到一个表中,但经常报错,也就抓不全了,不知如何改??请不吝赐教

解决方案 »

  1.   

    用mutex还用lock (this.ds)么?不是很理解意图
      

  2.   

    如果不用MUTEX,同一个线程也就是同一个URL的数据将被重置
      

  3.   

    如果线程终止而未释放 Mutex,则认为该 mutex 已放弃。这是严重的编程错误,因为该 mutex 正在保护的资源可能会处于不一致的状态。在 .NET Framework 2.0 版中,获取该 mutex 的下一个线程中将引发 AbandonedMutexException。
      

  4.   

    你给的代码不全,例如你的事件如何触发,getiit函数里如何处理, 所以只能给你的提示
      

  5.   

    这个问题是由于, 在一个子线程中,获取了mutex,但是在线程退出以前没有调用相应的ReleaseMutex方法,其他线程在等待这个mutex就会出现这个异常. 
    解决方法:
    在获取到mutex后, 一定要调用相应的ReleaseMutex方法