A Microsoft Windows service, formerly known as a NT service, enables you to create a long-running executable application that runs in a separate Windows session or we can configure the service to run in a user context other than a logged on user. Generally some tasks are required to be scheduled or dependent on the time. So this can be performed easily using a Windows service. Benefits of a Windows Service
Here are the steps to create a Windows service quickly:
Creating a Windows Service Project Using Visual Studio
Here is the sample code. If the available amount of RAM exceeds 500 Mb then a message is written in the log file on a specified interval.using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Text;using System.Timers;using System.IO; namespace serverMonitor{ partial class Service1 : ServiceBase { privateTimer timer1 = null; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { timer1 = newTimer(); this.timer1.Interval = 30000; this.timer1.Elapsed += newSystem.Timers.ElapsedEventHandler(this.timer1_Tick); timer1.Enabled = true; Log("Service Started successfully"); } protected override void OnStop() { timer1.Enabled = false; Log("Service Stopped successfully"); } privatevoid timer1_Tick(object sender, EventArgs e) { serverMonitor(); } public static void serverMonitor() { try { PerformanceCounter ramCounter; ramCounter = new PerformanceCounter("Memory", "Available MBytes"); if(ramCounter.NextValue() > 500) Log("Amount of the available RAM :" + ramCounter.NextValue() + "Mb"); } catch (Exception ex) { Log("Error =" + ex.ToString()); } } public static void Log(string str) { StreamWriter Tex = File.AppendText(@"d:\Log.txt"); Tex.WriteLine(DateTime.Now.ToString() + " " + str); Tex.Close(); } }}Creating an Installer
Deploying Service
Note: If we select the 'Account' type as 'User', the application will prompt for username and word in which the context service needs to run.Verifying whether service works
Un-installing the Service
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: