Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
In my WPF project I've done with clicking some UI element such stuff:
 
    void vb1_click(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        DispatcherTimer dt = new DispatcherTimer();
        dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
        dt.Tick += new System.EventHandler(dt_Tick);
        dt.Start();
    }
 
    void dt_Tick(object sender, System.EventArgs e)
    {
        for(int i = 0; i < 20; i++)
        {
            this.vb2_blur_eff.Radius = (double)i;
        }
    }
 
The main problem is: "I can't see the each step of blur-effect per second. The program is idling and when the process is complete it gives me the final step of bluring the UI element.
 
I don't want it, cause both program idling is looking ugly and I want actually see the rendered result at the each period.
Posted 26-Dec-12 3:41am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi i think you have choosen a bad approach with usage of Dispatcher timer, first of all read what MSDN said about it:

The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.
Timers are not guaranteed to execute exactly when the time interval occurs, but they are guaranteed to not execute before the time interval occurs. This is because DispatcherTimer operations are placed on the DispatcherTimer queue like other operations. When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities.

 
Improvements::
 
public static class ApplicationHelper
	{
		[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		public static void DoEvents(DispatcherPriority priority)
		{
			DispatcherFrame frame = new DispatcherFrame();
			DispatcherOperation oper = Dispatcher.CurrentDispatcher.BeginInvoke(priority,
				new DispatcherOperationCallback(ExitFrameOperation), frame);
 
			Dispatcher.PushFrame(frame);
			if (oper.Status != DispatcherOperationStatus.Completed)
			{
				oper.Abort();
			}
		}
 
		private static object ExitFrameOperation(object obj)
		{
			((DispatcherFrame)obj).Continue = false;
			return null;
		}
 
		[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		public static void DoEvents()
		{
			DoEvents(DispatcherPriority.Background);
		}
	}
 
So , let's summarize :
 
void vb1_click(object sender, System.Windows.Input.MouseButtonEventArgs e)
   {
       DispatcherTimer dt = new DispatcherTimer();
       dt.Interval = new TimeSpan(0, 0, 0, 0, 1000);
       dt.Tick += new System.EventHandler(dt_Tick);
       dt.Start();
   }
 
   void dt_Tick(object sender, System.EventArgs e)
   {
       for(int i = 0; i < 20; i++)
       {
           this.vb2_blur_eff.Radius = (double)i;
           ApplicationHelper.DoEvents();
 
       }
   }
  Permalink  
v2
Comments
Oleg Orlov at 26-Dec-12 8:57am
   
I see. Thanks. Can you give a piece of advice how to solve this problem? Thank you. As I have read. Using System.Threading for WPF/Silverlight isn't good and many articles provides to use System.Windows.Threading
Oleksandr Kulchytskyi at 26-Dec-12 9:05am
   
Try to do the next, set the Dispatcher priority. ->
DispatcherTimer(System.Windows.Threading.DispatcherPriority.Render);
and now i'm trying to provide you with some helper code to force push messages in Dispatcher loop (wait few minutes)
Oleksandr Kulchytskyi at 26-Dec-12 9:12am
   
So if substitution of Dispatcher priority doesn't help, so we need to enforce Dispatcher loop with help of the code above in my solution
Oleksandr Kulchytskyi at 26-Dec-12 9:17am
   
So if my solution will be helpful ,please accept solution, and RATE this post ;)
Oleg Orlov at 26-Dec-12 9:34am
   
Nah... It doesn't render smoothly. Program isn't idling, but the rendering process doesn't show the result step by step. Relly it does quickly render at the end but all the period, which is defined in TimeSpan ( 500 ms or another ) program is waiting and only it the end render each step in a very quick speed, but NOT smooth.

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

  Print Answers RSS
0 OriginalGriff 274
1 Shweta N Mishra 216
2 PIEBALDconsult 210
3 Sergey Alexandrovich Kryukov 185
4 BillWoodruff 174
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 26 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100