C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

How to schedule a task using windows Services


Posted Date: 26 Apr 2005    Resource Type: Articles    Category: .NET Framework
Author: rajupeta prasannaMember Level: Bronze    
Rating: 1 out of 5Points: 10



Introduction
This Article basically explains how to schedule a process which can run as windows service
The process may include like Database entry as in my example.

Paragraph heading 1

Create a Table in the Database Name MyDB (Custom DB name) with the following schema

CREATE TABLE [dbo].[MyServiceLog] (
[in_LogId] [int] IDENTITY (1, 1) NOT NULL,
[vc_Status] [nvarchar] (40)
COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[dt_Created] [datetime] NOT NULL
) ON [PRIMARY]

1. Add a New Project by choosing Windows service to the solution
2. In the Solution Explorer, right-click WindowsService1.cs and select View Designer.
3. In the Components tab of the Toolbox, drag Timer component to the designer.
4. Set the time of interval (no of milli seconds) at which your process (Ex: Database Entry) has to happen by right clicking the timer component and set the interval.
5. Override OnStart and OnStop Methods of the base class System.ServiceProcess.ServiceBase from which our WindowsService1 class has inherited .


Ex: protected override void OnStart (string[] args)
{
timer1.Enabled=true;
this.DBEntry("Service Started");

}
protected override void OnStop()
{

this.timer1.Enabled = false;
this.DBEntry("Service Stopped");

}


DBEntry Function could be a local function written which does the process like Database insertion.


private void DBEntry(string Message)
{
SqlConnection connection = null;
SqlCommand command = null;
try
{
connection = new SqlConnection("Server=rprasanna;Database=MyDB;
Integrated Security=false;
User Id=sa;Password=sa;");
command = new SqlCommand("INSERT INTO MyServiceLog (vc_Status, dt_Created)
VALUES ('" + Message + "',getdate())", connection);
connection.Open();
int numrows = command.ExecuteNonQuery();
}
catch( Exception ex )
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
command.Dispose();
connection.Dispose();
}
}



6. Add the following line in the InitializeComponent function of the service

this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);

7. Your logic can be written in the function timer1_Elapsed


private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// you can also check the database and then depending // on the DB you can manipulate the other tables in // the database.
this.DBEntry("Service Running");
}

8. Right click on the windows service project choose properties


Choose the Startup Project.
9. Select the ProjectInstaller.cs in the Solution Explorer
You will find two components
a) serviceInstaller1
b) serviceProcessInstaller1
Right click and choose properties for serviceInstaller1
Choose Start Type as Automatic
Right click and choose properties for serviceProcessInstaller1
Choose Account as Local System

10. To make Setup Choose Set up Project in Setup and Deployment Project

Right click the Setup1 and choose Add ? Project Output-?Primary Output of the WindowsService1
Now add the custom action to install WindowsService1.exe
Steps to add Custom Action
a. Solution Explorer right click the setup project point to view , choose the Custom Actions
b. In the Custom Action Editor , right click the custom action node and choose Add custom Action

c. Double click the Application Folder and choose the Primary Output from WindowsService1(Active)
d. The primary output is added to all four nodes of the custom actions — Install, Commit, Rollback, and Uninstall.

11. Browse to the directory where the setup project was saved, and run the .msi file to install MyNewService.exe.


12. Finally to Start?Run ?Services.msc


Double click the WindowsService

Start the Service.

This will automatically does the process like Database entry like in the above example for every 5 mins.

for further assistance contact me @ rprasanna@firstam.com



Responses

Author: Munish Sehgal    30 May 2005Member Level: Bronze   Points : 0
OnStart , OnStop not found


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: .Net Remoting Grounds up !!!
Previous Resource: Creating the functionality of a data grid
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use