Thread Progress Bar
-
I have this code:
public void mtdProgressbar() {
prgsBar.IsIndeterminate = false; prgsBar.Orientation = Orientation.Horizontal; prgsBar.Minimum = 0; prgsBar.Maximum = short.MaxValue; prgsBar.Value = 0; double value = 0; prgsBar.Visibility = Visibility; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue); do { value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value }); } while (prgsBar.Value != prgsBar.Maximum); prgsBar.Visibility = Visibility.Hidden; }
But i can not create the Thread do fill progressbar, do not work. The code:
.
.
.
else{
System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
trd.IsBackground = true;
trd.Start();
mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
}I'm using C# and Wpf
-
I have this code:
public void mtdProgressbar() {
prgsBar.IsIndeterminate = false; prgsBar.Orientation = Orientation.Horizontal; prgsBar.Minimum = 0; prgsBar.Maximum = short.MaxValue; prgsBar.Value = 0; double value = 0; prgsBar.Visibility = Visibility; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue); do { value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value }); } while (prgsBar.Value != prgsBar.Maximum); prgsBar.Visibility = Visibility.Hidden; }
But i can not create the Thread do fill progressbar, do not work. The code:
.
.
.
else{
System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
trd.IsBackground = true;
trd.Start();
mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
}I'm using C# and Wpf
Threads can be tricky, what you are running into is a problem with access.. threads by default can not access the ui to update a progress bar because that may create a deadlock. For the background worker there is a function built in for report progress, but it is limited to only allow for simple updates. You can however, get around this requirement by doing .Invoke((Action)delegate() which allows safe access to the ui through a delegate. I have used this method to update multiple labels, etc. That should do the trick.
=)
-
I have this code:
public void mtdProgressbar() {
prgsBar.IsIndeterminate = false; prgsBar.Orientation = Orientation.Horizontal; prgsBar.Minimum = 0; prgsBar.Maximum = short.MaxValue; prgsBar.Value = 0; double value = 0; prgsBar.Visibility = Visibility; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue); do { value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value }); } while (prgsBar.Value != prgsBar.Maximum); prgsBar.Visibility = Visibility.Hidden; }
But i can not create the Thread do fill progressbar, do not work. The code:
.
.
.
else{
System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
trd.IsBackground = true;
trd.Start();
mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
}I'm using C# and Wpf
You don't put the progress bar in the background thread. It MUST stay on the UI (startup) thread. Your work goes in the background thread and then Invokes a method that updates the progress bar on the UI thread.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Threads can be tricky, what you are running into is a problem with access.. threads by default can not access the ui to update a progress bar because that may create a deadlock. For the background worker there is a function built in for report progress, but it is limited to only allow for simple updates. You can however, get around this requirement by doing .Invoke((Action)delegate() which allows safe access to the ui through a delegate. I have used this method to update multiple labels, etc. That should do the trick.
=)
Forbiddenx wrote:
You can however, get around this requirement by doing
.Invoke((Action)delegate() which allows safe access to the ui through a delegate.Hi, isn't the OP already doing this in his code:
do
{
value += 1;Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
}
? Of course, the OP didn't disclose the code for 'updatePbDelegate, or show where it's created. In this case I think the problem is elsewhere (see Dave K.'s response).
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
-
Forbiddenx wrote:
You can however, get around this requirement by doing
.Invoke((Action)delegate() which allows safe access to the ui through a delegate.Hi, isn't the OP already doing this in his code:
do
{
value += 1;Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value });
}
? Of course, the OP didn't disclose the code for 'updatePbDelegate, or show where it's created. In this case I think the problem is elsewhere (see Dave K.'s response).
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
Yes, I do see invoke but it does not look like its used correctly. It looks to me like his code is trying to access the progress bar ui directly and outside of the invoke. It looks like he is setting a lot of values, my guess is some of those values are on the ui and that is what is crashing everything. I think he is going to have to re-write how this works..
=)
-
I have this code:
public void mtdProgressbar() {
prgsBar.IsIndeterminate = false; prgsBar.Orientation = Orientation.Horizontal; prgsBar.Minimum = 0; prgsBar.Maximum = short.MaxValue; prgsBar.Value = 0; double value = 0; prgsBar.Visibility = Visibility; UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue); do { value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object\[\] { ProgressBar.ValueProperty, value }); } while (prgsBar.Value != prgsBar.Maximum); prgsBar.Visibility = Visibility.Hidden; }
But i can not create the Thread do fill progressbar, do not work. The code:
.
.
.
else{
System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
trd.IsBackground = true;
trd.Start();
mtdTest(); //this method contains only loop for,represents an operation that requires the progressbar
}I'm using C# and Wpf