Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
Posted
Updated 13-Mar-11 20:49pm
v4
Comments
JF2015 14-Mar-11 2:15am    
Whithout showing us your code and telling us what is not working, we can't help you. Please modify your question to explain a bit more.
Sergey Alexandrovich Kryukov 14-Mar-11 2:17am    
Are you serious? How can you imagine the answer to something that nobody can see? Severe overwork?
--SA

1 solution

You might want to start using this simple walkthrough[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900