Asked By shweta
13-May-10 08:00 AM

My window has onstart method as:
protected override void OnStart(string[] args)
{
// Interval in milliseconds
string myInterval = System.Configuration.ConfigurationManager.AppSettings["ServiceInterval"].ToString();
double interval = Convert.ToDouble(myInterval);
// Create timer object
MyTimer = new System.Timers.Timer(interval);
MyTimer = new System.Timers.Timer();
// Set to run more than once
MyTimer.AutoReset = true;
MyTimer.Enabled = true;
// Delegate to handle the timer event
MyTimer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
// Start the timer
MyTimer.Start();
// Prevents garbage collection from occuring in long running methods
GC.KeepAlive(MyTimer);
// TODO: Add code here to start your service.
//code for date check
eventLog1.WriteEntry("ExactService started");
}
I want to Have my OnStart method kick off a background thread where a separate class is instantiated that has the methods i want for my business logic.
how to do this.
I am doing so coz the window service is not working properly :
The existing code......
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.Collections;
using System.Configuration;
using System.Timers;
using System.Xml;
using System.IO;
using System.Web;
using System.Globalization;
namespace ExactService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
//code to set timer
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("ExactServiceLog"))
System.Diagnostics.EventLog.CreateEventSource("ExactServiceLog",
"ESLOG");
eventLog1.Source = "ExactServiceLog";
// the event log source by which
//the application is registered on the computer
eventLog1.Log = "ESLOG";
//code ends
}
private System.Timers.Timer MyTimer;
protected override void OnStart(string[] args)
{
// Interval in milliseconds
string myInterval = System.Configuration.ConfigurationManager.AppSettings["ServiceInterval"].ToString();
double interval = Convert.ToDouble(myInterval);
// Create timer object
MyTimer = new System.Timers.Timer(interval);
MyTimer = new System.Timers.Timer();
// Set to run more than once
MyTimer.AutoReset = true;
MyTimer.Enabled = true;
// Delegate to handle the timer event
MyTimer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
// Start the timer
MyTimer.Start();
// Prevents garbage collection from occuring in long running methods
GC.KeepAlive(MyTimer);
// TODO: Add code here to start your service.
//code for date check
eventLog1.WriteEntry("ExactService started");
}
/// <summary>
///
/// </summary>
private void RunService()
{
string fileName = string.Format("customers_{0}.xml", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
CustomerCollection customers = GetCustomers();
string xml = ExactXMLFormatter.ExportCustomersToXML(customers);
WriteResponseXML(xml, fileName);
eventLog1.WriteEntry("Xml File written.");
}
public static void WriteResponseXML(string xml, string Filename)
{
if (!String.IsNullOrEmpty(xml))
{
String strFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/" + Filename;
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
document.Save(strFilePath);
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
MyTimer.Enabled = false;
MyTimer.Stop();
string startTimeString = ConfigurationSettings.AppSettings["StartTime"].ToString();
DateTime startTime;
startTime = DateTime.Now;
//If timestamp = myDateTime.ToString("HH:mm")
if(startTimeString==startTime.ToString("HH:mm"))
{
RunService();
}
}
catch (Exception ex)
{
System.IO.File.AppendAllText("C:\\CustomerList.txt", "Exact service threw Exception " + ex.Message + "\r\n");
}
finally
{
GC.Collect();
MyTimer.Start();
}
}
protected CustomerCollection GetCustomers()
{
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created at the follwing location: Customers started.\r\n");
string customerType = string.Empty;
if (customerType == string.Empty)
{
customerType = "Admin,Office,Customer";
}
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created at the follwing location: Customers 2nd step.\r\n");
DateTime startDate = DateTime.Now;
DateTime endDate = DateTime.Now;
CustomerCollection customers = null;
////CustomerID Inserted
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created at the follwing location: Customers 3rd step.\r\n");
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created at the follwing location: Customers 4th step." + startDate + ", " + endDate + ", " + int.MaxValue + ", " + customerType + "\r\n");
int totalRecords = 0;
customers = CustomerManager.GetAllCustomersByDate(startDate,
endDate, "", "", 2147483, 0, out totalRecords, customerType);
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created at the follwing location: Customers fetched.\r\n");
return customers;
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
eventLog1.WriteEntry("Exact Service stopped");
MyTimer.AutoReset = false;
MyTimer.Enabled = false;
}
}
}