473,394 Members | 1,951 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,394 software developers and data experts.

Seeking Advice for Starting a Windows Service

I have a Service which runs OK, but I'm abviously not starting it properly.
In my OnStart event I commence a long running process which polls a database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct feedback
that the service has started properly. Consequently, the SCM throws an error
dialog and the service is marked as 'Starting', even though the service is
in fact running properly. I'd like to correct this.

I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to do
this.

Also, any advice on how the service can execute at a low background priority
will be very much appreciated.

Thanks for your advice!

- Joseph Geretz -

Mar 7 '07 #1
7 1723
On Mar 6, 11:14 pm, "Joseph Geretz" <jger...@nospam.comwrote:
I have a Service which runs OK, but I'm abviously not starting it properly.
In my OnStart event I commence a long running process which polls a database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct feedback
that the service has started properly. Consequently, the SCM throws an error
dialog and the service is marked as 'Starting', even though the service is
in fact running properly. I'd like to correct this.

I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to do
this.

Also, any advice on how the service can execute at a low background priority
will be very much appreciated.

Thanks for your advice!

- Joseph Geretz -

Just create a thread in the onstart routine, and perform your work in
the thread method... You can also set that threads priority.
Something like:

// do resource aquisition
// create your thread, and set the priority, and start it up
Thread worker = new Thread (new ThreadStart (WorkerMethod));
worker.Priority = ThreadPriority.BelowNormal; // Or Lowest if you like
worker.Start();

Then you just do your actual work in the WorkerMethod. You can signal
the thread to end from the OnStop method, etc.

HTH

--
Tom Shelton

Mar 7 '07 #2
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:e$*************@TK2MSFTNGP04.phx.gbl...
>I have a Service which runs OK, but I'm abviously not starting it properly.
In my OnStart event I commence a long running process which polls a
database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct
feedback
that the service has started properly. Consequently, the SCM throws an
error
dialog and the service is marked as 'Starting', even though the service is
in fact running properly. I'd like to correct this.

I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to do
this.

Also, any advice on how the service can execute at a low background
priority will be very much appreciated.

Thanks for your advice!
Just start a thread in OnStart and set it's priority to be low

Thread thread = new Thread(new ThreadStart(DoStuff));

thread.Priority = ThreadPriority.Lowest;

thread.Start();

>
- Joseph Geretz -

Mar 7 '07 #3
OK, thanks guys - very helpful.

