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("Applic ation"))
{
System.Diagnostics.EventLog.CreateEventSource("Sim pleTimerService","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(ServicesToRu n);
}
/// <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 //////////////////////////////////////////////////////////
///
//////////////////////////////////////////////////////////////////////////////
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message news:O$**************@TK2MSFTNGP12.phx.gbl...
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" <an************@boeing.com> wrote in message
news:04****************************@phx.gbl... 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