XMLデータをロードするプロセスにバックグラウンドスレッドを使用したいのですが、おそらくプログレスバーを使用して、アプリケーションがアクティブに何かを実行していることをユーザーに知らせたいと思います。私はネットを検索してこのコードを書きました。
ユーザーが[参照]ボタンをクリックしたときに、WinFormのツリービューにXMLツリーをロードしたいと思います。大きなXMLファイルの場合、winformがフリーズします。したがって、バックグラウンドで作業が進行中であることをユーザーに知らせるために、プログレスバーを追加します。ここではバックグラウンドワーカーを使用しました。
ただし、System.ArgumentExceptionの例外が発生し、 xmlDocument.Load(txtFileName.Text);に「URLを空にすることはできません。\ r \ nパラメータ名:url」というメッセージが表示されます 。 この行。
私の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