A little help with Windows Services and threads

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi everyone,

I need to make a service that monitors a directory for changes in the files
contained within it. I have two questions:

1. I'm going to be using a FileSystemWatcher object to do the monitoring -
but do I need to somehow involve another thread to allow the service to do
other stuff as well, or is another thread created automatically when the
FileSystemMonitor object is created?

2. Because I'm creating a service, and not an application, do I need to
worry about threading at all? Is it possible that my service could try and
steal all the processors resources, or does the operating system
automatically give each service a shot of the processor such that I don't
have to worry about it?

Sorry, I'm a bit unkowledgeable in this area - I'm making an application for
my boss though and I don't want to arse it up! :-)

Thanks to nayone and everyone who can help

Kindest Regards

Simon
 
Hi Simon,

Have a look as msdn.microsoft.com and use the as search keyword

"Remoting" that is what I think you are looking for.

Cor
 
Have a look as msdn.microsoft.com and use the as search keyword
"Remoting" that is what I think you are looking for.

Cor

Hi Cor,

I've already looked around msdn and I'm nearly certain that remoting isnt
what I'm looking for.

Essentially I'm wondering if anyone knows about threading issues when making
a windows service with Visual Studio, and also, if I need to create a
multithreaded application when using the FileSystemWatcher.

I've had a look around and haven't been able to find this information
(although I'm sure its there somewhere).

I'm just wondering if anyone knows the answer to these questions off the top
of their head

Thanks

Simon
 
Hi Simon,

I hope I understand your questions well.

Some answers.

If there is no need for a seperate thread, than do not use them, it is only
extra processing than.

But if it is a service that watches the systemfilesystemobject, you should
give on one or the other way the results to another program.

I thought for that is remoting the best way, as I pointed you on.

(you can do things with the main tread you are working with just with
something as)
threading.thread.sleep(4000); It is not case sensetive written have a look
for that yourself. Your program sleeps than 4 second.

Have a look at the walkthroughs on MSDN for multithreading and services

http://msdn.microsoft.com/library/d...vborivisualbasicinactionendtoendsolutions.asp

I hope this helps?

Cor
 
Simon,

To put it simply, a Windows Service is an application that has some
predefined procedures so that the OS can control it (start/stop/pause). A
Windows Service should also not start doing anything (except initialization)
until it gets a start command, and it should run forever until it receives
pause/stop commands from the OS. If you use the Windows Service wizard in
VisualStudio, the functions are already created for you.

The service is run in it's own thread, but you still need to be friendly to
the other application running on the computer. Example, don't loop forever
waiting for a variable to change in order to wait for something to happen,
instead, using Mutexes or Thread.WaitforObject (not sure on exact function
name though). If you just loop waiting for a change, then that loop will eat
up 100% of the CPU time.

I've written a few windows services, and the hardest part about writing a
service is making it run forever (until it gets a stop/pause), without
causing interference while it's waiting for other things to happen at the
same time making it responsive, and pausable and stopable.

If you need some more help, let me know

Steve
 
Inline..

--
Manoj G [.NET MVP]
Site: http://www15.brinkster.com/manoj4dotnet
Blog: http://msmvps.com/manoj/

Simon Harvey said:
Hi everyone,

I need to make a service that monitors a directory for changes in the files
contained within it. I have two questions:

1. I'm going to be using a FileSystemWatcher object to do the monitoring -
but do I need to somehow involve another thread to allow the service to do
other stuff as well, or is another thread created automatically when the
FileSystemMonitor object is created?

[Manoj] Yes, the monitoring happens on a seperate thread from the thread
pool. This is done automatically. You dont need to worry.
2. Because I'm creating a service, and not an application, do I need to
worry about threading at all?

[Manoj] Depends on what all purposes you use the service for. If your
service just does file system monitoring, then you dont really have to
bother much about threads, unless there is some objects shared between the
main thread and the thread executing the event handler. In this case, you
may need to synchronize these resources.
Is it possible that my service could try and
steal all the processors resources, or does the operating system
automatically give each service a shot of the processor such that I don't
have to worry about it?

[Manoj] Oh yes, if you dont respect resources, you can screw the performance
of the system (spawing tens of threads with a high priority for instance).
And the OS schedules threads, not processes. A service is just another
process which has one or more threads.
For your simple file system monitor, this is not much of a problem
 
Hi!

Clue points are:

- when you implement a system service, then you must stop all the processing in the OnStart method when the system service is starting; when the OnStart method finishes, then the service enters the "started" state; so, all your work in the OnStart must finish when the system service is starting and is only for initialization, not for main system service processing;

- in the OnStart method of the system service you may either start a new thread that watches the directory for you or setup the FileSystemWatcher to fire events and handle them in other methods (which is better in your case)

In the latter case you don't need to worry about thread. FileSystemWatcher will do all the work for you.
 
Thanks everyone,

Thats a great help!

I may have problems later but this info is definately enough to get me
going, so thank you very much!

Simon
 
Back
Top