Results 1 to 2 of 2

Thread: Windows Service

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Angry Windows Service

    Hi there, this is a strange question, I have written a windows service using C# that simply inserts a row of data to a SQL Server Database, nothing to complicated...

    Once the service is started i looked in the task manager and it say that my service is using almost 11,000 kb, is this right? should a simple service use so much memory?

    Whats the answer?

    Thanks Rohan

    using System;
    using System.Data.SqlClient;
    using System.Diagnostics;

    namespace DemoService
    {
    public class Service1 : System.ServiceProcess.ServiceBase
    {
    private System.Timers.Timer timer1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Service1()
    {
    // This call is required by the Windows.Forms Component Designer.
    InitializeComponent();

    // TODO: Add any initialization after the InitComponent call
    }

    // The main entry point for the process
    static void Main()
    {
    System.ServiceProcess.ServiceBase[] ServicesToRun;

    // More than one user Service may run within the same process. To add
    // another service to this process, change the following line to
    // create a second service object. For example,
    //
    // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
    //
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };

    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.timer1 = new System.Timers.Timer();
    ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
    //
    // timer1
    //
    this.timer1.Interval = 30000;
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
    //
    // Service1
    //
    this.ServiceName = "Service1";
    ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    /// <summary>
    /// Set things in motion so your service can do its work.
    /// </summary>
    protected override void OnStart(string[] args)
    {
    this.timer1.Enabled = true;
    this.LogMessage("Service Started");
    }

    /// <summary>
    /// Stop this service.
    /// </summary>
    protected override void OnStop()
    {
    this.timer1.Enabled = false;
    this.LogMessage("Service Stopped");
    }

    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    this.LogMessage("Service Running");
    }

    /// <summary>
    /// Save the message to Database, and report errors.
    /// </summary>
    private void LogMessage(string Message)
    {
    SqlConnection conn = null;
    SqlCommand cmd = null;
    try
    {
    conn = new SqlConnection("Server=TESTINTRANET\\TESTINTRANET;Integrated Security=SSPI;Database=MyService");
    cmd = new SqlCommand("INSERT INTO MyServiceLog (vc_Status,dt_Created) VALUES ('" + Message + "',getdate())",conn);
    conn.Open();
    int numrows = cmd.ExecuteNonQuery();
    conn.Close();
    Console.WriteLine("Total Rows {0}", numrows.ToString());
    }
    catch(Exception ex)
    {
    if(!EventLog.SourceExists("MySource"))
    {
    EventLog.CreateEventSource("MySource", "MyNewLog");
    Console.WriteLine("CreatingEventSource");
    }
    EventLog myLog = new EventLog();
    myLog.Source = "MySource";
    myLog.WriteEntry(ex.Message);

    }
    finally
    {
    cmd.Dispose();
    conn.Dispose();
    }
    }
    }
    }

  2. #2
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    That isn't too bad, look at the other processes running, on my computer explorer is using 24000 kb and so is IE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width