欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 定时器定时更新的简单实例

程序员文章站 2024-02-19 21:24:52
如下所示:复制代码 代码如下:class program     {       ...
如下所示:
复制代码 代码如下:

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();
            }
        }

    }