How can I show a wait cursor from the moment I invoke the popup dialog until the dialog is visible to the user?

Viewed 634

So, I want to display a CDialog to the user:

void CMeetingScheduleAssistantDlg::OnOptionsOutlookCalendarOptions()
{
    COutlookCalendarSettingsDlg dlgSettings(this);

    dlgSettings.DoModal();
}

Now, the popup dialogue (in OnInitDialog) runs a console application behind the scenes. This console application is communicating with Microsoft Graph.

As a result, it can take a few seconds for the dialog to display.

I execute the console application with this method:

bool CMeetingScheduleAssistantApp::ExecuteProgram(CString strCommand, DWORD& rExitCode)
{
    PROCESS_INFORMATION processInformation = { nullptr };
    STARTUPINFO         startupInfo = { 0 };
    int                 nStrBuffer;
    BOOL                bProcessResult, bExitCodeProcess;
    bool                bOK = false;
    CWaitCursor         wait;

    SetProgramExecuting(true);

    rExitCode = -1;

    startupInfo.cb = sizeof(startupInfo);
    nStrBuffer = strCommand.GetLength() + 50;

    bProcessResult = CreateProcess(nullptr, strCommand.GetBuffer(nStrBuffer),
        nullptr, nullptr, FALSE,
        NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
        nullptr, nullptr, &startupInfo, &processInformation);
    strCommand.ReleaseBuffer();

    if (!bProcessResult)
    {
        // CreateProcess() failed
        // Get the error from the system
        LPVOID lpMsgBuf;
        DWORD dw = GetLastError();
        FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
            nullptr, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, nullptr);

        // Display the error
        CString strError = (LPTSTR)lpMsgBuf;
        TRACE(_T("Authenticate failed at CreateProcess()\nCommand=%s\nMessage=%s\n\n"), strCommand, strError);

        // Free resources created by the system
        LocalFree(lpMsgBuf);

        SetProgramExecuting(false);

        // We failed.
        return false;
    }
    else
    {
        // Successfully created the process.  Wait for it to finish.
        DWORD WaitResult;
        do
        {
            WaitResult = MsgWaitForMultipleObjects(1,
                // only 1 wait object
                &processInformation.hProcess, // worker thread
                FALSE,   // stop if any
                INFINITE,  // no timeout
                QS_ALLINPUT);
            if (WaitResult == WAIT_OBJECT_0 + 1)
            {
                // Handle windows message
                MSG Msg;
                while (PeekMessage(&Msg, nullptr, 0, (UINT)-1, PM_REMOVE))
                {
                    TRACE3("%d %d %d\n", Msg.message, Msg.wParam, Msg.lParam);
                    TranslateMessage(&Msg);
                    DispatchMessage(&Msg);
                }
            }
        } while (WaitResult != WAIT_OBJECT_0);
        ASSERT(WaitResult == WAIT_OBJECT_0);

        // Get the exit code.
        bExitCodeProcess = GetExitCodeProcess(processInformation.hProcess, &rExitCode);

        // Close the handles.
        CloseHandle(processInformation.hProcess);
        CloseHandle(processInformation.hThread);

        if (!bExitCodeProcess)
        {
            // Could not get exit code.
            TRACE(_T("Executed command but couldn't get exit code.\nCommand=%s\n"), strCommand);
            SetProgramExecuting(false);
            return false;
        }

        SetProgramExecuting(false);
        return true;
    }
}

Inside OnInitDialog, just before the ExecuteProgram is called, I tried using:

CWaitCursor wait;

But it makes no difference. So how can I show a wait cursor from the moment I invoke the popup dialog until the dialog is visible to the user?

1 Answers
Related