1

I made a C# windows application which opens a user input win form called mywinform.exe . I want to execute this mywinform.exe once their is no user activity for a time period t (no moving/input mouse or keyboard input). I've tried using the windows task scheduler but it's so buggy and not reliable at all especially when it comes to creating tasks with Idle triggers. So now I wonder how I can do it in C# where I want to have an application or service to detect user inactivity and launch mywinform.exe once time t reached.

I've checked and found that GetLastInputInfo can do it as shown in this post, but I don't want the user inactivity detection application to be appearing, so is there a way to make it running in the background or as a service?

Tak
  • 3,536
  • 11
  • 51
  • 93
  • Look for how to create a service in .net. Plenty of tutorials. – Tarik Jul 16 '20 at 20:42
  • @Tarik So you think the way to accomplish my goal is having to create a service? – Tak Jul 16 '20 at 20:43
  • The alternative is to have the application itself exit or launch mywinform.exe. – Tarik Jul 16 '20 at 20:46
  • You don't need an interface. Look up "ApplicationContext" and pass your instance to `Application.Run()` in program.cs. – Idle_Mind Jul 16 '20 at 21:23
  • @Idle_Mind Thanks for your comment. I've googled "ApplicationContext" but I am lost. Could you explain what you meant in your previous comment in a little more detail please? thank you. – Tak Jul 16 '20 at 21:26
  • 2
    You can create a class that inherits from ApplicationContext, then create an instance of it and pass that to Run() instead of a Form. This allows you to have an application that does not display a console window and also does not display any forms. Your code would be inside that class and would start from the constructor. You'd implement the GetLastInputInfo code in there. – Idle_Mind Jul 16 '20 at 21:32
  • 1
    To provide some *why* to go with Idle_Mind's *what to do*: It is expected that the main thread of a Windows application stays in a **message loop**. `Application.Run()` provides such a message loop. The loop has to end at the right time. If you pass a `Form` instance to `Application.Run`, the loop will end when that form (your main window) closes. By using `ApplicationContext` you can have some other event (like pressing a hotkey) exit your message loop. By having the message loop your no-main-window code will work right with OLE and other libraries that need hidden message-only windows. – Ben Voigt Jul 16 '20 at 22:17
  • @BenVoigt Thank you very much. Is it possible to provide an answer with a demo since I am kind of lost please? – Tak Jul 16 '20 at 22:34

0 Answers0