Pregunta

I am making a small tool which reads in a bunch of files and adds certain xml if the file does not contain these lines. This works perfectly but can be timeconsuming because of large directories and files.

Because of this, I decided to use a backgroundworker and add a progressbar showing where in the execution we are.

The problem, however, is that the progressbar does not update.

Code for reference:

    BackgroundWorker bw = new BackgroundWorker();
    Form progressForm = new Form();
    ProgressBar progressBar = new ProgressBar();

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {

        if (!csprojPaths.Any())
        {
            MessageBox.Show(Resources.noprojectsfound);
        }
        else
        {
            for (int i = 0; i < csprojPaths.Count(); i++)
            {
                string csprojPath = csprojPaths[i];
                CheckOutFromTFS(csprojPath);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(csprojPath);
                Convert(xmlDoc, ruleset);
                xmlDoc.Save(csprojPath);
                bw.ReportProgress(i);
            }
        }
    }

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar.Value = e.ProgressPercentage;
    }

    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressForm.Close();
        DisplayDialog(csprojPaths);
    }


    public void ConvertIn(string pad, string rules)
    {

        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

        path = pad;
        ruleset = rules;

        csprojPaths = Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories);
        progressForm.Size = new Size(30, 30);
        progressForm.Controls.Add(progressBar);
        progressBar.Visible = true;
        progressBar.Minimum = 0;
        progressBar.Maximum = csprojPaths.Count();
        progressBar.Value = 0;
        progressBar.Step = 1;
        progressForm.ShowDialog();

        bw.WorkerSupportsCancellation = false;
        bw.WorkerReportsProgress = true;
        if (!bw.IsBusy)
        {
            bw.RunWorkerAsync();
        }


    }

The form with the progressbar opens but never closes nor the bar updates. When I close the progressForm the DisplayDialog(csprojPaths) gets executed. It doesn't if I keep the progressForm open.

What am I doing wrong here?

¿Fue útil?

Solución

Problem : progressForm.ShowDialog() waits untill unless the form is closed to execute the further statements. so the statements after the progressForm.ShowDialog() will not get executed untill the Form is closed.

Solution: You need to call progressForm.Show() which will not block the program flow. as it will open the progressForm independently so the statements after the progressForm.Show() also get executed withoutwaiting for anything.

From MSDN: Form.ShowDialog()

You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.

Replace This:

 progressForm.ShowDialog();

With This:

 progressForm.Show();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top