How can I get computer status in c# - c#

I want to create something like a client in c#.
But I do not know that how I can learn computer status like sleep mode, off, logged in or logged out.
Also, I need to get the warning if the user haven't used the computer for 10 minutes.

You can find information about currently logged user and how to hook the Locked/Unlocked events in this thread.
There are some other different approaches you might try:
You can use the System.Diagnostics and get the process list via Processes.GetProcesses(). Just keep an eye on the Idle process -- if it runs for more than 50% CPU longer than 10 mins the user seems to be idling too.
You can use Performance Counters to monitor the activity taking place on the computer and make certain decisions.
You can also use the WMI service with similar purposes.

Partial answer:
User activity/inactivity can be monitored using hooks. Start a timer with a 10-minute interval. Whenever you detect a keyboard/mouse message, restart it. If the timer event happens, than you detected 10 minutes of inactivity.

Related

how to handle system wide key events in asp.net application

i am working on an asp.net web application, where tasks are assigned to users, we set standard time to every task, in that standard time period the user has to finish the task, there are two buttons on the page, proceed and save, when a user clicks on proceed button, the time is saved in database as starttime, and when the user clicks on save button, the time is saved in database as endtime. this way we are capturing the time period within which the user is completing the task.
the standard time is set on an average time study basis, not every time the task takes the same amount of time.
often users can complete the task in very less time than the standard time, in this case the users are proceeding the task and even after completing the task, instead of saving it, they lock the system and go for tea breaks and after coming from break, they save the task.
i want to save some information on the web page when they lock the pc even when the browser is minimized.
i tried implementing applet using jintellitype library but its not capturing the key combinations that are used by windows os.
i also tried using Silverlight but there is no such support as in winforms application in Silverlight, i have to create a com component or something that interacts with system32 or some native api. it doesn't seem easy for me, i would like to know if there is such library for Silverlight.
it should be browser independent, i haven't tried ActiveX, but i think it can be done using ActiveX, but i don't want to use ActiveX as it runs only on IE.
i want to know all the possible solutions to achieve this.
thanks in advance.
Why don´t you set a kind of timer-check to know if the last time is too far from the correspondent (and previewed) time to perform the job? If a task may expend, for instance, from 1 to 5 minutes, have 21 minutes is too far.
Why din´t you create a timer to TIMEOUT user? If users know they will be timed-out after some time, probably, they won´t leave to coffre-break during the test (a kind of penalty must be aggregated on this, like start from the initial point if timeout).
Why don´t you automatically save the record after the job finish, instead obly the user to press a button?
Until I know, you can perform SUSPEND mode, but not detect them if started from other apps.

C# - How to detect a Windows shutdown/logoff and cancel that action (after asking the user)

Some explanation: for a project I'm working on I have to write a program that is running in the background, detects changes to files, and uploads the modified files to a web service to make it available to others. Quite simple synchronization if it were not for the case when a user modifies a big file and decides to shutdown its computer right after the edit.
I could cancel the upload and wait for the next reboot to do the upload, but I can imagine the user downloading the file from the web to another computer the next morning and don't understanding why his changes from last night aren't there.
So my idea was to detect when the users logs off or reboots Windows, and if I'm in the middle of an upload, just asking the user "We're still synchronizing file Foo.txt that you just changed. Are you sure you want to reboot ? You're changes won't be available to others until you restart your computer !". If the users says no, I'd need to cancel the reboot/loging off
Is this possible?
There is a static class called SystemEvents that exposes this behaviour:
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx
However, it cannot differentiate between certain actions and doesn't pause the OS process time-out guard. I used it once, but the default time-out as configured in the registry is a little short so will likely need increasing.
To cut a long story short, it all felt a little hackish.
To add to #Adam's answer, if you need to tell the difference between logoff and shutdown/reboot, you can handle the WM_QUERYENDSESSION message.
"Shutdown Changes for Windows Vista" is a useful article for understanding the shutdown timeout.
Trying to block a shutdown is a lossy proposition these days, it's no longer possible to do so in Vista and up. A prompt isn't readable nor reachable. Using a service is highly indicated here, lets you survive a user log-off. And a reboot, your service will start running again automatically, letting you complete the job.

System Idle Detection

I want to detect if the system is idle, ie: user not using the system. I want it like the Windows Live Messenger it changes automatically to away when I leave the computer for a time like 3 minutes, I want to set this time within the code.
I`m working on the WPF under C# environment using both visual studio 2008 and 2010 so if here is a way that work on both that`ll be great.
There's an article on CodeProject that should get you started.
I've provided an answer for detecting inactivity and activity in WPF and it might be interesting for you:
WPF inactivity and activity
Windows does provide some API's for that, although they're not reliable for multiple sessions or something like that.
What I used is a hook to WW_MOUSE_LL with SetWindowsHookEx(); That's in C. Must be pretty similar for C#. Basicaly whenever the user does something with the mouse, the timer starts from 0 again, and if the timer reaches some value, you do something upon that(act-if system is idle). You could also hook it with the keyboard, in case the user is just typing, and share the timer between the two threads. It works wonderful for me.

