-----------------------------------------------------------

New Message on MumbaiUserGroup

-----------------------------------------------------------
From: Nasha
Message 1 in Discussion

 
Hi Group, 
This is continuation to Windows Service. I was requested to discuss how to send 
startup Parameters and start and stop a windows service programmatically. 
Let us first see how we can send startup parameters to our MoveFile windows 
service and even control the service programmatically. When we created this 
windows service we had hard coded the windows service parameters We will now 
pass the target folder and the destination folder as startup parameters. 
For this we will have to make some changes in the existing code. 
In MoveFile.cs we will have to add two variables for storing the target and 
destination folder as follows: - 
public class MoveFile 
{ 
Thread currThread; 
string targetFolderPath = null; 
string destinationFolderPath = null; 
 
public MoveFile(string targetFolder,string destinationFolder) 
{ 
targetFolderPath= targetFolder; 
destinationFolderPath = destinationFolder; 
currThread = new Thread(new ThreadStart(StartMoving)); 
currThread.Start(); 
} 
We now need to modify the constructor of the MoveFIle Class to take values of 
these two parameters as shown above. 
If you take a look at the OnStart function of MoveFilesService you will notice 
that the function takes an array of strings as an input parameter. We will 
modify the implementation of this function to check whether parameters are 
passed or not. If parameters are not passed then we set a default value to 
them. 
protected override void OnStart(string[] args) 
{  
if(args.GetLength(0) != 2) 
{ 
args = new string[2];  
args[0] = @"E:\FilesToMove"; 
args[1] = @"E:\MovedFiles"; 
} 
oMoveFile = new MoveFile(args[0],args[1]);  
} 
  
  
To control the service we will need a service controller. . NET Framework 
provides us with a service controller class. So let us prepare our service 
controller. 
Open a new windows application. Add a form to it and add four buttons and one 
label to the form. 
We will use the label to display the current service status and the four 
buttons to start, stop, pause and resume our win service. 
Add reference to System.ServiceProcess at the top as follows: 
using System.ServiceProcess; 
Create an object of the service controller at the class level 
private System.ServiceProcess.ServiceController sc; 
Go to the design of your Form and add check out the properties of your service 
controller. 
One of the properties is ServiceName. Select the Service Name as your 
MoveFilesService. 
ServiceController provides you with methods to control your service. So will 
call these methods in the each of these button click events. 
Rename the buttons as Start,Stop,Pause and Resume. 
Currently I m passing the parameters hard coded from my service controller 
program you can even pick them from app config file. 
This is code that we need to put into the click event of each of these buttons: 
- 
private void Start_Click(object sender, System.EventArgs e) 
{ 
if(sc.Status == ServiceControllerStatus.Stopped) 
{ 
sc.Start(new string[]{"E:\\MovedFiles","E:\\FilesToMove"});  
label2.Text = "Service Started"; 
} 
} 
private void Stop_Click(object sender, System.EventArgs e) 
{ 
sc.Stop(); 
label2.Text = "Service Stopped"; 
} 
private void Pause_Click(object sender, System.EventArgs e) 
{ 
if(sc.Status == ServiceControllerStatus.Running) 
{ 
sc.Pause(); 
label2.Text = "Service Paused"; 
} 
} 
private void Resume_Click(object sender, System.EventArgs e) 
{ 
if(sc.Status == ServiceControllerStatus.Paused) 
{ 
sc.Continue(); 
label2.Text = "Service Resumed"; 
} 
} 
  
Run the application and check out the functionality. 
If you explore other functions of Service Controller (static functions) you 
will find that it provides you with functions to enlist services, device 
services etc. 
e.g. ServiceController.GetServices  
ServiceController.GetDevices 
I hope I have helped you in clearing your doubts. If you face any issues get 
back to me. 
  
-- Please post your queries and comments for my articles in the usergroup for 
the benefit of all. I hope this step from my end is helpful to all of us.  
Regards, 
Namratha (Nasha) 
  
 

-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/MumbaiUserGroup/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member 
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you 
received this message by mistake, please click the "Remove" link below. On the 
pre-addressed e-mail message that opens, simply click "Send". Your e-mail 
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to