Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
In my WPF project I've done with clicking some UI element such stuff:

C#
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

1 solution

Hi i think you have choosen a bad approach with usage of Dispatcher timer, first of all read what MSDN said about it:
<br />
The DispatcherTimer is reevaluated at the top of every DispatcherTimer loop.<br />
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::

C#
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 :

C#
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();

       }
   }
 
Share this answer
 
v2
Comments
Oleg Orlov 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 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 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 26-Dec-12 9:17am    
So if my solution will be helpful ,please accept solution, and RATE this post ;)
Oleg Orlov 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)



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