0

I am not able to detect this Logon event in Windows.

Here is my code:

namespace ConsoleApplication2
{

    public class MyService: ServiceBase
    {
        public MyService()
        {
            CanPauseAndContinue = true;
            CanHandleSessionChangeEvent = true;
        }

        protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            base.OnSessionChange(changeDescription);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyService tpl = new MyService();
            Thread t = new Thread(delegate()
            {
                while (true) { }
            });
            t.Start();
        }
    }
} 

How do I test run this app and then remote desktop into my laptop? I can see the event generated in Windows EventViewer, but my OnSessionChange is never called (I added a breakpoint inside).

Is my code wrong or is the way I am testing wrong?

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
Adrian
  • 19,440
  • 34
  • 112
  • 219
  • One suggestion is not to code it in your laptop but to add it as a response to login as part of the scheduled tasks (if you have windows 10 this is a sinch)..you add this to run on login, and it does exactly that.. you dont have to detect it – BugFinder Feb 13 '17 at 13:23
  • I am testing this on Windows 7, but it needs to run on XP aswell, so I kinda need to detect it using code. – Adrian Feb 13 '17 at 13:24
  • boo.. hmm.. good plan. shot at the start.. you could monitor the event log if thats not working for you.. is your app running as a service?? – BugFinder Feb 13 '17 at 13:25
  • No, its not running as a service. And I don't think I am able to run it as a service. – Adrian Feb 13 '17 at 13:28
  • Oh .. that could be why its going a bit wrong then.. cos the servicebase is for services.. – BugFinder Feb 13 '17 at 13:31

1 Answers1

1

Normally multiple concurrent remote desktop sessions are not allowed on any of Windows desktop systems. So to use RDP to login as a different user then I assume that you have hacked this, or are using windows server (which rules out XP!).

Regardless, each user logged into the system will therefore have their own applications running and each set of applications are unique to that user. So App1 could be run independently by each user.

That means that your console application cannot detect the other user that is logged on.

To do this you must use a Windows Service. This runs in the background and can detect and work for multiple users and also detect login and logout. See this SO link

This is the purpose of inheriting MyService from ServiceBase. If you are not running the application as a service, then you are not running it correctly!

You need to first install your application as a service and then run it like a service.

You say that you don't think your application can run as a service. I'm not sure why, but if this is the case then you would have to instead look at creating some kind of script to run your application upon start-up/login.

This way, every time somebody logs in then your application would run. This might anyway be simpler for you.

Community
  • 1
  • 1
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51