PC Review


Reply
Thread Tools Rate Thread

Application.DoEvents() Equivalent in a Windows Service.

 
 
Tino Donderwinkel
Guest
Posts: n/a
 
      21st Mar 2008
I have a Windows Forms application that I'm 'converting' into a Windows
Service.

In order for the service to stop, I have to wait for an object to change
it's state. In the windows forms application I have something like;

while (myobject.state != objectstate.finished) {
Application.DoEvents();
}

I also have a Callback function in the object, that is called when its state
changes to 'finished'.

In the equivalent Windows Service, I cannot use Application.DoEvents()
because it requires the System.Windows.Forms namespace.

I have tried Thread.Sleep(1000) or something, but doing that will stall
'everything'.

Any ideas how I can solve this in a Windows Service?

This code is in the "Stop()" method of the service; I need to wait for
either the Callback to happen or the state to change to 'finsihed' before
returning from the Stop().

Tino Donderwinkel
Exchange Server MVP

 
Reply With Quote
 
 
 
 
BlackWasp
Guest
Posts: n/a
 
      21st Mar 2008
Hi Tino,

Application.DoEvents is used to make form elements redraw during processes
that are preventing the form painting events to be raised. You should not
need to do do this in a windows service because generally there is no
forms-based interface. (You can have windows forms in an interactive
service but then you would also have a reference to the relevant assembly).

Can you explain why you want to call DoEvents in a service so we can give
advice?

--

BlackWasp
www.blackwasp.co.uk


"Tino Donderwinkel" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>I have a Windows Forms application that I'm 'converting' into a Windows
>Service.
>
> In order for the service to stop, I have to wait for an object to change
> it's state. In the windows forms application I have something like;
>
> while (myobject.state != objectstate.finished) {
> Application.DoEvents();
> }
>
> I also have a Callback function in the object, that is called when its
> state changes to 'finished'.
>
> In the equivalent Windows Service, I cannot use Application.DoEvents()
> because it requires the System.Windows.Forms namespace.
>
> I have tried Thread.Sleep(1000) or something, but doing that will stall
> 'everything'.
>
> Any ideas how I can solve this in a Windows Service?
>
> This code is in the "Stop()" method of the service; I need to wait for
> either the Callback to happen or the state to change to 'finsihed' before
> returning from the Stop().
>
> Tino Donderwinkel
> Exchange Server MVP


 
Reply With Quote
 
Tino Donderwinkel
Guest
Posts: n/a
 
      21st Mar 2008
Hi,

Thanks for the quick response.

I am writing a new Windows Service, based on one I made previously, but
since debugging Windows Services is somewhat of a pain, I moved all the code
to a Windows Forms application for the time being. Now I want to move
everything back to the Windows Service.

Since a Windows Forms application does not know about OnStart() and
OnStop(), I have create 2 buttons to 'simulate' the service's start and stop
events.

The 'Service' use a lot of unmanaged threads etc. Therefore, I created a
class I call a 'Dispatcher' that keeps track of all threads etc. In the
service's OnStop() event, I will eventually call 'Dispatcher.Stop()'. The
Dispatcher then enters a stopping state; it will finish working threads, but
not create new ones. I have to wait for all current threads to finish.

There is a callback function I use. If the Dispatcher is finished
processing, it will trigger the callback function.

So, in the services OnStop() event, after the Dispatcher.Stop() call, I have
to wait for the callback to arrive OR for the Dispatcher to enter a Stopped
state. (Both are primarily the same.)

How can I wait for the Dispatcher to enter the stopped state? I cannot
return from the OnStop() method before all threads are really finished.

This is the code in the OnStop() method in the Windows Forms application;

protected override void OnStop()
{
_Stopping = true;
HandleEvent("Service is stopping.",
EventLogEntryType.Information, 1103);
Dispatcher.Stop();
while (Dispatcher.State != DispatcherState.Stopped)
{
Application.DoEvents();
}
HandleEvent("Service stopped successfully.",
EventLogEntryType.Information, 1104);
}

This is the code for the Dispatcher Callback in the Windows Forms
application:

private void DispatcherDone_Callback()
{
if (!_Stopping)
{
HandleEvent("Dispatcher stopped unexpectedly.",
EventLogEntryType.Error, 7001);
OnStop();
}
}

Any ideas how to wait for the dispatcher to stop in a Windows Serivce? Can I
use something like Thread.Sleep(1000)? Not too nice, but it might work. It
will lock up the Windows Forms application though....

Tino



"BlackWasp" <nospam@please> wrote in message
news:(E-Mail Removed)...
> Hi Tino,
>
> Application.DoEvents is used to make form elements redraw during processes
> that are preventing the form painting events to be raised. You should not
> need to do do this in a windows service because generally there is no
> forms-based interface. (You can have windows forms in an interactive
> service but then you would also have a reference to the relevant
> assembly).
>
> Can you explain why you want to call DoEvents in a service so we can give
> advice?
>
> --
>
> BlackWasp
> www.blackwasp.co.uk
>
>
> "Tino Donderwinkel" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>>I have a Windows Forms application that I'm 'converting' into a Windows
>>Service.
>>
>> In order for the service to stop, I have to wait for an object to change
>> it's state. In the windows forms application I have something like;
>>
>> while (myobject.state != objectstate.finished) {
>> Application.DoEvents();
>> }
>>
>> I also have a Callback function in the object, that is called when its
>> state changes to 'finished'.
>>
>> In the equivalent Windows Service, I cannot use Application.DoEvents()
>> because it requires the System.Windows.Forms namespace.
>>
>> I have tried Thread.Sleep(1000) or something, but doing that will stall
>> 'everything'.
>>
>> Any ideas how I can solve this in a Windows Service?
>>
>> This code is in the "Stop()" method of the service; I need to wait for
>> either the Callback to happen or the state to change to 'finsihed' before
>> returning from the Stop().
>>
>> Tino Donderwinkel
>> Exchange Server MVP

>


 
Reply With Quote
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      21st Mar 2008

> How can I wait for the Dispatcher to enter the stopped state? I cannot


With an event. See EventWaitHandle.

> return from the OnStop() method before all threads are really
> finished.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DoEvents in Windows Service Elmo Watson Microsoft Dot NET Framework Forms 2 20th Dec 2007 04:15 PM
DoEvents equivalent in VB.Net =?Utf-8?B?RWQgQ2hpdQ==?= Microsoft VB .NET 2 18th Apr 2005 08:19 PM
Windows Service and application.DoEvents() Wolfgang Kutschera Microsoft VB .NET 3 5th Apr 2005 03:01 PM
DoEvents Equivalent? MichaelR Microsoft ASP .NET 1 24th Jan 2005 06:17 PM
Application.DoEvents in a service Peter B Rasmussen Microsoft VB .NET 3 19th Nov 2003 12:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:50 AM.