Show Progress Visually

67 views Asked by At

I have a function that performs several steps, one at a time, and I would like to display a form or something that tells the user their function is being processed

Basically the function in the Main Form looks like this:

 public void MyFunction ()
{
//Step1, takes aproximately 20 seconds
//Step2, takes 10 seconds
//Step3, takes about 25 seconds
}

So, the idea is to inform the user something is under process, because during Step 1, 2 and 3, the GUI remains unresponsive. I don't want to use a simple ProgressBar, it's an image processing software and there's no space to add a ProgressBar, it should be something that pops up basically. I was thinking on another form but not sure how would that work..

Any ideas on how to do this?

Thank you,

Matias.

3

There are 3 answers

0
a-ctor On BEST ANSWER

To create a simple solution consider this:

Create a another form (in my case Progress) and put something on it which visualizes the progress. In my case it looks like this:

Progess form

The form itself has the FormBorderStyle set to FixedToolWindow and StartPosition set to CenterParent.

Now change the constructor to the a BackgroundWorker argument and set up the events and start the process.

public Progress(BackgroundWorker task)
{
    InitializeComponent();
    task.RunWorkerCompleted += (sender, args) => Close();
    task.ProgressChanged += (sender, args) => pB_progress.Value = args.ProgressPercentage;
    task.RunWorkerAsync();
}

In the main form just add something like this:

private void PopupJob()
{
    var worker = new BackgroundWorker
    {
        WorkerSupportsCancellation = false,
        WorkerReportsProgress = true
    };
    worker.DoWork += OurRealJob;
    new Progress(worker).ShowDialog();
}

private void OurRealJob(object sender, DoWorkEventArgs doWorkEventArgs)
{
    var wrk = (BackgroundWorker) sender;
    Thread.Sleep(1000);
    wrk.ReportProgress(30);
    Thread.Sleep(5000);
    wrk.ReportProgress(100);
}

To show the popup progress thingy simply call PopupJob. The OurRealJob can be replaced with your code.

When you do it this way, the code will run in the background and the form will stay responsive. Because we are calling Form.ShowDialog() the progress form will be at the front and the main form can not be used until the job is done.

2
fieora On

in C#.NET there is an object for a progress bar. It has a property called step that is an int that increases the progress by that value. You increase the progress by running the PerformStep() method. Which might look like this.


progressBar1.PerformStep();

You will have to figure out how you want to measure progress. You can hide the object and use the value in a pop-up window.

0
Graffito On

If you are using WinForms, you will find a class matching your expectations there: SxProgress overview and source code

SxProgress.Execute("XXXXX in progress...",3,false,false,myFunction_ExecInThread,new object[]{})

    private bool myFunction_ExecInThread,_ExecInThread(int ItemIndex,object[] UserObjects)
    { // 3 steps ==> ItemIndex varies from 0 to 2
      switch (i)
        { 
           case 0 : // your step 1 code there
                    // if you have a n/count progress info in step 1 process, use this:
                    // SetSubProgressValue(n,count,UserObjects) 
                    break ;
           case 1 : // your step 2 code there
           case 2 : // your step  3 code there
        }
       return true ; // or false if a step fails
    }