473,408 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
Nov 15 '05 #1
2 15675
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


Nov 15 '05 #2

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
indowsServiceApplications.asp

Also, you can get a sample code at:
http://www.codeproject.com/useritems...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" <an************@boeing.com>
| Sender: "andrewcw" <an************@boeing.com>
| Subject: timer, windows service
| Date: Tue, 22 Jul 2003 13:43:03 -0700
| Lines: 4
| Message-ID: <04****************************@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: 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
|

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ray | last post by:
Hi, I just wrote a windows service application to call a function of another object periodically. I used System.Server.Timer and I found that it works fine within the first 2 hours but the...
7
by: J. Hill | last post by:
I have a Windows Service with a timer but the .Tick event is not being fired/called. Don't know what code to include...I enabled and started the timer...I have the exact same code in a Windows...
5
by: Dhilip Kumar | last post by:
Hi all, I have developed a windows service using the windows service project template in VS.NET. I have used three controls in the service, a timer, performance counter and a message queue...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
9
by: archana | last post by:
Hi all, I want to know about interval of timer. I am using timer in windows service.I head somewhere that when i set interval property of timer while setting interval, restart time of Pc is...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
10
by: igor | last post by:
I have recently discovered that the system.Timers.Timer from.Net Framework v1.1 is not reliable when used on Windows 2003 server. When incorporated into a Windows Service, the timer_elapsed event...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.