473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A little help with Windows Services and threads

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
Nov 15 '05 #1
7 3907
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
Nov 15 '05 #2
> 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
Nov 15 '05 #3
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
Nov 15 '05 #4
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

"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

Nov 15 '05 #5
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

Nov 15 '05 #6
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
Nov 15 '05 #7
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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Fabio Papa | last post by:
I am trying to write a windows service that sends emails to clients at specific times based on information in a sql db. Since this is done for multiple cities, I start a thread for each city and...
6
by: Simon Harvey | last post by:
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...
2
by: karl | last post by:
I have a windows service that kicks off a 'monitor' thread which in turn kicks off 4 additional threads. These 4 threads basically are listen on a designated socket and report back any errors...
1
by: Jack David | last post by:
I need direction on how to create a program that will start multiple windows services. Each service will monitor a specific directory using FileSystemWatcher. I have the file system watcher part...
1
by: Jeevan | last post by:
Hi, I am creating a Window Service in C-Sharp. The Window Service has a reference to an OCX file created in VC++. In OnStart method I have created an instance of the class, of the OCX file and...
5
by: Ekempd | last post by:
Hi I need some advice about this situation an how I'm current handling it I hava a ASP.NET solution that in some point start multiples lenghty verification agains diferent databases, my big...
10
by: John | last post by:
I currently have a Windows Service that runs Transactions that are very Processor/Memory Intensive. I have a requirement to deploy multiple instances of the Web service on the Same server. Each...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
6
by: Simon Harvey | last post by:
Hi everyone, We have a need to make a Windows Forms (2.0) client application that will be installed on our clients site. The data that the application uses needs to be centrally available to a...
12
by: Dilip | last post by:
Hi All I have a server based C# console application. This application must hide its console window when its launched out on the field. So I dutifully P/Invoke'd FindWindow/ShowWindow...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.