How to create a visualization for a timer job - c#

In my SL application I have a DispatchTimer which periodically refreshes data on the screen. I would like to create a visualisation for the user to show when the next refresh will happen.
My first thought was to change the timer (which currently runs every 20 seconds) to run every millisecond and, each tick, update a ProgressBar and count-down till the 20 seconds are up to run the main update method.
But I cannot shake the feeling that there's probably a better way of doing this. This feels quite heavy.
IS there a better way of doing this?

I would make an animated progress bar which resets on reaching the end.
Easiest would be to create two rectangles, one on top of antother with different colors.
Animate width of the one on the top from 0 to length of your progress bar.
You can control it by making a property which indicates how long should the animation last.
And maybe some start method to synchronize with refresh actions.
EDIT. There is example on msdn HERE (examples section)

The idea of a progres bar is not that bad. Updating every milisecond or each tick is much to often. 10 times a second would be enough.

Related

Simple multi-threading issue

I have a class called Movement which moves a picture box (guy on the left) on my form to a random position when it's Lead method gets executed. I also have a method which moves another picture box (dark guy on the right) called SetMrShadowToMove. These two picture boxes are suppose to move at the same time and arrive at the random point (one point for both picture boxes) at the same time.
For now, these methods use a timer to move the picture boxes, but my main intention was to have a loop which runs until the picture box's coordinates meet the random point picked while using Thread.Sleep to delay the process and make an animation of it.
To do that, I need to run both methods on different threads, so my UI thread doesn't get frozen and these methods would run at the same time.
My problem is that I don't know how to run these methods on different threads, and also don't know how to access the picture boxes on the other threads (it seems that this has something to do with invoking and stuff!).
What is the best way to solve this problem?
The best way (in my opinion) is by using a background worker.
those things can do there calculations in the background. If you you want to update the interface after that you can use there report progress event to invoke your main (interface) thread and redraw the screen. You can also the work complete even if you want to have a event of the background worker as a final even (for example if mister shadow is 'destroyed' or something).
I don't know your code of mister shadow so I don't have any direct example code for you. But a full example about how to use a background worker can be found on msdn here. I hope this will help you.

UI frozen when updating pie-chart control

I'm using the chart controls from WinRTXAMLToolkit, to draw a pie chart. The chart is updated on the screen using the values from a few slider controls.
When the sliders values are changed, I call in a function to calculate a formula (say compound interest) based on the sliders' values.
And then I change the ItemsSource of the chart's SeriesDefinitions to point to the new collection of data.
I'm trying to have a 'live' PieChart which updates instantly. The problem is when I keep changing the values of the sliders, there is a 1-2 second delay for the chart to draw itself again - and this makes the slider movement 'freeze' in between for a second.
Is there any way around this problem ? Can I move the chart-updation to a different thread so that the UI thread remains free and doesn't freeze up ?
Try update it using another thread and run it async.
One way is to insert it into Update fuction, and do:
await Update()
While Update and the method which it called in are async.
I think that the problem may be that you're changing the ItemsSource. This is a more intensive operation because a lot of things happen in the background. Could you use an ObservableCollection and update it instead of replacing it?

Automatic Smooth Resize Transitions - A Problem with Storyboards

I have been trying for some time to create a class/series of classes that, upon detecting a resize in a related object, attempts to halt the resize of the object and create a smooth resize animation for it.
However, I have always had a problem with the objects 'flickering' on the screen for a second at the target size before the animation kicks in.
Long story short, after some serious debugging and self-doubt, I am under the impression that calling Storyboard.Begin() does not affect the target value until the next clock tick. I presume that this has something to do with Storyboard.Seek() not occuring until the next clock tick. (See the MSDN Reference). And as such, the object adopts its new height for a single frame before the animation kicks in which pulls it back to the starting height.
I have spent a long time trying to get this working because it's not nearly as simple as it seems. I can't set FrameworkElement.Height to the e.PreviousSize.Height in a FrameworkElement.SizeChanged event handler, because that affects the 'true' height of the object. As an example of why this doesn't work, is if something changes the 'true' height of the object while the animation is playing, then re-setting FrameworkElement.Height to the target height that was collected when the animation began could set it as an incorrect, out-dated value.
There's more things I've tried, and I'm sure they'll come up as answers arrive, but any ideas you have will be very much appreciated.
You'd probably have better luck by creating a layout container that handles the resize of the child objects, something like Robby Ingebretsen's Animated Panel.

How to do something like a screensaver in my WPF-Application?

I have an WPF-Application with 3 different UserControls in the MainWindow and only one of these is visible at the time. It's like having 3 different pages and you able to switch from one page to another when you like it. First page is like a start-screen. Second is like the "general view". And the third page shows details.
Switching between them works fine with Storyboard. I just make the visible page invisible (opacity to zero) and move it out of the visible window- area and move the new page into the visible window-area and make it visible. So far so good... (Hope you understood what I wanted to tell^^)
But it would be nice to have something like a screensaver-mode. What I want is:
When a user does nothing for , lets says, 2 minutes, then the Storyboard should be run that brings you back to the startscreen-view.
How would this work??
You can use DispatcherTimer to track the Application Idle time. Start this timer whenever you are in the screens except StartScreen. Reset the timer to zero whenever the window have mouse events. Set Timer duration to 2 mins, so that on timer callback make the startscreen visible.

Animate a e.Graphics.DrawLine with arrowhead

I need to "animate" an arrow. It can go in any direction (up, down, left, right and diagonals) and the arrowhead will be at the end. It needs to appear as if it is growing from orgin to the end.
To make matters more complicated it has to be in a user control so it is possible to add to the controls collection in the forms and remove it to delete it from the screen, and its background is really transparent (no color.transparent) so i can't paint the background to "clear" the previous line.
I have it static already (drawn from origin to end) but i can't animate it. I tried to add a timer in the user control but I fail to get a correct algorithm to calculate the intermediate ends of the line while it is growing.
Any help would be appreciated.
Thanks
When using animation on a Windows form you have to use another thread. Look into using a background worker: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
You don't need another thread - I have succesfully implemented Windows Forms animation.
My design:
Use a Windows.System.Forms.Timer object to call a callback function 'Animate()' at regular intervals
the Animate() function updates a property of your arrow, and then calls Invalidate() on the windows control
this all happens in the same UI thread, so yuo will not get any flicker effects (as long as your control has double duffering switched on).

Categories

Resources