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

New Message on BDOTNET

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

 
Hey Guys, 
I m sorry . I made a little mistake in the function names. I am reposting the 
article with changes. The changes are in bold red. 
Regards, 
Namratha. 
  
Hey Group,

Today we will start with windows services series.

Windows services are applications that start as soon as the OS starts although 
they can be configured to start manually.

To view all the windows services available on your machine you can go to Start 
-> Settings-> Control Panel -> Admistrative Tools -> Services. A windows 
service can run under a specific user or under the system user.

Generally all those applications that do not require any human intervention are 
preferred as windows service, say a background job.

Today the service that we will create will move the files from one location to 
another location.

Open a windows service application let the Service1.cs remain as it is now. We 
will come back to it a little later. Add a new class and name it MoveFile.cs

So let us start by creating a simple class called MoveFile. Add the following 
references to MoveFile

using System;
using System.Threading;
using System.IO;

In this class we will create we will declare a private thread. This thread will 
initiate the file movement from one folder to another.

public class MoveFile
{
Thread currThread;
public MoveFile()
{
currThread = new Thread(new ThreadStart(StartMoving));
currThread.Start();
}
}

In the constructor of the class we create a new thread by setting its thread 
start and start the thread. As soon as our thread will be started StartMoving 
will be called. So Let us check out its implementation

public void StartMoving()
{ 
while(1==1)
{ 
string[] files = Directory.GetFiles(@"E:\FilesToMove");
for(int i=0;i<files.GetUpperBound(0);i++)
{
File.Move(files[i],@"E:\MovedFiles" 
+files[i].Substring(files[i].LastIndexOf("\\")));
Console.WriteLine(files[i]); 
}
}
}

Note : For the sake of simplicity and demo purpose the paths have been 
hardcoded. You can add an application config file and read the paths from there.

Here I am having two folders FilesToMove and MovedFiles in my E: I am having a 
while loop which continuously checks for files in my

FilestoMove folder and if there are any files in it I am moving them to 
MovedFiles.

Since a windows service can be stopped, paused, resumed and abort we want our 
service to also exhibit the same behavior.
So let us add functions to MoveFile for the same.

public void PauseMoving()
{
currThread.Suspend();
} 

public void StopMoving()
{
currThread.Abort();
}

public void ResumeMoving()
{
currThread.Resume();
} 

Thus we have created our MoveFile Class that moves files from FileToMove to 
MovedFiles Folder.

This class is a generic class, you can also use it with a normal windows or 
console application. But we will create a windows service using it.

All the classes, which are required for creating a windows service, are present 
in System.ServiceProcess Namespace.

Any service that we create has to inherit from ServiceBase class. This class is 
used to register the service and handle its events.

Coming back to Service1.cs rename it to MoveFilesService. Please see to it that 
you change the name in solution explorer as well as the class constructor.

In InitailizeCompoment function there is a statement which talks about the 
service name ( as shown below) please make the changes there also.

this.ServiceName = "MoveFilesService"; 

In the Main function of your service class checkout Services added to your 
ServicesToRun Array change it to MoveFilesService

ServicesToRun = new System.ServiceProcess.ServiceBase[] { new 
MoveFilesService() };

Check out the name in properties of your MoveFilesService class. Compile your 
project , it should not give you any errors.

Go to the properties of MoveFile and set

AutoLog = true
CanPauseAndContinue = true
CanStop = true

Add a private variable of MoveFile to MoveFileService class.

public class MoveFilesService : System.ServiceProcess.ServiceBase
{
MoveFile oMoveFile;

We will now add code to OnStart method of our Service Class where we will 
instantiate an object of MoveFile.

protected override void OnStart(string[] args)
{
oMoveFile = new MoveFile();
}

ServiceBase class has got methods to Pause, Stop and Resume a service. We will 
have to override them and provide an implementation to pause, stop and resume 
our service as follows.

protected override void OnPause()
{
oMoveFile.PauseMoving(); 
}
protected override void OnStop()
{
oMoveFile.StopMoving(); 
}

protected override void OnContinue()
{
oMoveFile.ResumeMoving();
}

The methods that we had made in our MoveFile class are called here. Hence 
whenever our windows service is paused our thread will also exhibit the same 
behavior.

Compile the project and see that you don't get any errors will see how to 
install our windows service tomorrow.

-- 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/BDotNet/_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