Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi, Please help me with my problem.

For example, I want to create a Label Text that counts into 100 - (this can be more than 100)
At the same process, as it is counting I want to show a Progress Bar while Label Text is Updating.

Since I don't want to report the progress, that's why I'm using Marquee and for loop below is just and example of the process.

I encountered below error in
C#
this.label1.Text += string.Format("{0}. This is a test.", i);


An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred

What I have tried:

C#
private void button1_Click(object sender, EventArgs e)
       {
           button1.Enabled = false;
           progressBar1.Style = ProgressBarStyle.Marquee;
           progressBar1.MarqueeAnimationSpeed = 50;

           BackgroundWorker bw = new BackgroundWorker();
           bw.DoWork += bw_DoWork;
           bw.RunWorkerCompleted += bw_RunWorkerCompleted;
           bw.RunWorkerAsync();
       }

       void bw_DoWork(object sender, DoWorkEventArgs e)
       {
           // INSERT TIME CONSUMING OPERATIONS HERE
           // THAT DON'T REPORT PROGRESS
           for (int i = 1; i <= 100; i++)
           {
               this.label1.Text += string.Format("{0}. This is a test.", i);

           }
           Thread.Sleep(100);
       }

       void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
       {
           progressBar1.MarqueeAnimationSpeed = 0;
           progressBar1.Style = ProgressBarStyle.Blocks;
           progressBar1.Value = progressBar1.Minimum;

           button1.Enabled = true;
           MessageBox.Show("Done!");
       }
Posted
Updated 1-Aug-16 4:54am
v5
Comments
Philippe Mori 1-Aug-16 11:04am    
You should never use Marquee style if you can compute the percentage... as it give less information to the user about progress.

You should read the documentation on BackgroundWorker before using one to properly understand how it works and should be used.

BackgroundWorker Class (System.ComponentModel)[^]

How to: Make Thread-Safe Calls to Windows Forms Controls[^]

Here are some similar questions :
c# - CrossThreadMessagingException while using backgroundworker windows forms - Stack Overflow[^]
multithreading - C# threading issue - Stack Overflow[^]

In your case, you usually want to enable progress reporting and then implement an handler to report progress.
BackgroundWorker.ReportProgress Method (Int32) (System.ComponentModel)[^]

Usually, I would compute the percentage inside DSoWork handler and only report progress if the percentage (as an integer) has changed. That way, you ensure that you don't update the UI more than 100 times which is adequate when a progress bar is displayed.

By the way, there is a version that allows to pass an UserState which could be used if you want to report something else than percentage. This is somewhat simpler and clearer than using Invoke directly.

It generally works quite well if you report progress up to a few hundreds of time. If you have many thousand updates, then you somehow need to optimize communications between threads to reduce the overhead of cross-thread calls.
 
Share this answer
 
I'm not sure why you're setting progressBar1.Value = progressBar1.Maximum there - it may be something I dont know about the Marquee style .. that being said, I would have expected something like

C#
progressBar1.BeginInvoke(
  new Action( () =>
    {
      progressBar1.Value = i;
    }
  ));


inside your 'for loop' - I dont see where else you're updating the value for the progress bar

[edit] .. why not take this

C#
for (int i = 1; i <= 100; i++)
{
  this.label1.Text += string.Format("{0}. This is a test.", i);
  Thread.Sleep(10000);
}


and make it this

C#
for (int i = 1; i <= 100; i++)
{
  this.label1.Text += string.Format("{0}. This is a test.", i);
  progressBar1.Value = i; 
  Thread.Sleep(10000);  // Not sure you need to delay for this long 
}


unless somehow Ive missed what your trying to do/what your problem is

[/edit]
 
Share this answer
 
v2
Comments
berrymaria 1-Aug-16 1:39am    
Hi Sir Garth, I already updated my code.. Please help..
Garth J Lancaster 1-Aug-16 2:05am    
you still dont update progressBar1.Value in the loop ! see the updated solution (in a minute after I've edited it)
berrymaria 1-Aug-16 2:06am    
Yes Sir, i did not update the value of the progress bar because I just want to use the Marquee Style..
berrymaria 1-Aug-16 2:07am    
I have encountered a new error Sir..
Garth J Lancaster 1-Aug-16 2:16am    
you can handle that error by using BeginInvoke - see the strike through text and apply the same type of thing to label1.text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900