0

I am new to C# and would just like to ask a question.

I am working on a Windows application and am trying to insert a progress bar which does not work when I call it from another namespace.

My code:

namespace CLT
{
    public partial class GenBulkReceipts : UserControl
    {
        public void ProressBarMovement()
        {
            progressBar1.PerformStep();
        }

        public void LoadProgressBar(int progressbarMax)
        {
            progressBar1.Minimum = 1;
            progressBar1.Maximum = progressbarMax;
            progressBar1.Value = 1;
            progressBar1.Step = 1;
        }

private void btnOpen_Click(object sender, EventArgs e)
{
   try
   {
     OpenFile();
    }
}

private void OpenFile()
{
       if (dsEx1.Tables[0].Rows.Count > 0)
      {
        AccountsToBeImported = new BLLService().Get_AccountsToBeReceipted(dsEx1);
      } 
}
}

namespace BLL
{
    class GenBulkReceiptsBLL
    {
        public DataSet Get_AccountsToBeReceipted(DataSet dsImport)
        {
                  CLT.GenBulkReceipts pb = new CLT.GenBulkReceipts();
                  pb.LoadProgressBar(dsImport.Tables[0].Rows.Count); 

                  foreach (DataRow dr in dsImport.Tables[0].Rows)
            {
                          //Code cgoes here 
                  }
                  pb.ProressBarMovement();
               }
      }
  }

I would appreciate any help

Thanks a mil

rodrigoap
  • 7,405
  • 35
  • 46
user1171437
  • 25
  • 1
  • 5

5 Answers5

4

The reason it's not moving is because (I assume) you're doing all the work on the same thread. You probably want to do this processing on a separate thread, for example by using the BackGroundWorker

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
2

There's a couple things here. First, move the ProressBarMovement() code into your loop:

foreach (DataRow dr in dsImport.Tables[0].Rows) {
    //Code cgoes here 
    pb.ProressBarMovement();
}

You may also have to force the progress bar to repaint. That's what Refresh() is doing:

public void ProressBarMovement() {
    progressBar1.PerformStep();
    progressBar1.Refresh();
}
Yuck
  • 49,664
  • 13
  • 105
  • 135
  • I am really sorry the code example above is a little wrong the pb.ProressBarMovement(); is in the foreach loop – user1171437 Jan 26 '12 at 14:44
  • If you already have the `ProressBarMovement()` call inside the `foreach` loop, then the issue is that the UI updates aren't being processed with each iteration. Joel touches upon that in his answer as well. Either move your data processing to another thread, or force the UI element to update with each iteration. – Yuck Jan 26 '12 at 14:47
1

The key here is threading. I had the same issues when doing some WPF stuff back in the day. I ended up using a background task to update my UI and once I added that everything worked smooth.

Take a look at this thread, it's the same issue you're having. How to update GUI with backgroundworker?

Also here is the microsoft link's so that you can wrap your head around what the background worker is really doing. http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

Hope this helps.

Edit: Providing code to help get things rolling.

namespace CLT
{
    public partial class GenBulkReceipts : UserControl
    {
        public void ProressBarMovement()
        {
            progressBar1.PerformStep();
        }

        public void LoadProgressBar(int progressbarMax)
        {
            progressBar1.Minimum = 1;
            progressBar1.Maximum = progressbarMax;
            progressBar1.Value = 1;
            progressBar1.Step = 1;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
           {
               OpenFile();
            }
        }

        private void OpenFile()
        {
            if (dsEx1.Tables[0].Rows.Count > 0)
            {
                AccountsToBeImported = new BLLService().Get_AccountsToBeReceipted(dsEx1);
            } 
        }
}

namespace BLL
{
    class GenBulkReceiptsBLL
    {
        DataSet _dsImport;
        BackgroundWorker _backgroundWorker;
        CLT.GenBulkReceipts _pb;

        public GenBulkReceiptsBLL()
        {
            _backgroundWorker = new BackgroundWorker();
            _backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            _backgroundWorker.OnProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
            _backgroundWorker.ReportsProgress = true;
        }

        public DataSet Get_AccountsToBeReceipted(DataSet dsImport)
        {   
            _pb = new CLT.GenBulkReceipts();
            _pb.LoadProgressBar(dsImport.Tables[0].Rows.Count);
            _dsImport = dsImport;

            _backgroundWorker.RunWorkerAsync();  
        }

        public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {           
            int p = 0;  // set your progress if appropriate
            object param = "something"; // use this to pass any additional parameter back to the UI

            foreach (DataRow dr in _dsImport.Tables[0].Rows)
            {
                _backgroundWorker.ReportProgress(p, param);
            } 
        }

        // This event handler updates the UI
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            _pb.ProressBarMovement();
        }
    }
}
Community
  • 1
  • 1
Dylan Hayes
  • 2,331
  • 1
  • 23
  • 33
  • In what class will I put the BackgroundWorker? – user1171437 Jan 27 '12 at 07:29
  • It's been a while since I did much with the ole BackgroundTask but hopefully some code will help get you going in the right direction. This seems to make sense in my head but i didnt really test it out. Hopefully the theory is right tho. – Dylan Hayes Jan 27 '12 at 23:32
0

Is your app not responding? Try this:

    public void ProressBarMovement()
    {
        progressBar1.PerformStep();
        Application.DoEvents();
    }
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0

You should move the progressbar increment call into the foreach loop like so:

 foreach (DataRow dr in dsImport.Tables[0].Rows)
{
         //Code cgoes here 
         pb.ProressBarMovement();
}

so that each time you finish processing a row the progressbar updates by one, showing the overall progress of processing the rows to the user. If you only do it at the end you'll only end up incrementing the progressbar by 1, which will show inaccurate progress and make it look as though it's not moved

Iain Ward
  • 9,850
  • 5
  • 34
  • 41
  • I am really sorry the code example above is a little wrong the pb.ProressBarMovement(); is in the foreach loop – user1171437 Jan 26 '12 at 14:45
  • Yes thats the point, you're setting the maximum value of the progressbar to be the count of rows, which means each time you process a row you need to increment the progrssbar by 1 until you've processed all the rows. This is why it should be in the for loop, so that it shows the overall progress of processing the rows to the user. If you only do it at the end you'll only end up incrementing the progressbar by 1, which will show inaccurate progress – Iain Ward Feb 03 '12 at 15:26