2012年12月8日 星期六

Disable event trigger for a fixed period

OS : Windows 7 x64
Tool: Visual Studio 2010 ultimate SP1
WPF application
1. To make 2  window events as Load and MouseWheel, the former is used to set the fixed period timer while the latter is used to generate the simulate event.
2. The trigger of MouseWheel will sent now to the title of window.


private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            this.Title += " " + DateTime.Now.Millisecond.ToString();
            EventOn = false;
            this.MouseWheel -= new MouseWheelEventHandler(Window_MouseWheel);
            t1.Stop();
            t1.Start();
        }
        System.Windows.Threading.DispatcherTimer t1 = new System.Windows.Threading.DispatcherTimer();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            t1.Interval = new TimeSpan(0, 0, 0, 0, 200); //200 miliseconds
            t1.Tick += new EventHandler(t1_Tick);
            t1.Start();
        }

        bool EventOn = true;
        void t1_Tick(object sender, EventArgs e)
        {
            //this.Title += DateTime.Now.ToString();
            if (EventOn)
            {
                EventOn = false;
                this.MouseWheel -= new MouseWheelEventHandler(Window_MouseWheel);
            }
            else
            {
                EventOn = true;
                this.MouseWheel += new MouseWheelEventHandler(Window_MouseWheel);
            }
        }
3. Example shows the period may not being exactly 200 ms.


沒有留言:

張貼留言