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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • esafran

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

    Hi,

    I've have encountered a very strange behaviour under C#.
    My application is registering for:
    Microsoft.Win32 .SystemEvents.D isplaySettingsC hanged &
    Microsoft.Win32 .SystemEvents.U serPreferenceCh anged

    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.

  • Eyal Safran

    #2
    Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.

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

    Eyal.

    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.


      "esafran" <eyal@mokedor.c om> wrote in message
      news:1117784088 .916645.224580@ o13g2000cwo.goo glegroups.com.. .[color=blue]
      > Hi,
      >
      > I've have encountered a very strange behaviour under C#.
      > My application is registering for:
      > Microsoft.Win32 .SystemEvents.D isplaySettingsC hanged &
      > Microsoft.Win32 .SystemEvents.U serPreferenceCh anged
      >
      > 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.
      >[/color]

      The service runs in the security context of the SYSTEM pseudo account not
      the "interactiv e logon user" so these events do not apply (desktop and user
      preferences are user owned). When your service runs in the context of the
      "interactiv e 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.



      Comment

      • Eyal Safran

        #4
        Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.

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

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

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

        Thanks,
        Eyal.

        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.


          "Eyal Safran" <eyal@mokedor.c om> wrote in message
          news:1117918892 .222369.216350@ f14g2000cwb.goo glegroups.com.. .[color=blue]
          > Wow, these are very bad news you are telling me...
          >
          > That means that I have to sample the
          > System.Windows. Forms.Screen.Al lScreens for changes every now and
          > then...
          >
          > Do you think perhaps I will be able to get events from DirectX?
          >
          > Thanks,
          > Eyal.
          >[/color]

          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.


          Comment

          • Eyal Safran

            #6
            Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.



            Willy Denoyette [MVP] wrote:
            [color=blue]
            > Why do you need to catch these events in a windows service in the first
            > place?[/color]

            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.

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.


              "Eyal Safran" <eyal@mokedor.c om> wrote in message
              news:1117921268 .152052.323850@ g14g2000cwa.goo glegroups.com.. .[color=blue]
              >
              >
              > Willy Denoyette [MVP] wrote:
              >[color=green]
              >> Why do you need to catch these events in a windows service in the first
              >> place?[/color]
              >
              > 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.
              >[/color]

              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.


              Comment

              • Eyal Safran

                #8
                Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.



                Willy Denoyette [MVP] wrote:
                [color=blue]
                > 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).[/color]

                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.

                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.


                  "Eyal Safran" <eyal@mokedor.c om> wrote in message
                  news:1117929527 .225459.320090@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                  >
                  >
                  > Willy Denoyette [MVP] wrote:
                  >[color=green]
                  >> 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).[/color]
                  >
                  > 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.
                  >[/color]


                  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.




                  Comment

                  • Eyal Safran

                    #10
                    Re: Does not receive events from Microsoft.Win32 .SystemEvents when running as a Service.



                    Willy Denoyette [MVP] wrote:[color=blue]
                    > "Eyal Safran" <eyal@mokedor.c om> wrote in message
                    > news:1117929527 .225459.320090@ o13g2000cwo.goo glegroups.com.. .[color=green]
                    > >
                    > >
                    > > Willy Denoyette [MVP] wrote:
                    > >[color=darkred]
                    > >> 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).[/color]
                    > >
                    > > 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.
                    > >[/color]
                    >
                    >
                    > 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.[/color]

                    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.AllScree ns.
                    Next time the property is addressed, it returns the same array.
                    The only time it receives a null value is when the
                    DisplaySettings Changed 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.AllScr eens property.

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

                    Cheers,
                    Eyal.

                    Comment

                    Working...