1

i want to use a background thread for the process of loading the XML data, possibly with a progress bar to let the user know that the application is actively doing something. i have written this code through searching the net.
i want to load a XML tree in treeview on winform when a user cliks a Browse button. In case of a large XML file the winform freezes.So to let the user know that in background the work is going on i want to add a progress bar.i have used a background worker here.

But it is raising an exception of System.ArgumentException showing this message "The URL cannot be empty.\r\nParameter name: url" on xmlDocument.Load(txtFileName.Text); this line.
My xml file is in correct format and is at the proper location where i selected. But i am unable to find the cause of this exception. Can you please help out or tell me the correction in my code?
Thanks....

       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

2 回答 2

4

当用户单击“浏览”时,您将立即启动异步过程,方法是调用

bgWorker1.RunWorkerAsync();

这将调用DoWork您的后台工作程序的方法,该工作程序会休眠 5 秒,并从txtFileName.Text用户是否已完成他们在FileOpenDialog.

你最好把byWorker1.RunWorkerAsync()(和忙碌的等待)搬到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()从异步线程返回到它以影响进度条更新。

于 2009-03-02T14:47:45.673 回答
1

当方法 bgWorker1.RunWorkerAsync(); 被称为事件 DoWork 被触发。

因为该方法是在应用程序开始时调用的,所以文件名文本框为空。

我希望你已经明白了。

于 2009-03-02T15:02:55.107 回答