By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,161 Members | 1,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,161 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi,

I've have encountered a very strange behaviour under C#.
My application is registering for:
Microsoft.Win32.SystemEvents.DisplaySettingsChange d &
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.

Nov 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Please, Anyone know another way to get Display Settings Changed event
while running as a service?

Eyal.

Nov 17 '05 #2

P: n/a

"esafran" <ey**@mokedor.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi,

I've have encountered a very strange behaviour under C#.
My application is registering for:
Microsoft.Win32.SystemEvents.DisplaySettingsChange d &
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.

Nov 17 '05 #3

P: n/a
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.

Nov 17 '05 #4

P: n/a

"Eyal Safran" <ey**@mokedor.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.
Nov 17 '05 #5

P: n/a


Willy Denoyette [MVP] wrote:
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.

Nov 17 '05 #6

P: n/a

"Eyal Safran" <ey**@mokedor.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...


Willy Denoyette [MVP] wrote:
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.


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.
Nov 17 '05 #7

P: n/a


Willy Denoyette [MVP] wrote:
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.

Nov 17 '05 #8

P: n/a

"Eyal Safran" <ey**@mokedor.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...


Willy Denoyette [MVP] wrote:
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.

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.


Nov 17 '05 #9

P: n/a


Willy Denoyette [MVP] wrote:
"Eyal Safran" <ey**@mokedor.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...


Willy Denoyette [MVP] wrote:
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.

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.

Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.