zoukankan      html  css  js  c++  java
  • Thread Basics using Timer to trigger Event at a specified internals

    1. what's timer
    Timer provides a mechanism for executing a method in at specified intervals.
    the hierarchy can be the following:
    System.Threading.timer;
    System.Timer.timer;
    System.Windows.form.timer;

    2. relevant method about timer.

     public class AsynTest
        {
            private int result;//
    which is get randonly asynchrounousely by method GetResultAsync;
            ManualResetEvent endProcessEvent;

            public AsynTest(ManualResetEvent endprocessevent)
            {
                this.endProcessEvent = endprocessevent;
            }

            int GetResult()
            {
                Random rd = new Random();
                result =(int)(100*rd.Next())%100;
                Console.WriteLine("get value asynchronousely!");
                return result;
            }

            public delegate int GetResultDelegate();

            public void  GetResultAsync(object obj)
            {
                GetResultDelegate grd = new GetResultDelegate(GetResult);
                AsyncCallback cb = new AsyncCallback(this.GetIntergerfromCallBack);
                IAsyncResult ar = grd.BeginInvoke(cb, null);
                Console.WriteLine("result={0}",result);
                if (result%2 == 0)
                    Console.WriteLine("result is an even number");
            }

             //get the result asynchronousely
            
    public void GetIntergerfromCallBack(IAsyncResult ar)
            {
                try
                {
                    GetResultDelegate del = (GetResultDelegate)((AsyncResult)ar).AsyncDelegate;
                    del.EndInvoke(ar);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
               
            }


            static void Main(string[] args)
            {
                System.Threading.Thread.CurrentThread.Name = "MainThread";
               
    ManualResetEvent endProcessEvent = new ManualResetEvent(false);
                AsynTest hiron = new AsynTest(endProcessEvent);
               
    TimerCallback timercallback = new TimerCallback(hiron.GetResultAsync);
                Timer timer = new Timer(timercallback, i, 0,2000);
                //Blocks the current thread until the current waithhandle receives a signal
                endProcessEvent.WaitOne();
            }
    we can see such windows:



  • 相关阅读:
    第三章:使用listview控件展示数据
    第一章:初识Windows程序
    java MySQL数据库编程 第四章 高级查询(二)
    java MySQL数据库编程 第三章:高级查询(一)
    java MySQL数据库编程 第二章 :初识MySQL
    java MySQL数据库编程 第一章 数据库的设计
    波导缝隙天线
    微带天线读书计划(二)-Microstrip Antenna Design Handbook by Ramesh Garg etc.in 2001
    微带天线读书计划(三):MICROSTRIP AND PRINTED ANTENNAS by Debatosh Guha in 2011
    微带天线读书(一): Microstrip Antennas: The Analysis and Design of Microstrip Antennas and Arrays
  • 原文地址:https://www.cnblogs.com/Winston/p/1140168.html
Copyright © 2011-2022 走看看