tags:

views:

66

answers:

2

I'm developing an in-house .NET application that will be run on a VM (with VMware), and want to know if there's a way to get notifications from VM system events (like suspending, resumed, etc.)

Anyone know of a convenient way to do that? The virtual machine has VMware Tools installed, does that provide a .NET API for hooking events?

EDIT: In particular, I'm interested in when the system has just resumed. I assumed that this doesn't correspond to any "regular" Windows system event (after all, the whole point of suspending and resuming a VM is that Windows has no idea what happened). Am I mistaken? Will that trigger an event?

EDIT 2: I wrote this quick console app to hook all the System Events I could think of, and get nothing when I suspend/resume:

static void Main(string[] args) {

    SystemEvents.DisplaySettingsChanged += (sender, e) => Console.WriteLine("Display settings changed");
    SystemEvents.EventsThreadShutdown += (sender, e) => Console.WriteLine("Events thread shutdown");
    SystemEvents.PowerModeChanged += (sender, e) => Console.WriteLine("Power mode changed");
    SystemEvents.SessionEnding += (sender, e) => Console.WriteLine("Session ending");
    SystemEvents.SessionSwitch += (sender, e) => Console.WriteLine("Session switch");
    SystemEvents.UserPreferenceChanging += (sender, e) => Console.WriteLine("User preference changing");

    Console.ReadLine();
}
+2  A: 

are there any vmware-specific events you'd be listening for? Otherwise it sounds like you'd be better off listening for those events from Windows


Take a look at WM_POWERBROADCAST -- http://www.pinvoke.net/default.aspx/user32.RegisterPowerSettingNotification

STW
I don't know which system events would correspond to a "resume"... see edit.
Henry Jackson
if you can detect it entering standby then you can set some sort of flag that the application is in standby--if the application starts running and sees it's in standby then it can infer that it must have just resumed
STW
That would work if it went into standby, but suspending a VM isn't the same as the standby power mode.
Henry Jackson
haven't found anything promising in the documentation--detecting a Suspend operating might not be possible (would you want to delay the Suspend if your application took a while to complete?)... so it's also possible there isn't any notification to the guest that it's been resumed
STW
A: 

OK, so I've given up on looking for an easy .NET hook for this, but if someone else stumbles on this and wants to know how I solved it:

I have a timer in my app that fires regularly (every 10 seconds), and compares the current time to the last time. If the time is appreciably longer than 10 seconds, I assume that the computer has either been asleep or suspended, and refresh my app as needed.

A little hacky, but the timer adds nothing to the program's CPU or memory usage, so I figure it's not horrible.

Henry Jackson