我想要使用後臺線程來加載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
謝謝, 仍然例外仍然。請查看例外,我已經提出了我的問題。 – 2009-03-03 06:18:28