- Create Window service project in C# - Part 1 (Create and launch)
- Create Window service project in C# - Part 2 (View event logs)
Now let's create a windows service application which use Timers. Because most of the windows services are running to execute specific task periodically. That's mean execute a code after specific time period without user interaction.
For that purpose you can use Timer component in C# which enables you to execute a code segment periodically. Let's see how this thing worked...
First you have to create a timer object on your service class. I am not going to further describe these things, because you will be able to understand by looking on following code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace MyWinService
{
public partial class MyService : ServiceBase
{
System.Timers.Timer tmrDelay;
int count;
public MyService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MyLogSrc"))
{
System.Diagnostics.EventLog.CreateEventSource("MyLogSrc", "MyLog");
}
//set our event log to system created log
myEventLog.Source = "MyLogSrc";
myEventLog.Log = "MyLog";
tmrDelay = new System.Timers.Timer(5000);
tmrDelay.Elapsed += new System.Timers.ElapsedEventHandler(tmrDelay_Elapsed);
}
void tmrDelay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string str = "Timer tick " + count;
myEventLog.WriteEntry(str);
count++;
}
protected override void OnStart(string[] args)
{
myEventLog.WriteEntry("MyService started");
tmrDelay.Enabled = true;
}
protected override void OnStop()
{
myEventLog.WriteEntry("MyService stoped");
tmrDelay.Enabled = false;
}
}
}
This code was placed on "Service1.cs" file on my project.
Code explained
System.Timers.Timer tmrDelay;
int count;
tmrDelay = new System.Timers.Timer(5000);
tmrDelay.Elapsed += new System.Timers.ElapsedEventHandler(tmrDelay_Elapsed);
void tmrDelay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
string str = "Timer tick " + count;
myEventLog.WriteEntry(str);
count++;
}
Here adds a new entity on event logger as "Timer tick 1" , 2, 3, 4 ....
Other codes are as it is on previous post. Therefore I am not going to explain those here.
Now build and install your solution as describe in Part 1. Then you will see some thing similar to this in "Event Viewer" or "Visual Studio".
As you can see when each time timer elapsed it will add new entity to logger. So now it's your turn place any code in timer elapsed event to execute it periodically.
Complete project can be downloaded from here.
Thanks Asava Samuel.
ReplyDeleteMilinda Pro: Create Window Service Project In C - Part 3 (Using Timers In A Service) >>>>> Download Now
ReplyDelete>>>>> Download Full
Milinda Pro: Create Window Service Project In C - Part 3 (Using Timers In A Service) >>>>> Download LINK
>>>>> Download Now
Milinda Pro: Create Window Service Project In C - Part 3 (Using Timers In A Service) >>>>> Download Full
>>>>> Download LINK
ağrı
ReplyDeletevan
elazığ
adıyaman
bingöl
UV3OB
whatsapp goruntulu show
ReplyDeleteshow
QSKAB6
görüntülü show
ReplyDeleteücretlishow
WD5JH
BA01A
ReplyDeleteOrdu Şehir İçi Nakliyat
Adıyaman Şehir İçi Nakliyat
Sui Coin Hangi Borsada
Muş Şehirler Arası Nakliyat
Azero Coin Hangi Borsada
Sinop Şehirler Arası Nakliyat
Pitbull Coin Hangi Borsada
Sinop Lojistik
Ankara Şehirler Arası Nakliyat
B2F55
ReplyDelete%20 referans kodu
B7F90
ReplyDeleteFacebook Takipçi Hilesi
Coin Nasıl Çıkarılır
Ort Coin Hangi Borsada
Binance Neden Tercih Edilir
Bitcoin Üretme
Bitcoin Yatırımı Nasıl Yapılır
Referans Kimliği Nedir
Spotify Dinlenme Hilesi
Parasız Görüntülü Sohbet