Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Sleep in 15 Seconds

Description
This script will prompt to sleep the computer with a 15 second timeout.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
3d ago
Date Last Modified
3d ago

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

// The 'windowHandle' parameter will contain the window handle for the:
//   - Active window when run by hotkey
//   - Trigger target when run by a Trigger rule
//   - TitleBar Button owner when run by a TitleBar Button
//   - Jump List owner when run from a Taskbar Jump List
//   - Currently focused window if none of these match
public static class DisplayFusionFunction
{
	public static void Run(IntPtr windowHandle)
	{
		string message = "This computer will be put to sleep in 15 seconds.";
		string title = "System going to sleep in 15 seconds";
		int timeout = 15;
		
		// Run the messagebox on another thread so we can wait for it
		DialogResult result = DialogResult.OK;
		Thread t = new Thread(() => result = MessageBox.Show(message, title, MessageBoxButtons.OKCancel));
		t.ApartmentState = ApartmentState.STA;
		t.Start();
		
		// Wait for the thread to stop with a timeout
		t.Join(timeout * 1000);
		
		// If the user cancelled exit the script
		if(result != DialogResult.OK)
			return;
		
		// Find our messagebox window if it's still open and close it
		foreach(IntPtr window in BFS.Window.GetVisibleWindowHandles())
		{
			if(!BFS.Window.GetText(window).Equals(title))
				continue;
			
			BFS.Window.Close(window);
			break;
		}
		
		// Put the computer to sleep
		Application.SetSuspendState(PowerState.Suspend, false, true);
	}
}