zoukankan      html  css  js  c++  java
  • 在Window服务中应用Timer

    在Windwo服务中应用Timer记的要把System.Windows.Forms.Timer timer1改成System.Timers.Timer timer1。事件使用timer1_Elapsed,而不是time1_Tick

      #region 组件设计器生成的代码

            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.timer1 = new System.Timers.Timer();
                ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
                //
                // timer1
                //
                this.timer1.Enabled = true;
                this.timer1.Interval = 1000;
                this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
                //
                // myService
                //
                this.ServiceName = "myService";
                ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

            }

            #endregion

            //private System.Windows.Forms.Timer timer1;
            private System.Timers.Timer timer1;
        }

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Data.SqlClient;

    namespace myService
    {
        public partial class myService : ServiceBase
        {
            public myService()
            {
                InitializeComponent();
               
            }

            protected override void OnStart(string[] args)
            {
                timer1.Enabled = true;
                timer1.Start();
            }

            protected override void OnPause()
            {
                timer1.Stop();
            }

            protected override void OnContinue()
            {
                timer1.Start();
            }

            protected override void OnStop()
            {
                timer1.Stop();
            }

            protected void exe()
            {
                SqlConnection con = new SqlConnection("server=PC-201104141904\\SQL2005;database=myDataBase;user id=sa;password=sasasa");
                string sql = "insert into tb_User(UserName,Password) values('name','password')";
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

            private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                exe();
            }
        }
    }

  • 相关阅读:
    webservice未能加载文件或程序集“**.DLL”或它的某一个依赖项。
    四方在线打印控件使用(简介)
    C#图片处理高级应用(裁剪,缩放,清晰度,水印)
    基于Socket实现TCP/IP通讯
    委托
    FTP主配置文件详解
    关于解决RedHat6.0以上版本:Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
    Markdown学习
    网页HTML1,第一天学习。
    实现公众号留言的微信小程序--欢迎大家多多交流
  • 原文地址:https://www.cnblogs.com/zhuawang/p/2073207.html
Copyright © 2011-2022 走看看