2016-05-13 49 views
3

我試圖在本地關機/重新啓動/註銷之前關閉vbox等 我嘗試了一些東西,但沒有工作。 我的代碼是:如何才能延遲關閉,直到virtualbox關閉

private void VMStarter_Load(object sender, EventArgs e) 
{ 
    ... 
    ... 
    ShutDownHandle.StopShutdown(this.Handle, "Virtual Box is shutting down..."); 
} 

private void tmr_doWork_tick(object sender, EventArgs e) 
{ 
    tmr_doWork.Enabled = false; 
    Controller.closeVM(); //for 
    while (!ShutDownHandle.ResetShutdown(this.Handle)) 
    { 
     Thread.Sleep(10); 
    } 
    ShutDownHandle.Shutdown(); 
    this.Close(); 
} 

//[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] 
protected override void WndProc(ref Message m) 
{ 
    if (!systemShutdown) 
     if (m.Msg == (int)EnumClass.WindowsMessageCodes.SM_SHUTTINGDOWN || 
      m.Msg == (int)EnumClass.WindowsMessageCodes.WM_ENDSESSION || 
      m.Msg == (int)EnumClass.WindowsMessageCodes.WM_QUERYENDSESSION) 
     { 
      tmr_Check.Enabled = false; 
      //Message MyMsg = new Message(); 
      //MyMsg.Msg = (int)EnumClass.WindowsMessageCodes.WM_CANCELMODE; 
      //base.WndProc(ref MyMsg); 
      systemShutdown = true; 
      tmr_doWork.Enabled = true; 
      return; 
     } 
    base.WndProc(ref m); 
} 

ShutDownHandle類:包括StopShutdown和ResetShutdown梅索德。我使用ShutdownBlockReasonCreateShutdownBlockReasonDestroy來停止關閉窗口。

public static bool StopShutdown(IntPtr hWdn, string strMessage) 
{ 
    try 
    { 
     if (ShutdownBlockReasonCreate(hWdn, strMessage)) 
     { 
      return true; 
     } 
    } 
    catch (Exception ex) 
    { 
     Writer.errorWrite(ex); 
    } 
    return false; 
} 

public static bool ResetShutdown(IntPtr hWdn) 
{ 
    try 
    { 
     return ShutdownBlockReasonDestroy(hWdn); 
    } 
    catch (Exception ex) 
    { 
     return false; 
     Writer.errorWrite(ex); 
    } 
    return false; 
} 

我當應用程序正在運行單擊關閉並得到這個

errorerror

我該怎麼辦?怎麼了?

回答

0

試試這個 在你的方法tmr_doWork_tick 放異步這樣

異步無效tmr_doWork_Tick(對象發件人,等..) {

//這裏是你的Thread.sleep()方法; //替代等候Task.delay(10);

}

+0

我有一個關閉VirtualMachine的.bat文件。它必須運行。但是,直到圖片中出現的單擊取消按鈕纔會顯示。當我點擊取消,通常運行和虛擬機關機。任何想法這種情況。 –