Windows Mobile - detecting when power off button is pressed

Is there an easy way to get notified when user presses Power Off button on it's Windows Mobile device? Using C# of course.
Thanks!
When the power button is pressed, the power manager will send out a notification of a state change. You can request that the PM send you a notification by calling RequestPowerNotifications You have to send in a handle to a point-to-point messgae queue (managed version here) that will get the notification.
For thos who don't want to write all of the glue to make this work, all of this is already pre-done for you in the SDF's PowerManagement class.
Also be forewarned that just becasue you request the notification does not mean that your app will get the notification before the state change occurs. For example on pwer down it's pretty common that an app won't see the notification, and almost certain that even if you do see it you won't have time to execute anything before suspend actually occurs. Typically your handler will run when the device resumes (followed by any handler for the resume state).
The power manager doesn't wait for you, it simply broadcasts a message. You cannot use this to run code before a shutdown.
I just place CreateMsgQueue() into the XIP RPM binaries, without need for source code or OEM help. you need to use a hex editor and a few tools from XDA forums.
<DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Function CreateMsgQueue(ByVal lpszName As String, ByVal lpOptions As MsgQueueOptions) As IntPtr
End Function
also you should just create a simple driver for blocking the power down event, your driver is allowed to hold up the power down process in the PowerDown device Event. that way you get a guaranteed event that you can set the flag and stop your background running process, and store any variables, and then restore them on ther PowerUp event, which is also a standard evc++ event for winCE device driver. Pretty simple really. there is 100s of demo source code on the internet for this, I've seen it on 100s of sites.
Unfortunately, on the Windows CE 6 device i am using, CreateMsgQueue does not exist in CoreDll. Any other suggestions?
The only thing i can think of is continually checking Environment.TickCount against the device's real time clock. If time has moved forward but not the tick count, then presumably the device was sleeping.
Dont like this solution because it will get tricked if the time jumps due to a failure in the device's real time clock

How to you determine when Windows is done rebooting?

I am using the Windows Update API to update a bunch of VM's. With Windows Update comes the inevitable reboots. Can anyone think of a way that I could tell from a remote server if the windows box has indeed finished its reboot? All ideas or thoughts would appreciated.
EDIT:
Because the VM's are in Lab Manager and using a fenced configuration, WMI will not work, and although I thought about using the VM to send a signal when it was back up. There would have been no way to reliably know who to notify as the app waiting for the machine could be on any number of machines so it just didn't seem reasonable. However time is not essential (and even though I know this will bite me sometime when a Service Pack comes down) I have had good success with the PING and then wait 5 minutes so far, so I am going to use that for now. If I run into exceptions I will then try to implement the VM notfiying the world when it comes back up. Thanks to all.
Just wait for it to respond to a ping.
In light of your comments:
1 - Use this script
2 - If you get any errors with that script, follow these instructions.
Check for this event in the event log:
Event Type: Information
Event Source: EventLog
Event Category: None
Event ID: 6005
Date: 7/27/2007
Time: 12:56:24 PM
User: N/A
Computer: IWSDEV
Description:
The Event log service was started.
A more general version of this question is How do I know if a system has powered on?
The reality is that you can't know when it is done booting. The boot process is pretty asynchronous and so whatever criteria you use to determine that it is done "booting" could happen before something else completes.
What I would recommend is determine what you actually want to know. What specifically is it you are waiting for? Find a way to determine whether this has taken place and forget worrying about "booting".
If you just need to know that the machine is back up but maybe hasn't completed all of the post-boot loading, put something in the startup sequence or a service that signals your code. When this signal takes place, take whatever action you need to.
You could install a startup program or service on the machine to send an email or some type of network based posting everytime it restarts.
Windows is done rebooting only slightly before it'll need rebooting again :-)
If you're specifically looking to query the status of VMs then you should check out these links which deal with the API for Virtual Server 2005:
The IVMVirtualMachine Interface...
http://msdn.microsoft.com/en-us/library/aa368465(VS.85).aspx
... has a property called State...
IVMVirtualMachine::State Property
http://msdn.microsoft.com/en-us/library/aa368637(VS.85).aspx
... which will return a value from the VMVMState Enumeration...
http://msdn.microsoft.com/en-us/library/aa368922(VS.85).aspx
At a more general level, you should probably define how much of Windows you want up and running. Do you consider network stack ready to be "rebooted" or do you need IIS/SQL or some other application level service up?
I'd probably write an app that checks the "heart-beat" of your servers - that app could well be the same one that's invoking the Windows Update stuff. You'd then get yourself a nice "console" showing you the status of your servers. The heart-beat app could ping a server, hit a static html page, hit the remote event log, use WMI or whatever you define as enough to consider your server rebooted.

Categories

Resources