By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,501 Members | 2,805 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,501 IT Pros & Developers. It's quick & easy.

A little help with Windows Services and threads

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Cor
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
Jul 21 '05 #2

P: n/a
> 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
Jul 21 '05 #3

P: n/a
Cor
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/de...dsolutions.asp

I hope this helps?

Cor
Jul 21 '05 #4

P: n/a
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.

--
Cezary Nolewajka
mailto:c.*********************@no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply
"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message news:ud**************@TK2MSFTNGP10.phx.gbl...
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

Jul 21 '05 #5

P: n/a
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
Jul 21 '05 #6

P: n/a
Inline..

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

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
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

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

Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.