Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
// hang private void button3_Click(object sender, EventArgs e) { while (true) { } }
[DllImport("kernel32.dll")] static extern uint RegisterApplicationRecoveryCallback(IntPtr pRecoveryCallback, IntPtr pvParameter, int dwPingInterval, int dwFlags); [DllImport("kernel32.dll")] static extern uint ApplicationRecoveryInProgress(out bool pbCancelled); [DllImport("kernel32.dll")] static extern uint ApplicationRecoveryFinished(bool bSuccess); delegate int ApplicationRecoveryCallback(IntPtr pvParameter);
private ApplicationRecoveryCallback mCallback; // recover private void button4_Click(object sender, EventArgs e) { mCallback = new ApplicationRecoveryCallback(this.RecoverIt); IntPtr del = Marshal.GetFunctionPointerForDelegate(mCallback); RegisterApplicationRecoveryCallback(del, IntPtr.Zero, 5000, 1 4); }
private int RecoverIt(IntPtr pvParameter) { // just an action that proves this runs File.Create(Environment.CurrentDirectory + @"\recover.txt").Close(); int i = 4; //random number to simulate delay while (i != 0) { bool b = this.Ping(); if (b) return 0; Thread.Sleep(3000); //simulates some recovery action i--; } ApplicationRecoveryFinished(true); return 0; } private bool Ping() { bool esc; ApplicationRecoveryInProgress(out esc); if (esc) { MessageBox.Show("Cancelled"); } return esc; }