473,734 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1766
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$******** *****@TK2MSFTNG P04.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(DoS tuff));

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 BackgroundWorke r?) Is there any reason to
prefer a Thread over a BackgroundWorke r 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******** *****@TK2MSFTNG P04.phx.gbl...
"Joseph Geretz" <jg*****@nospam .comwrote in message
news:e$******** *****@TK2MSFTNG P04.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
synchronousl y 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
synchronousl y 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(DoS tuff));

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 BackgroundWorke r?) Is there any reason to
prefer a Thread over a BackgroundWorke r 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******** *****@TK2MSFTNG P04.phx.gbl...
"Joseph Geretz" <jger...@nospam .comwrote in message
news:e$******** *****@TK2MSFTNG P04.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(DoS tuff));
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************ ******@googlema il.comwrote in message
news:11******** *************@q 40g2000cwq.goog legroups.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 BackgroundWorke r?) Is there any reason
to
prefer a Thread over a BackgroundWorke r 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******* ******@TK2MSFTN GP04.phx.gbl...
"Joseph Geretz" <jger...@nospam .comwrote in message
news:e$******* ******@TK2MSFTN GP04.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
synchronousl y 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
synchronousl y 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(DoS tuff));
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$******** *****@TK2MSFTNG P04.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
1779
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 account and the Interact with the Desktop is Checked. Windows media player seems to start but then closes almost immediately. I would appreciate any help. Thanks, Glenn
2
1191
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 application which is tightly integrated with the workstation Windows environment. Our software does not necessarily handle any specific document type. User's can import any document type into our repository. On the display side, we use WinAPI to...
24
2282
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 environment and so we are in the process of imposing a WebService gateway between our application client and the repository. (As a starting point, the WebService won't be a richly featured application server; business rules are still implemented in the...
0
2811
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 progress fine until it hits about halfway. It then stops there for about 4 or 5 seconds then goes a little farther. I then get a message: Could not start the MyService Service on Local Computer. Error 1053: The service did not respond to the...
0
1250
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 (e.g. through serial port and other sources) 2) Local machine user interface 3) Remote machine user interface (networked)
1
9648
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
6
4202
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 time, but occasionally after a system startup (where the service doing the starting of the other is automatically started by Windows XP) the ServiceController's Start() method fails to start the other service. It is probably throwing an...
5
4722
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 more, but I couldn't get it working, so I boiled it down to the exception to test. Anyway, the service can be installed using InstalUtil but when starting the service, I just get the message that the service stopped again withour reporting any...
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.