What is a Windows Service?
Windows Service is nothing but process running in background without our knowledge. It mostly won't require User interaction. If we type "services.msc" in Run of Start Menu, we can see what are the services running in our system. Some will start automatically, when system starts. But some services has to be started by us manually.
Advantages of Windows Service:
1. It can be run automatically.
2. It wont require User-Interaction
3. It runs in background
Normally, Windows Service is used to run time-consuming processes, like taking backup of a database.
Now, we are going to design a windows service to record startup and shutdown timings of your system. I had designed this application using Visual Studio 2003.
Step 1:
First open Visual Studio and select Visual C# Projects. Select Template as Windows Service. Give name as Monitoring as shown in figure below:
Step 2:
Open Code Window by pressing F7. Next include using System.IO namespace for writing system timings into a file. Next find Service1 word and replace all occurrences with Monitoring word. It will change namespace, classname, constructor name... to Monitoring. After that go to design mode(by pressing shift+F7). Select Solution Explorer(by pressing Ctrl+Alt+L). Click on Service1.cs and rename it to Monitoring.cs.
After that go to code window. And write this code in OnStart event.
As shown in figure:
I will explain what I am doing in OnStart event:
First create a xml file in c drive with file1 as name.And write this code in that file:
And close it.
I am creating a service, which will start automatically start and record startup time. It will shutdown, when system shutdowns. And it will record shutdown time and time spent on system.
First, I am creating StreamWriter to write startup time of system into file1.xml.
After copying the code to your OnStart event. Create a public variable temp, just above Monitoring Constructor as shown below:
After that, copy this code into your code window:
Copy this code to OnStart event as shown in figure:
StreamWriter writer=File.AppendText("d:\\file1.xml"); writer.Write("");
writer1.WriteLine("
writer1.Close();
DataSet ds=new DataSet();
ds.ReadXml("c:\\temp1.xml");
TimeSpan t=new TimeSpan();
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
XmlTextReader reader=new XmlTextReader("c:\\temp1.xml");
while(reader.Read())
{
if(reader.NodeType==XmlNodeType.Element)
{
if(reader.Name=="timespent")
{
string temp11=reader.ReadInnerXml().ToString();
if(temp11!="undefined")
{
temp11=temp11.Replace(":",".");
temp11=temp11.Replace(" ","");
duration +=Convert.ToDouble(temp11);
}
}
}
}
Response.Write("Total Duration is : ");
double temp122=Convert.ToDouble(duration);
string hr=temp122.ToString();
string hrstr=hr.Substring(0,hr.IndexOf("."));
Response.Write(hrstr.ToString()+" Hours");
string mins=hr.Substring(hr.IndexOf(".")+1,(hr.Length -hr.IndexOf(".")-1));
Response.Write(" "+mins.ToString()+" Minutes");
reader.Close();
File.Delete("c:\\temp1.xml");
Next, paste this declaration above Page_Load event:
private double duration;
I will explain what I have done in Page_Load event:
First, I copied contents of file1.xml(which contains timings of system) to temporary file. Then, I am appending some tag to that temp file. Afterwards, I am reading contents of temp into a dataset and finally binding it to a DataGrid. Afterwards, I written logic to find total time spent. I hope, everyone can understand that code.
Finally, the page output will be like this:

No comments:
Post a Comment