Add your own alternative version
Stats
167.7K views 57 bookmarked
Posted
9 Apr 2009
|
Comments and Discussions
|
|
Its not working with asynchronous call. Like I start the that bar and then start my work and then stop the bar. But it not working.
//Bar start
//My work
//Bar Stop
But its not behave like that
I want it start the bar and after that i do my work like saving record etc and once my work finshed it should stop the bar but its not at all.
You can download the sample from here
Progress Bar
modified 3-Mar-12 2:59am.
|
|
|
|
|
I am using my phone to type this so need to keep it brief. But if you check the prism article from home page of my mvvm framework cinch.codeplex.com there is fully working example in the prism demo app. Look there for async example
Sacha Barber
- Microsoft Visual C# MVP 2008-2012
- Codeproject MVP 2008-2011
Open Source ProjectsCinch SL/WPF MVVM
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
I have implemented this user control (thanks, btw), and have added it on a grid named ProgressBar. The grid is invisible, and is placed on top of all other controls on my UI.
I want to make the grid (and therefore the circularprogressbar) visible, then call a stored procedure that takes about 20 seconds to run. Since this UI does nothing else (it is simply to get data for a report, the app to be installed on somoene else's computer so I don't have to get the report for him), I don't want any UI interaction for the user, just the progressbar to let them know it's not frozen.
I have tried several things, from changing visibility to visible before the stored procedure is called, and changing it back to hidden after, to spinning a new thread for the SP, and so forth. No matter what I've tried, the UI just freezes until the SP returns the data, and the progressbar does not appear. Of note, if I remove the visibility = hidden from the xaml of the grid, it is visible and works great.
Here is the latest code I have:
private delegate void UpdateProgressbarDelegate(bool b);
private void UpdateProgressbar(bool b)
{
if (this.ProgressBar.Dispatcher.CheckAccess())
{
if (b)
{
ProgressBar.Visibility = Visibility.Visible;
}
else
{
ProgressBar.Visibility = Visibility.Hidden;
}
}
else
{
this.ProgressBar.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new UpdateProgressbarDelegate(this.UpdateProgressbar));
}
}
In debugging, the dispatcher.invoke is never called - it goes through the if(b) statement every time. I can step through the making it visible, then wait for the sp to return data, then step through making it hidden again, but in the UI, it does not appear.
Do you have any suggestions for a relatively new developer, working in WPF for the first time (C# 4.0)? Thanks!
Enter any 11 digit prime number to continue.
|
|
|
|
|
Before I start, let me say this is very old code, I have done much better progress bar since then. There will be links shown below. DO NOT USE THIS ONE
The problem you are having is that WPF only have 1 active thread, the same as Winforms. It is called the Dispatcher thread. What is happening is that you are starting some work on the Dispatcher thread (your 20 second SQL call), and then trying to get the dispatcher to do some more stuff, like toggling the visibility of the progress wheel. Which does not work as the Dispatcher is busy doing your SQL call.
What is happening is the Dispatcher message pump can't push the toggle visibility message through as its too busy. In winforms you solved this with a DoEvents(), WPF doesn't have that exactly, what you need to do is push an empty DispatcherFrame (or empty delegate) onto message loop.
But that is neither here or there, what YOU should be doing, is launching your long running call in a new thread. Starting a new thread can be done in loads of ways.
New Thread
ThreadPool
Task Parallel Library
Rx
Async Delegates
Loads of choices.
What you basically need to do is this
1. Click button (say) that starts work
2. Toggle visibility of progress wheel to visible
3. Start new thread which does stored procedure 20 seconds work
4. When new thread is done, get results out, and also toggle visibility of progress wheel to hidden
You will of course need to take care when getting the results back into WPF land, as they may required some threading synchronization.
I actually do this thing you want to do a lot using WPF and MVVM. I have even written a specialized control which you can grab from my own MVVM framework http://cinch.codeplex.com/
The class you want is this one : AsyncBusyUserControl, which relies on some ViewModel code and also makes use of AsyncFailedUserControl and the CircularProgressBar controls.
If you download the Cinch V2 codebase and look at the WPF sample you will see an example of that controls usage.
I also use it here : Showcasing Cinch MVVM framework / Prism 4 interoperability (demo 1)
So there you go I would just use the control I wrote, it just works, you will have to dig through the code but its all there.
Sacha Barber
- Microsoft Visual C# MVP 2008-2011
- Codeproject MVP 2008-2011
Open Source ProjectsCinch SL/WPF MVVM
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Hi,
nice component - I was noticing that if you use multiple instances of it, you get an exception since it is only allowed to override the metadata once.
So I moved it to a static constructor:
static CircularProgressBar()
{
Timeline.DesiredFrameRateProperty.OverrideMetadata(
typeof(Timeline),
new FrameworkPropertyMetadata { DefaultValue = 20 });
}
public CircularProgressBar()
{
InitializeComponent();
}
|
|
|
|
|
Fair enough, I have a V2 of this which is much better, which you can get from my blog
Sacha Barber
- Microsoft Visual C# MVP 2008-2010
- Codeproject MVP 2008-2010
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
The XAML code can be significantly simplified if you replace the Canvas with a RadialPanel (e. g. this one:
http://jobijoy.blogspot.com/2008/04/simple-radial-panel-for-wpf-and.html[^]) for layout. This saves you from manually calculating all these Canvas.Left/Top attached properties.
I'm still looking for a way to declaratively (=through XAML) stop the animation when the element gets Hidden/Collapsed. Does anybody know how to accomplish this?
|
|
|
|
|
Yeah I dont actually use this one any more, I use this one instead : http://sachabarber.net/?p=639[^]
Sacha Barber
- Microsoft Visual C# MVP 2008-2010
- Codeproject MVP 2008-2010
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Hi Sacha,
thanks a lot for the pointer to the new version.
Did you realize performance problems when using WPF animation instead of your own timer? I mean, it should be possible to stop the WPF animation when the control is invisible with the code behind file. Was it still a performance problem when the WPF animation was running only in visible state?
Chris
|
|
|
|
|
Yeah it was not good performance at all when the animation was still running when it was not visible, so I wrote that new one. That works fine, we use it all over our code and it works great.
Sacha Barber
- Microsoft Visual C# MVP 2008-2010
- Codeproject MVP 2008-2010
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Resizing was suicidal but thanks for sharing!
|
|
|
|
|
Just put it in a ViewBox element to resize. Easy
Sacha Barber
- Microsoft Visual C# MVP 2008/2009
- Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Hi Sacha,
Your program is brilliant. I want to use it in a project where in i want to run this Circular Progressbar in one thread and have my ohter operations run in another thread. In that way both threads can run simultaneaously.
Please guide me in this asap. I'm a beginner in WPF.
Thanks.
Regards,
JNongrum.
|
|
|
|
|
Nice article. But the title should be either rounded or rounded 3D progress bar. Circular implies going around in a circle (what I was looking for).
AliR.
Visual C++ MVP
|
|
|
|
|
It should've been an alternate template for standard progress bar, not a brand new control.
|
|
|
|
|
So why dont you go ahead and do that. You know you have the code, its not my job to do everything for evryone. You do it.
Sacha Barber
- Microsoft Visual C# MVP 2008/2009
- Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
But it's your responsibility to produce good code. First of all you are MVP, this title binds you to be a professional, you cannot rest on your laurels. Second, you are popular and famous among .NET programmers, so you should teach them proper ways to implement code, not a quick-but-work code. And third, I didn't want to offend you, and if you thought so - sorry.
|
|
|
|
|
Yeah I was offended actually.
mfrasiu wrote: First of all you are MVP, this title binds you to be a professional, you cannot rest on your laurels
Er no, I do what I want how I want when I want, if that gets me awards so be it. BUt I do code for me, and to share, if people learn great, if not, thats really nothing to do with me.
mfrasiu wrote: Second, you are popular and famous among .NET programmers, so you should teach them proper ways to implement code, not a quick-but-work code
I do not feel this is a hack. We actually use this in production code, as we want the StoryBoard frames to be different for this control, which AFAIK cant be done in a template anyway.
But anyway thanks for your comments at any rate
Sacha Barber
- Microsoft Visual C# MVP 2008/2009
- Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Sacha Barber wrote: Er no, I do what I want how I want when I want, if that gets me awards so be it. BUt I do code for me, and to share, if people learn great, if not, thats really nothing to do with me.
Rember that with great power comes great responsibility :P
Sacha Barber wrote: we want the StoryBoard frames to be different for this control, which AFAIK cant be done in a template anyway.
Did you mean the changing the framerate? I know it can be done in template:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ControlTemplate x:Key="MyPB" TargetType="{x:Type ProgressBar}">
<Border Initialized="Border_Initialized" BorderBrush="Black" BorderThickness="2">
<Border.RenderTransform>
<RotateTransform x:Name="SpinnerRotate" Angle="0" />
</Border.RenderTransform>
<Border.Triggers>
<EventTrigger RoutedEvent="ContentControl.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SpinnerRotate"
Storyboard.TargetProperty="(RotateTransform.Angle)"
From="0" To="360" Duration="0:0:01"
RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
</Border>
</ControlTemplate>
</Application.Resources>
</Application> namespace WpfApplication1
{
public partial class App : Application
{
private void Border_Initialized(object sender, EventArgs e)
{
Timeline.DesiredFrameRateProperty.OverrideMetadata(
typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 5 } );
}
}
}
Cheers 
|
|
|
|
|
See you didn't need me to do anything after all.
Sacha Barber
- Microsoft Visual C# MVP 2008/2009
- Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Reading your comments,
I find you to be an unpleasant individual that I do not like.
|
|
|
|
|
I was having a bad day, and you know what I completely agree with you. Based on the comments I made (which are actually out of character for me, normally I am quite helpful), I would think I was a complete idiot too.
modified 18-Mar-15 4:50am.
|
|
|
|
|
Another great Article from you Sacha and it is a nice one of course.
"Imagination is more important than knowledge.."
{Albert Einstein}
|
|
|
|
|
Its just a simple blog, but thanks
Sacha Barber
- Microsoft Visual C# MVP 2008/2009
- Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
Another great session from you Sacha and it is a nice one of course.
"Imagination is more important than knowledge.."
{Albert Einstein}
|
|
|
|
|
|
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|