Skip to content

@Vaccano /Forum1.cs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
/*************************************************************************************
* Sample application demonstrating how to register in order to receive power state
* notifications. This particular application wants to display login box upon resume
*
* Author: Alex Feinman (C) 2003
* Contact at: sample_code@alexfeinman.com
*
*************************************************************************************/
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace PowerAwareApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private IntPtr m_hEvtQuit;
private Thread m_th;
private IntPtr m_hQueue;
#region Housekeeping
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
//
// Form1
//
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "Power-aware App";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new Form1());
}
#endregion
private void Form1_Load(object sender, System.EventArgs e)
{
// Create event to stop the thread
m_hEvtQuit = CreateEvent(0, true, false, null);
// Create listener thread
m_th = new Thread(WaitThreadProc);
try
{
// Create message queue for power broadcasts
m_hQueue = CreateMsgQueue( "PowerEventWaitQueue", new MSGQUEUEOPTIONS() );
}
catch (MissingMethodException)
{
// If we got here, it means the target device does not have CE.NET. This API is not available in Windows CE 3.0
MessageBox.Show("This application uses API not available on current platform. Supported platforms are Windows CE.NET 4.1 devices, such as Windows Mobile 2003", "Error");
Close();
return;
}
// Show login form - simulate login
Login(null, null);
// Register for power notifications.
IntPtr hNotif = RequestPowerNotifications(m_hQueue, PowerEventType.PBT_TRANSITION);
// Start listening
m_th.Start();
}
private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
// Tell listener thread to stop
EventModify(m_hEvtQuit, EVENT_SET);
// Clean up
CloseHandle(m_hQueue);
}
catch
{
}
}
// This function will be called from a different thread via Control.Invoke -
// thus it has EventHandler signature
private void Login(object sender, EventArgs e)
{
// Show login form
Login login = new Login();
login.ShowDialog();
}
// Listener thread
void WaitThreadProc()
{
// Open our message queue for reading
MSGQUEUEOPTIONS opt = new MSGQUEUEOPTIONS();
opt.bReadAccess = 1;
IntPtr hQueue = CreateMsgQueue( "PowerEventWaitQueue", opt);
// Start waiting for Message Queue to fill up, while checking for a termination request
while ( WaitForSingleObject( m_hEvtQuit, 0 ) != WAIT_OBJECT_0 )
{
// Did queue get some data?
if ( WaitForSingleObject(hQueue, 100) == WAIT_OBJECT_0 )
{
// Yes, let's read it
// 1024 byte buffer should be well enough.
POWER_BROADCAST pb = new POWER_BROADCAST(1024);
int nRead, Flags;
ReadMsgQueue(hQueue, pb.Data, pb.Data.Length, out nRead, 0, out Flags);
// Is this an event we wanted?
if ( (pb.Message & PowerEventType.PBT_TRANSITION) == PowerEventType.PBT_TRANSITION &&
( pb.Flags & PowerState.POWER_STATE_ON ) == PowerState.POWER_STATE_ON )
{
//Notify parent - launch login dialog
this.Invoke(new EventHandler(Login));
}
}
}
CloseHandle(hQueue);
CloseHandle(m_hEvtQuit);
}
#region P/Invoke Stuff
[DllImport("coredll")]
static extern IntPtr CreateMsgQueue(string Name, MSGQUEUEOPTIONS Options);
[DllImport("coredll")]
static extern bool ReadMsgQueue(
IntPtr hMsgQ,
byte[] lpBuffer,
int cbBufferSize,
out int lpNumberOfBytesRead,
int dwTimeout,
out int pdwFlags
);
[DllImport("coredll")]
static extern IntPtr CreateEvent( int dwReserved1, bool bManualReset, bool bInitialState, string Name);
[DllImport("coredll")]
static extern bool EventModify(IntPtr hEvent, int func);
[DllImport("coredll")]
static extern bool CloseHandle(IntPtr h);
[DllImport("coredll")]
static extern IntPtr RequestPowerNotifications( IntPtr hMsgQ, PowerEventType Flags );
[DllImport("coredll")]
static extern int WaitForSingleObject( IntPtr hHandle, int dwMilliseconds );
const int WAIT_OBJECT_0 = 0;
const int EVENT_SET = 3;
#endregion
}
[Flags]
enum PowerEventType
{
PBT_TRANSITION = 0x00000001,
PBT_RESUME = 0x00000002,
PBT_POWERSTATUSCHANGE = 0x00000004,
PBT_POWERINFOCHANGE = 0x00000008,
}
[Flags]
enum PowerState
{
POWER_STATE_ON =(0x00010000),
POWER_STATE_OFF =(0x00020000),
POWER_STATE_CRITICAL =(0x00040000),
POWER_STATE_BOOT =(0x00080000),
POWER_STATE_IDLE =(0x00100000),
POWER_STATE_SUSPEND =(0x00200000),
POWER_STATE_RESET =(0x00800000),
}
class MSGQUEUEOPTIONS
{
public MSGQUEUEOPTIONS()
{
dwSize = Marshal.SizeOf(typeof(MSGQUEUEOPTIONS));
dwFlags = 0;
dwMaxMessages = 20;
cbMaxMessage = 100;
bReadAccess = 0;
}
public int dwSize;
public int dwFlags;
public int dwMaxMessages;
public int cbMaxMessage;
public int bReadAccess;
}
class POWER_BROADCAST
{
public POWER_BROADCAST(int size)
{
m_data = new byte[size];
}
byte[] m_data;
public byte[] Data { get { return m_data; } }
public PowerEventType Message { get { return (PowerEventType)BitConverter.ToInt32(m_data, 0); } }
public PowerState Flags { get { return (PowerState)BitConverter.ToInt32(m_data, 4); } }
public int Length { get { return BitConverter.ToInt32(m_data, 8); } }
public byte[] SystemPowerState { get { byte[] data = new byte[Length]; Buffer.BlockCopy(m_data, 12, data, 0, Length); return data; } }
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace PowerAwareApp
{
/// <summary>
/// Summary description for Login.
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
public Login()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 40);
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 40);
this.label1.Size = new System.Drawing.Size(64, 20);
this.label1.Text = "User ID:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 80);
this.label2.Size = new System.Drawing.Size(64, 20);
this.label2.Text = "Password:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 80);
this.textBox2.PasswordChar = '*';
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 136);
this.button1.Text = "Login";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Login
//
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox2);
this.Text = "Login";
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult = DialogResult.OK;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.