简体   繁体   中英

show progress of the background task on a pop-up windows in windows form application using C#.Net

I am creating some files from xml data in the background using

Task.Factory.StartNew(() => xmlconvert(xx, yy));

Now, the question is how to show the progress of this method using a StatusStrip control with some message and the progress or at least just a scrolling animation for the progress. I don't just have any idea how would it work.

Update: First of all, this method 'xmlconvert(xx, yy)' has four different forms depends on the condition user selects at runtime. In the main form of my application user can select from different conditions to process on the data. Then finally when user click on the Button 'Create' all these conditions are being checked and a suitable method will be called within that button click event. I need to show the progress of this method which is being invoked at runtime.

private void btnCreateRelease_Click(object sender, EventArgs e)
{
      // Checks set of conditions

      if(cond 1)
      {
          xmlconvert_1();
      }
      else if (cond2)
      {
          xmlconvert_2();
      }
      else if (cond3)
      {
          xmlconvert_3();
      }
      else if (cond4)
      {
          xmlconvert_4();
      }
}

I want to show progress of one of these methods which will be invoked at runtime depends on the condition.

Thanks a lot.

You can use the BackgroundWorker for this, and it's pretty simple, too. Here's a sample to get you going:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }

    void Form1_Shown(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Do your work in here.
        xmlconvert(xx, yy);

        for (int i = 0; i <= 100; i++)
        {
            backgroundWorker1.ReportProgress(i);
            System.Threading.Thread.Sleep(100);
        }
    }

    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
}

And here's the link to the documentation .

To get it to work in your scenario, I would suggest you add a Progress bar to your StatusStrip control and update it from within the backgroundWorker1_ProgressChanged event.

If you wish just to show, that your app is not hang may help following approach:

public static class ActionExtensions
{
    public static void RunWithMargueProgress(this Action action)
    {           
        var progressForm = new ProgressForm();

        progressForm.Show();
        Task.Factory.StartNew(action)
            .ContinueWith(t =>
                              {
                                  progressForm.Close();
                                  progressForm.Dispose();
                              }, TaskScheduler.FromCurrentSynchronizationContext());            
    }
}

Where ProgressForm would be a simple form with ProgressBar , that is set to Marquee style. If you have idea, how it is progressing, it is better to show progress for user and use BackgroundWorker .

As long as it's parameter is Action , it is easily reusable.

Usage would be:

private void button_Click(object sender, EventArgs e)
{
    Action action = () => Thread.Sleep(5000);
    action.RunWithMargueProgress();          
}

If you have control in status strip, that you wish to animate, you can do it like this:

public static void RunWithMargueProgress(this Action action, ToolStripProgressBar progressBar)
{
    progressBar.Style = ProgressBarStyle.Marquee;            
    progressBar.MarqueeAnimationSpeed = 30;

    Task.Factory.StartNew(action)
        .ContinueWith(t =>
                           {
                               progressBar.MarqueeAnimationSpeed = 0;
                               progressBar.Style = ProgressBarStyle.Continuous;                                      
                           }, TaskScheduler.FromCurrentSynchronizationContext());
}

Usage would be pretty much the same:

private void button_Click(object sender, EventArgs e)
{
    Action action = () => Thread.Sleep(5000);
    action.RunWithMargueProgress(ToolStripProgressBar);          
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM