Problem with win services

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maziar Aflatoun

    #1

    Problem with win services

    Hi evereyone,
    I'm trying to test this win service... Basically, I want to write a test
    windows service that updates a text file at some fixed time interval. Here
    is my code which doesn't work... it only writes it once.

    Any idea
    Thank you
    Maziar A.

    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Diagnost ics;
    using System.ServiceP rocess;
    using System.IO;

    namespace maWinService4 {
    public class Service1 : System.ServiceP rocess.ServiceB ase
    {
    private System.Windows. Forms.Timer timer1;
    private System.Componen tModel.IContain er components;
    private static FileStream fs = new FileStream(@"c: \temp\myfile.tx t" ,
    FileMode.OpenOr Create, FileAccess.Writ e);
    private static StreamWriter m_streamWriter = new StreamWriter(fs );

    public Service1(){
    // This call is required by the Windows.Forms Component Designer.
    InitializeCompo nent();
    // TODO: Add any initialization after the InitComponent call
    }

    // The main entry point for the process
    static void Main()
    {
    System.ServiceP rocess.ServiceB ase[] 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.ServiceP rocess.ServiceB ase[] {new Service1(),
    new MySecondUserSer vice()};
    //
    ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new Service1() };
    System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
    }
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.components = new System.Componen tModel.Containe r();
    this.timer1 = new System.Windows. Forms.Timer(thi s.components);
    //
    // timer1
    //
    this.timer1.Ena bled = true;
    this.timer1.Int erval = 1;
    this.timer1.Tic k += new System.EventHan dler(this.timer 1_Tick);
    //
    // Service1
    //
    this.ServiceNam e = "maWinService4" ;
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Disp ose();
    }
    }
    base.Dispose( disposing );
    }
    /// <summary>
    /// Set things in motion so your service can do its work.
    /// </summary>
    protected override void OnStart(string[] args)
    {
    // TODO: Add code here to start your service.
    // Create the source, if it does not already exist.
    // Write to the file using StreamWriter class
    m_streamWriter. BaseStream.Seek (0, SeekOrigin.End) ;
    m_streamWriter. Write(" File Write Operation Starts : ");
    m_streamWriter. WriteLine("{0} {1}", DateTime.Now.To LongTimeString( ),
    DateTime.Now.To LongDateString( ));
    m_streamWriter. WriteLine(" =============== =============== ======= \n");
    m_streamWriter. Flush();
    this.timer1.Sta rt();
    }
    /// <summary>
    /// Stop this service.
    /// </summary>
    protected override void OnStop()
    {
    // TODO: Add code here to perform any tear-down necessary to stop your
    service.
    this.timer1.Sto p();
    }
    private void timer1_Tick(obj ect sender, System.EventArg s e)
    {
    m_streamWriter. BaseStream.Seek (0, SeekOrigin.End) ;
    m_streamWriter. Write(" File Write Operation Starts : ");
    m_streamWriter. WriteLine("{0} {1}", DateTime.Now.To LongTimeString( ),
    DateTime.Now.To LongDateString( ));
    m_streamWriter. WriteLine(" =============== =============== ======= \n");
    m_streamWriter. Flush();
    }
    }
    }


Working...