Windwos服务实施 [英] Windwos Service Implementation

查看:82
本文介绍了Windwos服务实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我一直在开发一个小项目.该项目有一个计时器,该计时器在每个指定的时间间隔(例如每12个小时或24个小时)引发一个事件,以生成一个来自数据库的文件,此操作已经完成,并且工作正常.但是,我希望将其包含在Windows服务程序中,我尝试了一下,但是它没有按我希望的那样工作.

帮帮我,我该怎么办?

好的,这就是我尝试做的事情.我在下面放置的示例代码根本不是我的.它摘自The Code Project.其余代码与我改编的内容完全匹配,因此没有包括在内.

Hallo Everyone,

I have been developing a little project. The project has a timer which raises an event at every specified time interval, like every 12 hours or 24 hours, so as to produce a file which comes from a database, this has been done, and it works fine. But, I wish to emebed it in a windows service program, I tried but it doesn''t work as I wished.

Help me, what can I do?

Ok, here it is how I tried to do. The sample code I put below is not mine at all. It is taken from The Code Project. The rest of the code is matches exactly from what I adapted, so I didn''t include it.

<pre lang="msil">using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Collections.Specialized;
using System.Configuration;
using System.Data.SqlClient;
using System.Threading;
    public class Lite : System.ServiceProcess.ServiceBase
    {
        private System.Diagnostics.EventLog LiteEventLog;
        private IContainer components;
        NameValueCollection appSettings = ConfigurationManager.AppSettings;
        static TimerCallback timerDelegate;
        static Timer timer;
        public Lite()
        {
            InitializeComponent();

            if(!System.Diagnostics.EventLog.SourceExists("LiteSource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("LiteSource","Lite");
            }
            LiteEventLog.Source = "LiteSource";
            LiteEventLog.Log = "Lite";
        }

        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Lite()};
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
        private void InitializeComponent()
        {
            this.LiteEventLog = new System.Diagnostics.EventLog();
            ((System.ComponentModel.ISupportInitialize)(this.LiteEventLog)).BeginInit();
            this.LiteEventLog.Log = "Application";
            this.LiteEventLog.Source = "LiteSource";
            this.ServiceName = "Lite";
            ((System.ComponentModel.ISupportInitialize)(this.LiteEventLog)).EndInit();
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                    timer.Dispose();
                }
            }
            base.Dispose( disposing );
            timer.Dispose();
        }

        protected override void OnStart(string[] args)
        {
            LiteEventLog.WriteEntry(" Lite started.");
            Lite lite = new Lite();
            AutoResetEvent autoEvent = new AutoResetEvent(false);
            timerDelegate = new TimerCallback(lite.OnTimedEvent);
            timer = new Timer(timerDelegate, autoEvent, 1000, 1000);
            autoEvent.WaitOne();
        }

        protected override void OnStop()
        {
            LiteEventLog.WriteEntry(" Lite stoped.");
            timer.Dispose();
        }
        protected override void OnContinue()
        {
            LiteEventLog.WriteEntry(" Lite working.");
        }
        public void OnTimedEvent(object source)
        {
            SqlConnection Sqlca;
            string stmt = appSettings["sqlStmt"];
            try
            {
                string connectString = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
                if (null != connectString)
                {
                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);
                    Sqlca = new SqlConnection(builder.ConnectionString);
                    Sqlca.Open();
                    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(stmt, Sqlca);
                    DataSet ds_location = new DataSet();
                    DataTable dt_loc = new DataTable("Table_1");
                    ds_location.DataSetName = "Dataset_1";
                    mySqlDataAdapter.Fill(dt_loc);
                    ds_location.Tables.Add(dt_loc);
                    dt_loc.Columns[0].ColumnMapping = MappingType.Attribute;
                    dt_loc.Columns[1].ColumnMapping = MappingType.Attribute;
                    ds_location.WriteXml("Sample.xml");
                    Sqlca.Close();
                }
            }
            catch (SqlException sqlEX)
            {
                Console.WriteLine(sqlEX);
            }
        }
    }







谢谢







Thanks

推荐答案

您可能想开始使用此简单的
You might want to start using this simple walkthrough[^].


这篇关于Windwos服务实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