Programmatically reset Windows XP IDLE time - c#

Windows resets the IDLE time every time the user touches the keyboard or the mouse. My application needs to reset the IDLE time at specific moments, but i can't figure out how to do this programmatically.
The following does NOT reset the IDLE time using VB, C# or QT4.
- Programmatic mouse movement / click.
- Programmatic keystroke.
Somehow Windows knows these actions are simulated.
How can i reset the IDLE time? Any thoughts will be greatly appreciated!

Use SetThreadExecutionState(). The ES_SYSTEM_REQUIRED option (2) resets the system idle timer. Visit pinvoke.net for the required declarations.

If you need the display to stay on for example, you would call SetThreadExecutionState(ES_DISPLAY_REQUIRED|ES_CONTINUOUS);
On 2000/XP you could use the ES_USER_PRESENT flag (Does not work on Vista+)
To disable the screen saver, you can handle WM_SYSCOMMAND's SC_SCREENSAVE (You must be the foreground window, otherwise use SystemParametersInfo)

In principle SetThreadExecutionState() should do what you need. But I have found it not to work with the system required flag in some situations on some modern Windows. Sorry, I can't remember exact details.
As a fallback you can just fake some input by calling SendInput() with a null mouse move message for example. My guess is that your input faking failed because you called SendMessage or PostMessage.

Related

Perform left mouse click from Background

I have desktop app that is in background. I later want to try this a a service too.
How can I perform mouse click when certain time and date is reached.
I don't want to move user mouse or something. Just send left mouse click from background.
Controling an application via simulated Mouse or Keyboard input goes into the Area of Desktop Automation. There are many existing solutions for it. I advise agaisnt making your own custom hack.
That asumes you do not have control over the target application of course. If that was the case, using a Timer is the obvious way.
As for doing this from a Service: That is nigh impossible. Ever since Windows Vista, Service do not get interactive Sessions by default.This is a rather important part of the UAC security. While getting around it is possible it will propably raise some red flags.

Fine tuning the Application Idle Time in Windows Mobile

I am trying to detect the Idle time in a Windows Mobile application and show a Screen lock after this inactive period. This link http://blog.opennetcf.com/ctacke/2009/05/19/DetectingApplicationIdle.aspx gives a hint but I found that this works only for a single form.
How can this same code be used across an application with multiple forms. I tried implementing this code after the InitializeComponent() for each form and used the Reset() function in the form Activate and GotFocus event so that the timer is reset after the user closes the screen lock by entering a PIN number. But this does not seem to work for multiple forms and the timer is not reset but it keeps firing every minute. Even if I am working on the application the event fires up every 1 minute (as per the sample code) and this is not correct.
Can anyone suggest how to use this functionality across the entire application.
Thanks in advance for any solution.
We need to see your code to know what's wrong, but the method proposed in that blog entry uses an IMessageFilter implementation. That hooks into the Windows message pump, which is application-wide not a single-Form system. I use the exact same technique for application-wide idle detection in a couple of solutions and it works well.
My guess is that you have a scope issue with the IMessageFilter instance you're using. If you create it as a Form-level variable (like the sample in that blog does), when close the Form that the filter is disposed of and will no longer work. If you have multiple Forms, you need to put it somewhere else in the Model so it survives as long as the app is up.
What about utilizing GetIdleTime ? Seems like this, paired with a timer that wakes peroidically to check it, could detect system idle and allow you to show the screen lock.

Detect WP7 Application is Idle and perform some task

I have a requirement where I have to detect if the user is not doing on Application and Application is idle(for certain period of time) and in the meantime, i.e while the application is idle before device screen gets locked, I have to show some images one by one.
I have gone through MSDN documentation of PhoneApplicationService.UserIdleDetectionMode and PhoneApplicationService.ApplicationIdleDetectionMode but i didn't found anything helpful.
How can i detect that application is idle ?
Thanks in advance.
Detect that the application is NOT idle: Create a class containing a timer. When the timer fires, show the images. Whenever the user does ANYTHING on ANY page (detect this by e.g. overriding the manipulationstarted events), reset the timer to 0.

How to keep track of the time duration that a foreground application is active

I have found the foreground window on my desktop using the GetForegroundWindow() function. I need to keep track of how much time each application spends in the foreground and update it in the database each time in resumes to the foreground.
Is there an API to set the counter or find when the window is not in focus?
There is WH_CBT hook type supported by SetWindowsHookEx, which fires every time user switches to another window or application. How to install Windows hooks from C#.
However, if you don't need perfect accuracy, it's much easier to just call GetForegroundWindow() once per second and check if the numerical value of the returned HWND has changed. For something like an activity tracker app which does not need to notice half-a-second intervals this technique is a better choice.
Tips:
GetForegroundWindow() returns the handle of the window that is currently in focus (which can be desktop or taskbar or floating widget so you may need to filter that), or zero handle if there is no window currently focused.
It is ultra-fast so there is no harm in calling it tens or hundreds times a second.
Window handle values are unique (at least in the current user session), different even when they belong to different applications, and they are assigned in a way that a destroyed handle is almost never immediately reassigned.
If you are concerned about your own application, you may handle Activate/Focus/LostFocus/DeActivate events. If you need to monitor all windows, you need to use Windows Hooks for this. One article here.

calling the user attention outside of app

I want to create an alarm app for myself. On certain conditions (i need to poll websites) i want my app to inform me and make it HARD TO MISS. So i can take appropriate action or ignore it if i need to do something else.
I wrote a test app and using a BalloonTip (ShowBalloonTip with notifyIcon) isnt great. One of my previous apps brings the window in front of you and does a MessageBox however that doesnt always work or work well (if i somehow miss it or accidentally forget to click ok no futher messages will occur).
What are your suggestions?
You could also make it a system tray application and change the icon out if there is something which requires attention, a la a messenger application. That may not be "hard to miss" but I am trained (for better or worse) to look down at the icon tray when I see something blinking.
Where I work, we have a TimeTracker application (built in house) with which developers are supposed to log what we worked on and when. I am notorious for not using it. So, I wrote my own (Windows Forms) version for my own use which, every hour opens up and takes over my screen:
It is a frameless dialog which consumes the entire screen.
TopMost = True.
On resize, it sets WindowsState = Normal and resizes to fill the screen.
While it is open, it polls for taskmgr.exe and procexp.exe and kills them if found.
It disables the start menu to prevent cmd.exe commands from the menu in Windows 7.
The only way to close it is to enter a log, only then is the OK button shown!
So far, it's working out well - no one has been able to break it!
My less drastic suggestion would be to have a notification which pops up momentarily above the system tray. After a second or two, fade it out. Keep showing the notification every 30 seconds or so until it is dismissed.
Always-on-top window in the corner of the screen?
You could always set your window to be a top most window, make it full screen, and activate it. It would be very, very hard to miss...
Granted, it would also be very annoying, and not something I'd do to other users...
My "real" suggestion would be to use sound along with standard notification methods if this is going to be used by other people, as that's an easy way to grab attention without necessarily killing their workflow. A modeless window that appears in a corner of the screen, especially if combined with sound and color, can be very effective to grab attention.
The industry has been adopting these ambient orb devices and variations of it when such a hard-to-miss notification is required. It is used for tracking the stock-market and for broken-daily-builds.
http://www.ambientdevices.com/cat/orb/MAN_Ambient%20Orb_3-23-03.pdf
Regards

Categories