Does not receive events from Microsoft.Win32.SystemEvents when running as a Service.

  • Thread starter Thread starter esafran
  • Start date Start date
E

esafran

Hi,

I've have encountered a very strange behaviour under C#.
My application is registering for:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged &
Microsoft.Win32.SystemEvents.UserPreferenceChanged

When I run my application as a Console Application (not as a service),
my application receives the events when I change the screen resolution
or change from Dual View to Horizontal Span or to Single View.

However, when I launch my application as a service, it does not receive
these events...

I clicked on the "Interact with desktop" in the service properties.

Anyone has any idea why it acts the way it does?
If it cannot be done that way? any idea how it can be done?

Best Regards,
Eyal Safran.
 
Please, Anyone know another way to get Display Settings Changed event
while running as a service?

Eyal.
 
esafran said:
Hi,

I've have encountered a very strange behaviour under C#.
My application is registering for:
Microsoft.Win32.SystemEvents.DisplaySettingsChanged &
Microsoft.Win32.SystemEvents.UserPreferenceChanged

When I run my application as a Console Application (not as a service),
my application receives the events when I change the screen resolution
or change from Dual View to Horizontal Span or to Single View.

However, when I launch my application as a service, it does not receive
these events...

I clicked on the "Interact with desktop" in the service properties.

Anyone has any idea why it acts the way it does?
If it cannot be done that way? any idea how it can be done?

Best Regards,
Eyal Safran.

The service runs in the security context of the SYSTEM pseudo account not
the "interactive logon user" so these events do not apply (desktop and user
preferences are user owned). When your service runs in the context of the
"interactive logon user", then it runs in a non interactive
desktop/winstation and again the changes to the interactive (active) desktop
do not apply (and aren't posted either). There is very little you can do
about this, and is yet another reason why Windows Services shouldn't have a
UI or interact with the UI for any reason other than debugging.

Willy.
 
Wow, these are very bad news you are telling me...

That means that I have to sample the
System.Windows.Forms.Screen.AllScreens for changes every now and
then...

Do you think perhaps I will be able to get events from DirectX?

Thanks,
Eyal.
 
Eyal Safran said:
Wow, these are very bad news you are telling me...

That means that I have to sample the
System.Windows.Forms.Screen.AllScreens for changes every now and
then...

Do you think perhaps I will be able to get events from DirectX?

Thanks,
Eyal.

DirectX and Windows.Forms needs a UI, again, Services should not have a UI
and should not assume to run in an interactive logon user context.
Why do you need to catch these events in a windows service in the first
place?

Willy.
 
Willy said:
Why do you need to catch these events in a windows service in the first
place?

My service as I mentioned in my first post, has the interact with
desktop checkbox, checked.
It does manipulation of graphics, and for that I need to know if the
resolution was changed or the monitors mode was switched to DualView of
SingleView or Horizontal Span.

It runs in the background.

Eyal.
 
Eyal Safran said:
My service as I mentioned in my first post, has the interact with
desktop checkbox, checked.
It does manipulation of graphics, and for that I need to know if the
resolution was changed or the monitors mode was switched to DualView of
SingleView or Horizontal Span.

It runs in the background.

Eyal.

Well, that doesn't explain why it's implemented as a service, IMO it should
not.
Not sure what you mean with - It runs in the background, if it has a UI it
runs in the foreground and it makes no sense to run if there is no active
desktop (no interactive user logon).

Willy.
 
Willy said:
Not sure what you mean with - It runs in the background, if it has a UI it
runs in the foreground and it makes no sense to run if there is no active
desktop (no interactive user logon).

It doesn't have a UI....
It interacts with the desktop image and thats why I need to know the
resolution...

Do you know any way I can query for resolution change?

Eyal.
 
Eyal Safran said:
It doesn't have a UI....
It interacts with the desktop image and thats why I need to know the
resolution...

Do you know any way I can query for resolution change?

Eyal.


Having a UI means: human input (mouse, Keyboard etc...) and/or graphical
output to a display device. You said "it manipulates graphics" isn't that
(part of) a UI?
As I said before, Services are designed to run in the background without the
need for an active desktop or an interactive Logon session. What you need is
BOTH (Interactive Logon AND an Active Desktop) for the events to be
successfully delivered to the applications message queue, however, Windows
Services can't have both.
So please, answer the question - why does it have to be a Windows Service?

Willy.
 
Willy said:
Having a UI means: human input (mouse, Keyboard etc...) and/or graphical
output to a display device. You said "it manipulates graphics" isn't that
(part of) a UI?
As I said before, Services are designed to run in the background without the
need for an active desktop or an interactive Logon session. What you need is
BOTH (Interactive Logon AND an Active Desktop) for the events to be
successfully delivered to the applications message queue, however, Windows
Services can't have both.
So please, answer the question - why does it have to be a Windows Service?

Willy.

Just so you would know... I solved the problem.

It appears that System.Windows.Forms.Screen hold an array of screens.
It is first initialized when using the propety Screen.AllScreens.
Next time the property is addressed, it returns the same array.
The only time it receives a null value is when the
DisplaySettingsChanged event occurs.

and guess what, it doesn't occur under a service...
So I created my own System.Windows.Form.Screen class (copy paste from
Lutz reflector).

which enumerates the screens every time I address the
MyScreen.AllScreens property.

So every few minutes I get the updated Screen array...
Works for me...

Cheers,
Eyal.
 
Back
Top