How to you determine when Windows is done rebooting? - c#

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.

Related

How to prevent users from stopping a process in windows? [duplicate]

Long story short, I need to create an application that monitor the sound volume on a computer. The computer's user must not be able to stop the application no matter what.
I'll need to make my app start on computer start up, so the user can't just restart it to enter his session without the application running. As of now I don't know precisely how to do it but with some research this shouldn't be a problem.
My biggest concern is if he just stops the process in the task manager. I guess that I can't avoid that programmatically ? Is there a way to just modify the session's right so that it can't stop processes in the task manager? Or any other solution I didn't think about ?
Or there is no way I can do that and I'll just have to trust my user not to ever stop the program in the task manager ?
Thanks in advance for your help. :)
PS : This will run on a computer in a student club that runs the music for the club and that anyone can access. There are chambers where people sleep the floors above so we don't want people to put the volume too loud. That's it. No malware or anything.
In general, the only programs that act like that are malicious (e.g. rootkits). If you think about it, you really wouldn't want programs to be able to act like that.
As others have indicated, the closest you'll get here is a Windows Service, which automatically starts with Windows. Average users won't know to stop it, but it's still possible to stop it manually for power users.
One work-around you could try is to periodically have it call a web service to verify that it's running. That way you could tell who might have uninstalled or stopped the service. (The problem, of course, is that they might just not have their computer on or something; you could have separate calls for "Start" and "Still On").
Alternatively, if this installed only on computers that are exclusively under your direct control, as others have indicated, you could configure things so that you need administrative access to stop the process. This option was addressed in the comments.
I'm not accusing you of malware - just wanted to illustrate how bad it would be if you could easily make a program that the user can "never stop".
So no you can't make a program that a computer-savvy person could never stop.
But from what I gathered....
Sounds like you want to make it a service. https://msdn.microsoft.com/en-us/library/d56de412(v=vs.110).aspx.
Another (easier I think) option is just to make a console app that starts up from the Task Scheduler http://www.c-sharpcorner.com/uploadfile/manas1/console-application-using-windows-scheduler/.

Prevent users from stopping a process

Long story short, I need to create an application that monitor the sound volume on a computer. The computer's user must not be able to stop the application no matter what.
I'll need to make my app start on computer start up, so the user can't just restart it to enter his session without the application running. As of now I don't know precisely how to do it but with some research this shouldn't be a problem.
My biggest concern is if he just stops the process in the task manager. I guess that I can't avoid that programmatically ? Is there a way to just modify the session's right so that it can't stop processes in the task manager? Or any other solution I didn't think about ?
Or there is no way I can do that and I'll just have to trust my user not to ever stop the program in the task manager ?
Thanks in advance for your help. :)
PS : This will run on a computer in a student club that runs the music for the club and that anyone can access. There are chambers where people sleep the floors above so we don't want people to put the volume too loud. That's it. No malware or anything.
In general, the only programs that act like that are malicious (e.g. rootkits). If you think about it, you really wouldn't want programs to be able to act like that.
As others have indicated, the closest you'll get here is a Windows Service, which automatically starts with Windows. Average users won't know to stop it, but it's still possible to stop it manually for power users.
One work-around you could try is to periodically have it call a web service to verify that it's running. That way you could tell who might have uninstalled or stopped the service. (The problem, of course, is that they might just not have their computer on or something; you could have separate calls for "Start" and "Still On").
Alternatively, if this installed only on computers that are exclusively under your direct control, as others have indicated, you could configure things so that you need administrative access to stop the process. This option was addressed in the comments.
I'm not accusing you of malware - just wanted to illustrate how bad it would be if you could easily make a program that the user can "never stop".
So no you can't make a program that a computer-savvy person could never stop.
But from what I gathered....
Sounds like you want to make it a service. https://msdn.microsoft.com/en-us/library/d56de412(v=vs.110).aspx.
Another (easier I think) option is just to make a console app that starts up from the Task Scheduler http://www.c-sharpcorner.com/uploadfile/manas1/console-application-using-windows-scheduler/.

Aggretating Windows Event Viewer events

My boss is wanting to capture certain Windows Event Viewer events from desktops as they happen. Obviously, this means putting something on each machine that would monitor & aggregate desired events to a single database. I'm sure there are tools out there to do this, be he isn't really that interested in buying more "stuff".
I already know how I would do it, but I was interested in seeing how others might accomplish the same task.
I should make clear that I want to continuously monitor the event logs remotely
I would prefer a push-option over a pull-option.
you do not need to install anything in the client machines.
Have a look at the event log classes of the .NET Framework, when you open an event log you can pass the remote machine name to the constructor; in this way you can see the events of all machines from a centralized location (server) where you need to deploy only one copy of your application.

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.

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

Categories

Resources