0

I have created one windows form, there i have a progress bar, i want to update the value of progress bar, from another class. I have written one function in my class on its execution, i want to see the progress in progress bar.

For Example:

  1. The code written in forms .cs file:
 namespace UpdateProgressBar
{
    public partial class ProgressBarUdpate : Form
    {
        public ProgressBarUdpate()
        {
            InitializeComponent();
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            UpdateDataProgress updt = new UpdateDataProgress();
            updt.ExecuteFucntion();
        }
    }
}
  1. The code written in another class
namespace UpdateProgress
{
    public class UpdateDataProgress
    {
        public void ExecuteFucntion()
        {
            for (int i = 0; i < 100; i++)
            {

            }

        }
    }
}

My Expected output is when i call updt.ExecuteFucntion function it should update the progressbar values as per loop implemented in another class.

bommelding
  • 2,969
  • 9
  • 14

2 Answers2

2

You should use Event in such requirement.

Logic:

As your basic requirement is to update Progressbar (UI) based on current status of execution of a method (which is in class library)

You need to raise an event while executing ExecuteFucntion(). This Event will be Handled at ProgressBarUdpate form.

As you can see in code below, after creating an object of UpdateDataProgress, we are subscribing it's event by updt.ExecutionDone += updt_ExecutionDone;

Thus as soon as That event raised form ExecuteFucntion() it will call updt_ExecutionDone of ProgressBarUdpate where you can suerly update your progress bar.

Update your code as below.

    public partial class ProgressBarUdpate : Form
    {
        public ProgressBarUdpate()
        {
            InitializeComponent();
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            UpdateDataProgress updt = new UpdateDataProgress();
            updt.ExecutionDone += updt_ExecutionDone;
            updt.ExecuteFucntion();
        }

        void updt_ExecutionDone(int value)
        {
            //Update your progress bar here as per value
        }
    }

and class UpdateProgress

    public delegate void ExceutionHandler(int value);
    public class UpdateDataProgress
    {
        public event ExceutionHandler ExecutionDone;
        public void ExecuteFucntion()
        {
            for (int i = 0; i < 100; i++)
            {
                //perform your logic

                //raise an event which will have current i 
                //      to indicate current state of execution
                //      use this event to update progress bar 

                if (ExecutionDone != null)
                    ExecutionDone(i);
            }

        }
    }
Amit
  • 1,821
  • 1
  • 17
  • 30
0

You can use Events, or simply:

  1. Code in form:

    private void btn_Submit_Click(object sender, EventArgs e)
    {
        UpdateProgress.UpdateDataProgress updt = new UpdateProgress.UpdateDataProgress();
        updt.ExecuteFucntion(progressBar1);
    }
    
  2. Code in class:

    public class UpdateDataProgress
    {
        public void ExecuteFucntion(System.Windows.Forms.ProgressBar progbar)
        {
            for (int i = 0; i < 100; i++)
            {
                progbar.Value = i;
            }
        }
    }
    
Nivoro
  • 11
  • 2