timer, windows service

  • Thread starter Thread starter andrewcw
  • Start date Start date
A

andrewcw

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
 
you can use System.Threading.Timer, here's an example

///// start of code //////////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////////////////////////////
using System;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;

namespace SimpleTime
{
public class SimpleTimerSrv : System.ServiceProcess.ServiceBase
{
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.
InitializeComponent();

if (!System.Diagnostics.EventLog.SourceExists("Application"))
{
System.Diagnostics.EventLog.CreateEventSource("SimpleTimerService","Application");
}

eventLog1.Source = "SimpleTimerService";
eventLog1.Log = "Application";
}

// 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.ServiceProcess.ServiceBase[] {new SomeService(), new MySecondUserService()};
//
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new SimpleTimerSrv() };

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()
{
//
// SimpleTimerService
//
this.ServiceName = "SimpleTimerService";
}
/// <summary>
/// Setup the service.
/// </summary>
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("Start MySimpleTimer Service");

//
// Create the delegate that invokes methods for the timer.
//
s.interval = 100; // 1000 = one second
TimerCallback timerDelegate = new TimerCallback(CheckStatus);
System.Threading.Timer _timer = new System.Threading.Timer(timerDelegate, 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(Object state)
{
//
// stop timer
//
s.tmr.Change(System.Threading.Timeout.Infinite, s.interval);
//
// do something
//
myclass.DoSomethingMethod();
//
// 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.Threading.Timer tmr;
public long interval = 0;
}
}
///// end of code //////////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////////////////////////////
 
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-us/vbcon/html/vboriCreatingConfiguringW
indowsServiceApplications.asp

Also, you can get a sample code at:
http://www.codeproject.com/useritems/simplewindowsservice.asp?target=windows
+service%7C%2Enet

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" <[email protected]>
| Sender: "andrewcw" <[email protected]>
| Subject: timer, windows service
| Date: Tue, 22 Jul 2003 13:43:03 -0700
| Lines: 4
| Message-ID: <[email protected]>
| 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: AcNQkdjHhgNP29G6RzqilDuX01q/UQ==
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:171032
| NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
| X-Tomcat-NG: microsoft.public.dotnet.languages.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
|
 
Back
Top