
DispatcherTimer is a timer which is integrated with the Dispatcher queue. It is available under the System.Windows.Threading Namespace. It is simply a class, but it is not typically used in the XAML file.
The DispatcherTimer is not guaranteed to execute a work item, when the DispatcherTimer time interval occurs, but they are guaranteed not to execute a work item before the DispatcherTimer time interval occurs. The reason why is that the DispatcherTimer operation are placed in the Dispatcher queue like other operations.When the DispatcherTimer operation executes is dependent on the other jobs in the queue and their priorities. The higer priorities jobs execute first and the the lower priorities.
Note:- You can explicitly set the priority of the jobs, using the DispatcherPriority Enumeration.
DispatcherPriority describes the priorities at which operations can be invoked by way of the Dispatcher.
The followings are the value of the DispatcherPriority enumeration
public enum DispatcherPriority
Member Discription
- Invalid:- The enumeration value is -1. This is an invalid priority.
- Inactive:- The enumeration value is 0. Operations are not processed.
- SystemIdle:- The enumeration value is 1.Operation is processed when the system is idle.
- ApplicationIdle:- The enumeration value is 2. Operation is processed when the app is idle.
- ContextIdle:-The enumeration value is 3. Operations are processed after background operations have completed.
- Background:-The enumeration value is 4. Operations are processed after all other non-idle operations are completed.
- Input:-The enumeration value is 5. Operations are processed at the same priority as input.
- Loaded:-The enumeration value is 6. Operations are processed when layout and render has finished but just before items at input priority are serviced. Specifically this is used when raising the Loaded event.
- Render:-The enumeration value is 7. Operations processed at the same priority as rendering.
- DataBind:-The enumeration value is 8. Operations are processed at the same priority as data binding.
- Normal:-The enumeration value is 9. Operations are processed at normal priority. This is the typical application priority.
- Send:-The enumeration value is 10. Operations are processed before other asynchronous operations. This is the highest priority.
These are the following constructor of DispatcherTimer to construct an new instance of the DispatcherTimer.
- DispatcherTimer() :- It is a no parameter constructor to initialize a new instance of the DispatcherTimer class.
- e.g.
class DispatcherTimerDemo
{
DispatcherTimer timer=new DispatcherTimer();
}
- DispatcherTimer(DispatcherPriority) :-
e.g.
System.Windows.Threading
class DispatcherTimerDemo
{
DispatcherTimer timer=new DispatcherTimer(DispatcherPriority.Normal);
}
- DispatcherTimer(DispatcherPriority,Dispatcher) :-
e.g.
System.Windows.Threading
class DispatcherTimerDemo
{
DispatcherTimer timer=new DispatcherTimer(DispatcherPriority.Normal, Dispatcher.CurrentDispatcher);
}
- DispatcherTimer(TimeSpan,DispatcherPriority,EventHandler,Dispatcher) :-
e.g.
System.Windows.Threading
class DispatcherTimerDemo
{
DispatcherTimer timer=new DispatcherTimer(new TimeSpan(0,0,1),DispatcherPriority.Normal,new EventHandler(dispatcherTimer_Tick), Dispatcher.CurrentDispatcher);
}
Let's understad the DispatcherTimer, the picture helps you:-
Complete Example:-
Using System.Windows.Threading
namespace DispatcherTimerDemo
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
int minute = 0;
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer time = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, new EventHandler(time_Tick), Dispatcher.CurrentDispatcher);
time.Tick += new EventHandler(time_Tick);
time.Start();
minute = DateTime.Now.Minute;
}
void time_Tick(object sender, EventArgs e)
{
lblTime.Content = DateTime.Now.ToLongTimeString();
int mnt = DateTime.Now.Minute;
if (mnt - minute == 1)
{
DoWork()//This function at an interval of One minute.
minute = DateTime.Now.Minute;
}
}
}
}
Note:- The above code is written in the WPF Application using c#.
No comments:
Post a Comment