Timer 类 计时器

代码
   
     
1 /*
2 * class Example
3 {
4 public static void Demo(System.Windows.Controls.TextBlock outputBlock)
5 {
6 outputBlock.Text += "This message is never seen by users.\n";
7 }
8 }
9 */
10   using System;
11   using System.Threading;
12
13   // The following Imports are not required for the timer. They merely simplify
14 // the code.
15 using System.Windows.Controls;
16 using System.Windows.Input;
17
18 // The Example class holds a reference to the timer, and contains the
19 // event handler for the MouseLeftButtonUp events that control the demo.
20 //
21 public class Example
22 {
23 // The static Demo method sets the starting message and creates an
24 // instance of Example, which hooks up the handler for the MouseLeftButtonUp
25 // event.
26 public static void Demo(TextBlock outputBlock)
27 {
28 outputBlock.Text += " Click to create the timer.\n " ;
29 Example dummy = new Example(outputBlock);
30 }
31
32
33 // Instance data for the demo.
34 private int phase = 0 ;
35 private Timer t;
36
37 public Example(TextBlock outputBlock)
38 {
39 // Hook up the mouse event when a new Example object is created. Note
40 // that this keeps garbage collection from reclaiming the Example
41 // object.
42 outputBlock.MouseLeftButtonUp += new MouseButtonEventHandler( this .MouseUp);
43 }
44
45 private void MouseUp( object sender, MouseButtonEventArgs e)
46 {
47 TextBlock outputBlock = (TextBlock) sender;
48
49 if (phase == 0 )
50 {
51 // On the first click, create the timer.
52 outputBlock.Text += " \nCreating the timer at " +
53 DateTime.Now.ToString( " h:mm:ss.fff " ) +
54 " , to start in 1 second with a half-second interval.\n " +
55 " Click to change the interval from 1/2 second to 1 second.\n\n " ;
56
57 // Create a timer that invokes the callback method after one second
58 // (1000 milliseconds) and every 1/2 second thereafter. The TextBlock
59 // that is used for output is passed as the state object. C# infers the
60 // delegate type, as if you had typed the following:
61 // new TimerCallback(MyTimerCallback)
62 //
63 t = new Timer(MyTimerCallback, outputBlock, 1000 , 500 );
64
65 }
66 else if (phase == 1 )
67 {
68 outputBlock.Text += " \nChanging the interval to one second.\n " +
69 " Click to destroy the timer.\n\n " ;
70 t.Change( 0 , 1000 );
71 }
72 else
73 {
74 // On the last click, destroy the timer and shut down the demo.
75 outputBlock.Text += " \nDestroying the timer.\n " +
76 " Refresh the page to run the demo again. " ;
77 outputBlock.MouseLeftButtonUp -= new MouseButtonEventHandler( this .MouseUp);
78
79 t.Dispose();
80 }
81
82 phase += 1 ;
83 }
84
85
86 // The static callback method is invoked on a ThreadPool thread by the Timer.
87 // The state object is passed to the callback method on each invocation. In this
88 // example, the state object is the TextBlock that displays output. In order to
89 // update the TextBlock object, which is on the UI thread, you must make the
90 // cross-thread call by using the Dispatcher object that is associated with the
91 // TextBlock.
92 private static void MyTimerCallback( object state)
93 {
94 TextBlock outputBlock = (TextBlock) state;
95 string msg = DateTime.Now.ToString( " h:mm:ss.fff " ) + " MyTimerCallback was called.\n " ;
96
97 outputBlock.Dispatcher.BeginInvoke( delegate () { outputBlock.Text += msg; });
98 }
99 }
100
101 /* This example produces output similar to the following:
102
103 Click to create the timer.
104
105 Creating the timer at 3:40:17.712, to start in 1 second with a half-second interval.
106 Click to change the interval from 1/2 second to 1 second.
107
108 3:40:18.820 MyTimerCallback was called.
109 3:40:19.335 MyTimerCallback was called.
110 3:40:19.849 MyTimerCallback was called.
111
112 Changing the interval to one second.
113 Click to destroy the timer.
114
115 3:40:20.317 MyTimerCallback was called.
116 3:40:21.331 MyTimerCallback was called.
117
118 Destroying the timer.
119 Refresh the page to run the demo again.
120 */

 

  
    
< Grid x:Name ="LayoutRoot" Background ="White" >
< TextBlock x:Name = "outputBlock" FontSize ="12" TextWrapping ="Wrap" />

</ Grid >

 

  
    
public Page()
{
InitializeComponent();
Example.Demo(outputBlock);

}

 

你可能感兴趣的:(timer)