在 ATL 中显示后台线程进度的最佳方式?

发布于 2024-12-18 05:46:49 字数 110 浏览 3 评论 0原文

使用 ATL 时,在不阻塞 UI 的情况下显示后台线程进度(例如,当它搜索文件时)的最佳方法是什么?

我仍然希望能够处理消息,允许使用“取消”按钮,并可能允许用户在搜索发生时继续使用该程序。

When using ATL, what is the best way to display the progress of a background thread (e.g. when it's searching for a file) without blocking the UI?

I still want to be able to process messages, to allow for a Cancel button and to possibly allow the user to continue working with the program while the search happens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-12-25 05:46:49

这里没有具体的 ATL。方法之一是将进度详细信息更新到成员变量中并将消息发布到 GUI 窗口,然后通过从成员变量中提取数据并更新 GUI 来处理消息,例如更新静态和/或进度条。

工作线程伪代码:

m_DataCriticalSection.Lock();
m_nProgress = (INT) (nCurrent * 100 / nTotal);
m_DataCriticalSection.Unlock();
PostMessage(WM_MYUPDATEPROGRESS);

Window:

OnMyUpdateProgress()
{
  m_DataCriticalSection.Lock();
  INT nProgress = m_nProgress;
  m_DataCriticalSection.Unlock();
  m_ProgressBar.SetPos(nProgress);
}

UPD。 真实代码片段AddText 在后台线程上调用,:

    VOID AddText(const CString& sText)
    {
            _A(sText.Find(_T('\n')) < 0);
            BOOL bIsTextEmpty;
            {
                    CRoCriticalSectionLock TextLock(m_TextCriticalSection);
                    bIsTextEmpty = m_sText.IsEmpty();
                    m_sText.Append(sText);
                    m_sText.Append(_T("\r\n"));
            }
            if(bIsTextEmpty)
                    PostPrivateMessage(WM_UPDATETEXT);
    }

并且 代码处理程序

BEGIN_MSG_MAP_EX(CMainDialog)
        // ...
        MESSAGE_HANDLER_EX(WM_UPDATETEXT, OnUpdateText)

    LRESULT OnUpdateText(UINT, WPARAM, LPARAM)
    {
            CString sText;
            {
                    CRoCriticalSectionLock TextLock(m_TextCriticalSection);
                    sText = m_sText;
                    m_sText.Empty();
            }
            if(!sText.IsEmpty())
            {
                    m_TextEdit.SetValue(m_TextEdit.GetValue() + sText);
                    const INT nTextLength = m_TextEdit.GetWindowTextLength();
                    m_TextEdit.SetSel(nTextLength, nTextLength);
            }
            return 0;
    }

这使用自定义类(不是“纯”ATL),但我希望你明白这个想法。

There is no ATL specific here. One of the ways to do is to update progress details into member variable and post a message to GUI window, then handle the message by pulling the data from member variable and updating GUI, such as updating static and/or progress bar.

Worker thread pseudo-code:

m_DataCriticalSection.Lock();
m_nProgress = (INT) (nCurrent * 100 / nTotal);
m_DataCriticalSection.Unlock();
PostMessage(WM_MYUPDATEPROGRESS);

Window:

OnMyUpdateProgress()
{
  m_DataCriticalSection.Lock();
  INT nProgress = m_nProgress;
  m_DataCriticalSection.Unlock();
  m_ProgressBar.SetPos(nProgress);
}

UPD. A real code snippet, AddText is called on background thread, :

    VOID AddText(const CString& sText)
    {
            _A(sText.Find(_T('\n')) < 0);
            BOOL bIsTextEmpty;
            {
                    CRoCriticalSectionLock TextLock(m_TextCriticalSection);
                    bIsTextEmpty = m_sText.IsEmpty();
                    m_sText.Append(sText);
                    m_sText.Append(_T("\r\n"));
            }
            if(bIsTextEmpty)
                    PostPrivateMessage(WM_UPDATETEXT);
    }

And the code handler:

BEGIN_MSG_MAP_EX(CMainDialog)
        // ...
        MESSAGE_HANDLER_EX(WM_UPDATETEXT, OnUpdateText)

    LRESULT OnUpdateText(UINT, WPARAM, LPARAM)
    {
            CString sText;
            {
                    CRoCriticalSectionLock TextLock(m_TextCriticalSection);
                    sText = m_sText;
                    m_sText.Empty();
            }
            if(!sText.IsEmpty())
            {
                    m_TextEdit.SetValue(m_TextEdit.GetValue() + sText);
                    const INT nTextLength = m_TextEdit.GetWindowTextLength();
                    m_TextEdit.SetSel(nTextLength, nTextLength);
            }
            return 0;
    }

This uses custom classes (not 'pure' ATL), but I hope you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文