Application Scheduler Service Using C#.Net And XML
2007-03-03 17:05
429 查看
This is an application scheduler that is implemented as a Windows Service, similar to the Windows Task Scheduler - but simple, as it has fewer configuration options and it uses XML to store and retrieve data.
The program uses System.Timers, System.Threading and System.Diagnostics to repeatedly loop through the XML data to see whether an application is scheduled to run at the present time or not, and if yes, to launch it as a new process in a new thread.
The source
1using System;
2
3using System.Collections;
4
5using System.ComponentModel;
6
7using System.Data;
8
9using System.Diagnostics;
10
11using System.ServiceProcess;
12
13using System.Xml;
14
15using System.Timers;
16
17using System.Threading;
18
19using System.Configuration;
20
21using System.IO;
22
23
24
25namespace AppScheduler
26
27
333
I have created a class named [b]AppLauncher that accepts the executable name of a program as its constructor parameter. There is a method RunApp() in the class that creates a new ProcessInfo object with the specified path and calls Process.Start(ProcessInfo) with the ProcessInfo object as its parameter.
Class that launches applications on demand
1class AppLauncher
2
3
I had to create a separate class to launch an application in a new thread, because the Thread class in .Net 2003 does not allow you to pass parameters to a thread delegate (whereas you can do so in .Net 2005). The ProcessStartInfo class can be used to create a new process. The static method [b]Start (ProcessInfo) of the Process class returns a Process object that represents the process started.
There is a Timer variable used in the program, named _timer. The event handler for the timer's tick event is given below:
Event handler for the timer's tick event
1void timeElapsed(object sender, ElapsedEventArgs args)
2
3
71
[/b]
[/b]
An easy way to compare date and time disregarding some particular values such as hour of the day or minute or second: convert them to the appropriate string format first, and check whether the two strings are equal. Otherwise, you have to individually check each item you want to compare, like if(currTime.Day==runtime.Day && currTime.Month==runtime.Month && ...). The interval values are : "D" (for daily schedule), "W" (for weekly schedule), and "M" (for monthly schedule). The values are read from an XML file named AppScheduler.xml. The file format is given below:
The XML file containing list of applications to launch
1<?xml version="1.0" encoding="utf-8" ?>
2
3<!DOCTYPE appSchedule[
4
5<!ELEMENT appSchedule (task*)>
6
7<!ELEMENT task EMPTY>
8
9<!ATTLIST task name CDATA #REQUIRED>
10
11<!ATTLIST task exePath CDATA #REQUIRED>
12
13<!ATTLIST task time CDATA #REQUIRED>
14
15<!ATTLIST task repeat (D|W|M) #REQUIRED>
16
17]>
18
19<appSchedule>
20
21 <task name="Notepad" exePath="%SystemRoot%\system32\notepad.exe" time="05/05/2006 10:45" repeat="D"/>
22
23 <task name="Wordpad" exePath="C:\Program Files\Outlook Express\msimn.exe" time="05/05/2006 10:46" repeat="W"/>
24
25 <task name="Calculator" exePath="%SystemRoot%\System32\calc.exe" time="05/05/2006 10:47" repeat="M"/>
26
27</appSchedule>
28
29
Starting the service
1protected override void OnStart(string[] args)
2
3
45
The path of the XML file is set in the [b]App.config file (the IDE will not create this file automatically, so you will have to manually add one into your project) in the following way:
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="configpath" value="C:\AppScheduler.xml"/>
</appSettings>
</configuration>
[/b]
An XmlValidatingReader is used to ensure that the data strictly confirms to the DTD. The catch block stops the service, if some error occurs while trying to load data from the XML file. The timer interval is set to 30 seconds on starting the service.
To install/unistall the service
Build the application. Copy the AppScheduler.xml file to your C:\. Select Start > Programs > Microsoft Visual Studio .NET 2003 > Visual Studio .NET Tools > Visual Studio .NET 2003 Command Prompt. Go to the \bin\release folder in the project directory. Type the following command:
installutil AppScheduler.exe
Now, go to control panel. Select Performance and Maintenance > Administrative Tools and select Services. Doble-click on the AppScheduler service. Select the Log on tab. Check the Allow this service to interact with desktop checkbox. Click OK. Then click on the Start Service(}) button in the toolbar.
To uninstall the service, in the Visual Studio .NET command prompt, go to the \bin\release folder in the project directory and enter:
installutil /u AppScheduler.exe
Summary
Creating Windows services is fun, once you learn the basic things to do. XML is really a great tool that makes lot simple to define data and behavior using plain text files.
The program uses System.Timers, System.Threading and System.Diagnostics to repeatedly loop through the XML data to see whether an application is scheduled to run at the present time or not, and if yes, to launch it as a new process in a new thread.
The source
1using System;
2
3using System.Collections;
4
5using System.ComponentModel;
6
7using System.Data;
8
9using System.Diagnostics;
10
11using System.ServiceProcess;
12
13using System.Xml;
14
15using System.Timers;
16
17using System.Threading;
18
19using System.Configuration;
20
21using System.IO;
22
23
24
25namespace AppScheduler
26
27
333
I have created a class named [b]AppLauncher that accepts the executable name of a program as its constructor parameter. There is a method RunApp() in the class that creates a new ProcessInfo object with the specified path and calls Process.Start(ProcessInfo) with the ProcessInfo object as its parameter.
Class that launches applications on demand
1class AppLauncher
2
3
I had to create a separate class to launch an application in a new thread, because the Thread class in .Net 2003 does not allow you to pass parameters to a thread delegate (whereas you can do so in .Net 2005). The ProcessStartInfo class can be used to create a new process. The static method [b]Start (ProcessInfo) of the Process class returns a Process object that represents the process started.
There is a Timer variable used in the program, named _timer. The event handler for the timer's tick event is given below:
Event handler for the timer's tick event
1void timeElapsed(object sender, ElapsedEventArgs args)
2
3
71
[/b]
[/b]
An easy way to compare date and time disregarding some particular values such as hour of the day or minute or second: convert them to the appropriate string format first, and check whether the two strings are equal. Otherwise, you have to individually check each item you want to compare, like if(currTime.Day==runtime.Day && currTime.Month==runtime.Month && ...). The interval values are : "D" (for daily schedule), "W" (for weekly schedule), and "M" (for monthly schedule). The values are read from an XML file named AppScheduler.xml. The file format is given below:
The XML file containing list of applications to launch
1<?xml version="1.0" encoding="utf-8" ?>
2
3<!DOCTYPE appSchedule[
4
5<!ELEMENT appSchedule (task*)>
6
7<!ELEMENT task EMPTY>
8
9<!ATTLIST task name CDATA #REQUIRED>
10
11<!ATTLIST task exePath CDATA #REQUIRED>
12
13<!ATTLIST task time CDATA #REQUIRED>
14
15<!ATTLIST task repeat (D|W|M) #REQUIRED>
16
17]>
18
19<appSchedule>
20
21 <task name="Notepad" exePath="%SystemRoot%\system32\notepad.exe" time="05/05/2006 10:45" repeat="D"/>
22
23 <task name="Wordpad" exePath="C:\Program Files\Outlook Express\msimn.exe" time="05/05/2006 10:46" repeat="W"/>
24
25 <task name="Calculator" exePath="%SystemRoot%\System32\calc.exe" time="05/05/2006 10:47" repeat="M"/>
26
27</appSchedule>
28
29
Starting the service
1protected override void OnStart(string[] args)
2
3
45
The path of the XML file is set in the [b]App.config file (the IDE will not create this file automatically, so you will have to manually add one into your project) in the following way:
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="configpath" value="C:\AppScheduler.xml"/>
</appSettings>
</configuration>
[/b]
An XmlValidatingReader is used to ensure that the data strictly confirms to the DTD. The catch block stops the service, if some error occurs while trying to load data from the XML file. The timer interval is set to 30 seconds on starting the service.
To install/unistall the service
Build the application. Copy the AppScheduler.xml file to your C:\. Select Start > Programs > Microsoft Visual Studio .NET 2003 > Visual Studio .NET Tools > Visual Studio .NET 2003 Command Prompt. Go to the \bin\release folder in the project directory. Type the following command:
installutil AppScheduler.exe
Now, go to control panel. Select Performance and Maintenance > Administrative Tools and select Services. Doble-click on the AppScheduler service. Select the Log on tab. Check the Allow this service to interact with desktop checkbox. Click OK. Then click on the Start Service(}) button in the toolbar.
To uninstall the service, in the Visual Studio .NET command prompt, go to the \bin\release folder in the project directory and enter:
installutil /u AppScheduler.exe
Summary
Creating Windows services is fun, once you learn the basic things to do. XML is really a great tool that makes lot simple to define data and behavior using plain text files.
相关文章推荐
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- Debugging Memory Related Issues in .Net Application Using WinDBG and SOS
- Printing out your W2 Form using C# and .NET
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Programmatically using ADO.NET and XML
- Create an Asp.Net Web Forms Application using Bootstrap and Web API
- How To Transfer XML Data to Microsoft Excel 2002 by Using Visual C# .NET
- using C# to post XML to an https service (non-SOAP) RSS
- Creating a LINQ Enabled ASP.NET Web application template using C#.[转]
- Scrolling News Web Control using ASP.Net and C#
- Sending Emails Through Outlook using C# and VB.NET
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- Using .Net, Flex, and Red5 to create a flash web application
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- [转]Get Started: Debugging Memory Related Issues in .Net Application Using WinDBG and SOS
- Global Text Chat Room Application using C#.NET Remoting Technology(转载)
- Build a Desktop GIS Application Using MapWinGIS and C#
- A Book Store Application Using AngularJS and Asp.Net Web Api
- Printing out your W2 Form using C# and .NET
- Uploading File using Ajax and receiving binary data in Asp.net (C#)[转]