如何在虚拟机关闭之前延迟关机

时间:2016-05-13 14:01:40

标签: c#

我尝试在本地关机/重启/注销等之前关闭vbox。 我尝试了一些但不工作。 我的代码是:

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;
}

我在应用程序运行时单击关闭并获取此

error error

我该怎么办?什么错了?

1 个答案:

答案 0 :(得分:0)

试试这个 在你的方法tmr_doWork_tick中 像这样放异步

async void tmr_doWork_Tick(对象发件人等) {

//这是你的thread.sleep();  // reemplace for await Task.delay(10);

}