VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C# 定时器定时更新的简单实例

如下所示:
复制代码 代码如下:

class Program
    {

        static void Main(string[] args)
        {
            //for (int i = 0; i < 100; i++)
            //{

            //    SendMessage("13161626306", "13161626306");
            //}
            System.Timers.Timer aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(aTimer_Elapsed);
            // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒)
            aTimer.Interval = 300;  //设置时间间隔
            aTimer.Enabled = true;
            Console.WriteLine("按回车键结束程序");
            Console.WriteLine(" 等待程序的执行......");
            Console.ReadLine();

   
            //SendMessage("13161626306", "13161626306");
        }

        static void aTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            SendMessage("13161626306", "13161626306");
        }

     
        private static void SendMessage(string PhoneNum, string Message)
        {
            try
            {

 
                using (ServiceReference1.WcfServiceClient sum = new ConsoleApplication1.ServiceReference1.WcfServiceClient())
                {
                   ServiceReference1.MessageOk ok = new ConsoleApplication1.ServiceReference1.MessageOk();

                        ok.Message = Message;
                        ok.Phone = PhoneNum;

                        ServiceReference1.MessageOk NewMessage = sum.Ok(ok);

 
                        Console.WriteLine(NewMessage.Message);
                        Console.WriteLine("22");

                    //Console.ReadLine();
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.ReadKey();
            }
        }

    }

相关教程