But how do I cancel a thread? I guess I can't just kill the operation. Is
cancelling a thread simply setting a flag on the thread and my code needs to
monitor that flag to see when it is set in order to terminate? (This seems
to be the way it would work for a BackgroundWorker?) Is there any reason to
prefer a Thread over a BackgroundWorker object or vice versa? I guess the
two approaches would be similar? (Please forgive the basic questions. As an
ex-VB6 devleoper, I haven't had much exposure to threads up to this point.)

Thanks for your advice.

- Joe Geretz -

"Michael C" <no****@nospam.comwrote in message
news:ea*************@TK2MSFTNGP04.phx.gbl...
"Joseph Geretz" <jg*****@nospam.comwrote in message
news:e$*************@TK2MSFTNGP04.phx.gbl...
>>I have a Service which runs OK, but I'm abviously not starting it
properly.
In my OnStart event I commence a long running process which polls a
database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct
feedback
that the service has started properly. Consequently, the SCM throws an
error
dialog and the service is marked as 'Starting', even though the service
is
in fact running properly. I'd like to correct this.

I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are
obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to
do
this.

Also, any advice on how the service can execute at a low background
priority will be very much appreciated.

Thanks for your advice!

Just start a thread in OnStart and set it's priority to be low

Thread thread = new Thread(new ThreadStart(DoStuff));

thread.Priority = ThreadPriority.Lowest;

thread.Start();

>>
- Joseph Geretz -


Mar 7 '07 #4
On Mar 7, 8:01 am, "Joseph Geretz" <jger...@nospam.comwrote:
OK, thanks guys - very helpful.

But how do I cancel a thread? I guess I can't just kill the operation. Is
cancelling a thread simply setting a flag on the thread and my code needs to
monitor that flag to see when it is set in order to terminate? (This seems
to be the way it would work for a BackgroundWorker?) Is there any reason to
prefer a Thread over a BackgroundWorker object or vice versa? I guess the
two approaches would be similar? (Please forgive the basic questions. As an
ex-VB6 devleoper, I haven't had much exposure to threads up to this point.)

Thanks for your advice.

- Joe Geretz -

"Michael C" <nos...@nospam.comwrote in message

news:ea*************@TK2MSFTNGP04.phx.gbl...
"Joseph Geretz" <jger...@nospam.comwrote in message
news:e$*************@TK2MSFTNGP04.phx.gbl...
>I have a Service which runs OK, but I'm abviously not starting it
properly.
In my OnStart event I commence a long running process which polls a
database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct
feedback
that the service has started properly. Consequently, the SCM throws an
error
dialog and the service is marked as 'Starting', even though the service
is
in fact running properly. I'd like to correct this.
I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are
obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to
do
this.
Also, any advice on how the service can execute at a low background
priority will be very much appreciated.
Thanks for your advice!
Just start a thread in OnStart and set it's priority to be low
Thread thread = new Thread(new ThreadStart(DoStuff));
thread.Priority = ThreadPriority.Lowest;
thread.Start();
- Joseph Geretz -
you can use a flag
bool run = true

while(run){
// Do some work
// Check condition
run = false;
}
Mar 7 '07 #5
Hi Oscar,
you can use a flag
bool run = true

while(run){
// Do some work
// Check condition
run = false;
}
So what you're saying is that when the method which is running on the thread
terminates, the thread itself is terminated automatically?

Thanks for your help,

- Joseph Geretz -

<os******************@googlemail.comwrote in message
news:11*********************@q40g2000cwq.googlegro ups.com...
On Mar 7, 8:01 am, "Joseph Geretz" <jger...@nospam.comwrote:
>OK, thanks guys - very helpful.

But how do I cancel a thread? I guess I can't just kill the operation. Is
cancelling a thread simply setting a flag on the thread and my code needs
to
monitor that flag to see when it is set in order to terminate? (This
seems
to be the way it would work for a BackgroundWorker?) Is there any reason
to
prefer a Thread over a BackgroundWorker object or vice versa? I guess the
two approaches would be similar? (Please forgive the basic questions. As
an
ex-VB6 devleoper, I haven't had much exposure to threads up to this
point.)

Thanks for your advice.

- Joe Geretz -

"Michael C" <nos...@nospam.comwrote in message

news:ea*************@TK2MSFTNGP04.phx.gbl...
"Joseph Geretz" <jger...@nospam.comwrote in message
news:e$*************@TK2MSFTNGP04.phx.gbl...
I have a Service which runs OK, but I'm abviously not starting it
properly.
In my OnStart event I commence a long running process which polls a
database
table and performs various processing. Since this polling loop is
entered
synchronously from OnStart, basically the OnStart event doesn't
terminate
for the life of the program. This doesn't give the SCM the correct
feedback
that the service has started properly. Consequently, the SCM throws an
error
dialog and the service is marked as 'Starting', even though the
service
is
in fact running properly. I'd like to correct this.
>I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are
obtained,
and then launch into the main service routine asynchronously so that
the
OnStart event can terminate in a timely manner. But I'm not sure how
to
do
this.
>Also, any advice on how the service can execute at a low background
priority will be very much appreciated.
>Thanks for your advice!
Just start a thread in OnStart and set it's priority to be low
Thread thread = new Thread(new ThreadStart(DoStuff));
thread.Priority = ThreadPriority.Lowest;
thread.Start();
>- Joseph Geretz -

you can use a flag
bool run = true

while(run){
// Do some work
// Check condition
run = false;
}


Mar 7 '07 #6
On Mar 7, 8:26 am, "Joseph Geretz" <jger...@nospam.comwrote:
Hi Oscar,
you can use a flag
bool run = true
while(run){
// Do some work
// Check condition
run = false;
}

So what you're saying is that when the method which is running on the thread
terminates, the thread itself is terminated automatically?

Thanks for your help,

- Joseph Geretz -
Yes, when a thread method exits - so does the thread. Be a little
careful using a flag like this though to end the thread - especially
if it is going to be set from outside the thread... While on x86
processors, reads and writes of this type are guarenteed to be atomic
- you may still get unexpected results. The reason is that sometime
the compiler will optimize the code to read from a register instead of
the actual memory value. When this happens in a threaded environment,
you may set the value to false - but the thread will never see it
happen... You have two choices to fix this - one use a lock or
declare the bool as volatile (which would be my prefered method in
this case :). By declaring it volatile, you tell the compiler that
this value maybe changed from an outside source, and to always fetch
it from memory.

--
Tom Shelton

Mar 8 '07 #7

Look here:
http://blogs.msdn.com/bclteam/archiv...15/396428.aspx

I would append his article by saying you want to have a

when you start you "actual code" I would kill off the timerDelegate...
after you job run, reinstantiate the timerDelegate.


"Joseph Geretz" <jg*****@nospam.comwrote in message
news:e$*************@TK2MSFTNGP04.phx.gbl...
I have a Service which runs OK, but I'm abviously not starting it
properly.
In my OnStart event I commence a long running process which polls a
database
table and performs various processing. Since this polling loop is entered
synchronously from OnStart, basically the OnStart event doesn't terminate
for the life of the program. This doesn't give the SCM the correct
feedback
that the service has started properly. Consequently, the SCM throws an
error
dialog and the service is marked as 'Starting', even though the service is
in fact running properly. I'd like to correct this.

I guess the best practice approach would be to perform initialization
synchronously just to make sure that all necessary resources are obtained,
and then launch into the main service routine asynchronously so that the
OnStart event can terminate in a timely manner. But I'm not sure how to do
this.

Also, any advice on how the service can execute at a low background
priority
will be very much appreciated.

Thanks for your advice!

- Joseph Geretz -

Mar 10 '07 #8

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

Similar topics

1
by: Glenn | last post by:
I have used this code successfully in a form application. I tried to add the same code in a service and have not been able to get the application to start. I have the service starting with a local...
2
by: Joseph Geretz | last post by:
I don't know if this is the right group for my question, but I'm seeking advice from knowledgable .NET developers. Hopefully I've come to the right place. I work with a document management...
24
by: Joseph Geretz | last post by:
Up to this point, our application has been using Windows File Sharing to transfer files to and from our application document repository. This approach does not lend itself toward a secure...
0
by: tshad | last post by:
I have a Windows Service I created that just sets a timer and writes to EventLog every 10 seconds. It installed fine and it actually starts. But it says it doesn't. The progress bar shows the...
0
by: CodeMonkey | last post by:
Hi, I was wondering if anybody could give me some advice on how to structure an application that I am about to start work on. The vague requirements will be: 1) Always-On monitoring of data...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
6
by: Arnie | last post by:
We're using the ServiceController class provided by the .NET Framework, programming in C#. We are using the Start() method to start a service from another service. This works fine most of the...
5
by: eliasen | last post by:
Hi I have created a Windows Service using C# and .NET2.0. The service is quite simple - right now it doesn't do anything except throwing an exception in the OnStart method. It used to something...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.