실버라이트의 Storyboard를 이용하면 간단하게 타이머를 만들수있다;

물론 System.Windows.Threading.DispatcherTimer 를 이용해도 같은 효과를 낼수있다!

매우 간단한 예제인지라 소스코드만 보면 금새 알수 있을것이다!


using System;
using System.Windows;
using System.Windows.Controls;
//실버라이트 스토리보드를 이용하기 위해 using!
using System.Windows.Media.Animation;

//실버라이트 스토리보드를 이용한 타이머 예제
namespace MyTimer
{
    public partial class Page : UserControl
    {
        Storyboard timer = new Storyboard();
        int cnt = 0;

        public Page()
        {
            InitializeComponent();
            //타이머 간격 설정;
            timer.Duration = new Duration(new TimeSpan(0, 0, 1));

            //x:Name 설정            
            timer.SetValue(NameProperty, "StoryboardTimer");

            //타이머 종료 이벤트 등록
            timer.Completed += new EventHandler(timer_Completed);

            //타이머 시작
            timer.Begin();
        }

        //타이머 종료 이벤트
        void timer_Completed(object sender, EventArgs e)
        {
            txtResult.Text = string.Format("{0} TimeSpan..", ++cnt);
            //다시한번 타이머를 호출한다!
            timer.Begin();
        }
    }
}
Posted by 맨날맑음
,