timer, windows service

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

    #1

    timer, windows service

    I am trying to do a windows service with C#. I am using as
    a base the VB.NET article in VS, but I thing the
    WITHEVENTS timer notation is a delegate. Can anyone
    provide sample code & anh hints. Thanks Andrew
  • Peter

    #2
    Re: timer, windows service

    you can use System.Threadin g.Timer, here's an example

    ///// start of code //////////////////////////////////////////////////////////
    ///
    //////////////////////////////////////////////////////////////////////////////
    using System;
    using System.ServiceP rocess;
    using System.Threadin g;
    using System.Windows. Forms;
    using System.Diagnost ics;

    namespace SimpleTime
    {
    public class SimpleTimerSrv : System.ServiceP rocess.ServiceB ase
    {
    private static TimerState s;
    private static EventLog eventLog1 = new EventLog();
    private static MyClass myclass = new MyClass();

    public SimpleTimerSrv( )
    {
    // This call is required by the Windows.Forms Component Designer.
    InitializeCompo nent();

    if (!System.Diagno stics.EventLog. SourceExists("A pplication"))
    {
    System.Diagnost ics.EventLog.Cr eateEventSource ("SimpleTimerSe rvice","Applica tion");
    }

    eventLog1.Sourc e = "SimpleTimerSer vice";
    eventLog1.Log = "Applicatio n";
    }

    // The main entry point for the process
    static void Main()
    {
    //
    // create timer state
    //
    s = new TimerState();

    // 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 SomeService(), new MySecondUserSer vice()};
    //
    System.ServiceP rocess.ServiceB ase[] ServicesToRun;
    ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new SimpleTimerSrv( ) };

    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()
    {
    //
    // SimpleTimerServ ice
    //
    this.ServiceNam e = "SimpleTimerSer vice";
    }
    /// <summary>
    /// Setup the service.
    /// </summary>
    protected override void OnStart(string[] args)
    {
    eventLog1.Write Entry("Start MySimpleTimer Service");

    //
    // Create the delegate that invokes methods for the timer.
    //
    s.interval = 100; // 1000 = one second
    TimerCallback timerDelegate = new TimerCallback(C heckStatus);
    System.Threadin g.Timer _timer = new System.Threadin g.Timer(timerDe legate, s, 1000, s.interval);
    //
    // Keep a handle to the timer, so it can be disposed.
    //
    s.tmr = _timer;
    }
    /// <summary>
    /// Stop this service.
    /// </summary>
    protected override void OnStop()
    {
    s.tmr.Dispose() ;
    s.tmr = null;
    }

    /// <summary>
    /// The following method is called by the timer's delegate.
    /// </summary>
    /// <param name="state"></param>
    static void CheckStatus(Obj ect state)
    {
    //
    // stop timer
    //
    s.tmr.Change(Sy stem.Threading. Timeout.Infinit e, s.interval);
    //
    // do something
    //
    myclass.DoSomet hingMethod();
    //
    // restart timer
    //
    s.interval = 60 * 1000; // 1000 for each second
    s.tmr.Change(s. interval, s.interval);
    }
    }
    /// <summary>
    /// Keeps track of the timer
    /// </summary>
    class TimerState
    {
    public int counter = 0;
    public System.Threadin g.Timer tmr;
    public long interval = 0;
    }
    }
    ///// end of code //////////////////////////////////////////////////////////
    ///
    //////////////////////////////////////////////////////////////////////////////



    "Michael Culley" <mculley@NOSPAM optushome.com.a u> wrote in message news:O$SlkVLUDH A.1576@TK2MSFTN GP12.phx.gbl...[color=blue]
    > You can't use the windows forms timer. You need to use the one under
    > components. Be warned it does not wait for your code to complete before
    > firing again.
    >
    > --
    > Michael Culley
    >
    >
    > "andrewcw" <andrew.c.watts @boeing.com> wrote in message
    > news:040f01c350 91$d8c97ff0$a40 1280a@phx.gbl.. .[color=green]
    > > I am trying to do a windows service with C#. I am using as
    > > a base the VB.NET article in VS, but I thing the
    > > WITHEVENTS timer notation is a delegate. Can anyone
    > > provide sample code & anh hints. Thanks Andrew[/color]
    >
    >[/color]

    Comment

    • Jeffrey Tan[MSFT]

      #3
      RE: timer, windows service


      Hi Andrew,

      The .net framework provided a simple way of developing windows service
      application.
      To get more information, you can visit:
      http://msdn.microsoft.com/library/en...ngConfiguringW
      indowsServiceAp plications.asp

      Also, you can get a sample code at:

      +service%7C%2En et

      Hope it helps.

      Jeffrey Tan
      Microsoft Online Partner Support
      Get Secure! - www.microsoft.com/security
      This posting is provided "as is" with no warranties and confers no rights.

      --------------------
      | Content-Class: urn:content-classes:message
      | From: "andrewcw" <andrew.c.watts @boeing.com>
      | Sender: "andrewcw" <andrew.c.watts @boeing.com>
      | Subject: timer, windows service
      | Date: Tue, 22 Jul 2003 13:43:03 -0700
      | Lines: 4
      | Message-ID: <040f01c35091$d 8c97ff0$a401280 a@phx.gbl>
      | MIME-Version: 1.0
      | Content-Type: text/plain;
      | charset="iso-8859-1"
      | Content-Transfer-Encoding: 7bit
      | X-Newsreader: Microsoft CDO for Windows 2000
      | X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
      | Thread-Index: AcNQkdjHhgNP29G 6RzqilDuX01q/UQ==
      | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
      | Path: cpmsftngxa06.ph x.gbl
      | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1710 32
      | NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
      | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
      |
      | I am trying to do a windows service with C#. I am using as
      | a base the VB.NET article in VS, but I thing the
      | WITHEVENTS timer notation is a delegate. Can anyone
      | provide sample code & anh hints. Thanks Andrew
      |

      Comment

      Working...