2009-03-02 92 views
1

我想要使用後臺線程來加載XML數據的進程,可能還有一個進度條讓用戶知道應用程序正在積極地執行某些操作。 我已經通過搜索網絡寫了這段代碼。
我想要在winform樹形視圖中加載一個XML樹,當用戶cliks瀏覽按鈕。 如果一個大XML文件的winform凍結。爲了讓用戶知道在後臺工作正在進行中我想添加一個進度條。我在這裏使用了一個後臺工作。在XMLDocument.load方法(txtFileName.Text):上傳Xml文件的進度條

但它是提高System.ArgumentException的顯示此消息的異常「上的網址不能爲空\ r \ n參數的名字。」這一行。
我的xml文件格式正確,位於我選擇的正確位置。 但我無法找到此異常的原因。 您可以請幫忙或告訴我在我的代碼更正嗎?
謝謝....

 private void btnBrowse_Click(object sender,EventArgs e) 
     { 
      bgWorker1.RunWorkerAsync(); 
      StripProgressBar.Value = 0; 
      toolStripStatusLabel1.Text = "Browsing for a Xml file"; 

      if (open.ShowDialog(this) == DialogResult.OK) 
      { 
       txtFileName.Text = open.FileName; 
       initiatingTree(open.FileName); //this variable gives the name of selected file 
      } 
      while (this.bgWorker1.IsBusy) 
      { 
       StripProgressBar.Increment(1); 
       // Keep UI messages moving, so the form remains 
       // responsive during the asynchronous operation. 
       Application.DoEvents(); 
      } 
     }//Browse button  
     private void bgWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      xmlDocument = new XmlDocument(); 
      Thread.Sleep(5000); 
      xmlDocument.Load(txtFileName.Text); 
      btnBrowse.Enabled = false; 
     } 
     private void bgworker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      // Set progress bar to 100% in case it's not already there. 
      StripProgressBar.Value = 100; 
      if (e.Error == null) 
      { 
       MessageBox.Show(xmlDocument.InnerXml, "Download Complete"); 
      } 
      else 
      { 
       MessageBox.Show("Failed to download file"); 
      } 
      // Enable the Browse button and reset the progress bar. 
      this.btnBrowse.Enabled = true; 
      StripProgressBar.Value = 0; 
      toolStripStatusLabel1.Text = "work finished processing request."; 
     }//workerCompleted 

回答

4

你立即啓動異步處理,當用戶點擊「瀏覽」,通過調用

bgWorker1.RunWorkerAsync(); 

這就要求你的後臺工作,的DoWork方法,睡眠5秒鐘,並從txtFileName.Text中提取值,無論用戶是否已完成FileOpenDialog中的輸入。

你最好將​​(和繁忙的等待)移動到if (open.ShowDialog(this) == DialogResult.OK)塊。

private void btnBrowse_Click(object sender,EventArgs e) 
    { 
     StripProgressBar.Value = 0; 
     toolStripStatusLabel1.Text = "Browsing for a Xml file"; 

     if (open.ShowDialog(this) == DialogResult.OK) 
     { 
      txtFileName.Text = open.FileName; 
      initiatingTree(open.FileName); 

      bgWorker1.RunWorkerAsync(); 

      while (this.bgWorker1.IsBusy) 
      { 
       StripProgressBar.Increment(1); 
       // Keep UI messages moving, so the form remains 
       // responsive during the asynchronous operation. 
       Application.DoEvents(); 
      } 
     } 
    } 

對於這類問題,它可以幫助把該文件是要得到加載斷點正確的,看看有什麼價值是當這種情況發生......你可能會發現,它越來越被稱爲用空字符串。

您可能還會考慮帶有參數的RunWorkerAsync版本;你可以通過這種方式傳遞文件,而不是試圖從文本框中異步讀取文件。

而且我個人不會使用一個叫Application.DoEvents()的循環;相反,我會將控制返回到UI線程,然後將Invoke()從異步線程轉移到它以影響進度條更新。

+0

謝謝, 仍然例外仍然。請查看例外,我已經提出了我的問題。 – 2009-03-03 06:18:28

1

當方法bgWorker1.RunWorkerAsync();被稱爲事件DoWork被解僱。

由於該方法在應用程序的開頭調用,所以文件名文本框爲空。

我希望你能理解。