【问题标题】:How to lock a resource using multithreading?如何使用多线程锁定资源?
【发布时间】:2014-09-04 16:24:08
【问题描述】:

我有一个属性表,其中有四页。在第二页中,我有一个列表控件和一个按钮。在第二页中,我创建了两个线程。当我在第一页中单击下一步时,我尝试使用从网络中检索到的一些值来枚举列表控件。因此,这里的搜索对话框和枚举列表正在两个并行运行的不同线程中处理。在页面的前面,弹出了搜索对话框和后台检索来自网络的值,并用这些值枚举列表。在此期间,如果我单击客户区域,则此搜索对话框将被最小化。但这不应该发生,除非搜索对话框被关闭,否则我不能可以访问父窗口(与 ModalDiaolg 框相同的场景,我们知道,除非子窗口关闭,否则我们将无法访问父窗口,我需要类似的场景。)这是我所做的代码为了得到那些线程一次运行。

    BOOL CModelSelectionView::CreateModelThread()
        {
               unsigned threadID;

          if( NULL == ( m_hModelThread = (HANDLE)_beginthreadex(
                       NULL,
                        0,
                       &CModelSelectionView::ModelThreadProc,
                       reinterpret_cast<void*>(this),
                       0,
                       &threadID)) )
      {
               return FALSE;
      }



      return TRUE;
     }

//此线程用于搜索对话框

    UINT CModelSelectionView::ModelThreadProc( void* lpContext )
    {
     CModelSelectionView *pSelectModelFromList = 
      reinterpret_cast<CModelSelectionView*> (lpContext);`
      AfxSetResourceHandle(theApp.m_hDialogResource);

      CSearchingView SearchView(IDD_DIALOG_SEARCH);

     INT nRes = SearchView.DoModal();
    ::CloseHandle( pSelectModelFromList->m_hModelThread );
     pSelectModelFromList->m_hModelThread = NULL;

    _endthreadex( 0 );



 return TRUE;
}

BOOL CModelSelectionView::CreateInstallerThread()
{
    unsigned threadID;
if( NULL == ( m_hInstallerThread = (HANDLE)_beginthreadex(
    NULL,
    0,
    &CModelSelectionView::InstallerThreadProc,
    reinterpret_cast<void*>(this),
    0,
    &threadID)) )
{
    return FALSE;
}

return TRUE;
}

//用一些值初始化列表的第二个线程

UINT CModelSelectionView::InstallerThreadProc( void* lpContext )
{
    CModelSelectionView *pSelectModelFromList = 
    reinterpret_cast<CModelSelectionView*> (lpContext);
    pSelectModelFromList->m_listCtrl.DeleteAllItems();

    LVITEM lvitem;
    lvitem.mask = LVIF_TEXT;
    lvitem.iItem = 0;
    lvitem.iSubItem = 0;
    lvitem.pszText = L"";
    lvitem.cchTextMax = sizeof(lvitem.pszText);
    int nItem = pSelectModelFromList->m_listCtrl.InsertItem(&lvitem);
    ::Sleep(200);

    pSelectModelFromList->m_listCtrl.SetItemText(0,1,L"XXX");
    pSelectModelFromList->m_listCtrl.SetItemText(0,2,L"YYY");
    pSelectModelFromList->m_listCtrl.SetItemText(0,3,L"ZZZ");
    pSelectModelFromList->m_listCtrl.SetItemText(0,4,L"AAAA");


::Sleep(200);



::TerminateThread(pSelectModelFromList->m_hModelThread, 0);
    ::CloseHandle(pSelectModelFromList->m_hModelThread );
    pSelectModelFromList->m_hModelThread = NULL;

    ::CloseHandle( pSelectModelFromList->m_hInstallerThread );
    pSelectModelFromList->m_hInstallerThread = NULL;

    _endthreadex( 0 );

    return TRUE;
}

除非搜索对话框关闭,否则它不应该被允许访问父窗口。例如,当单击一个按钮并且对于那个按钮处理程序我正在调用 domodal 时,会出现一个子对话框弹出,直到我们关闭该对话框我们将不被允许访问父权限,同样我必须在这种情况下获得。

任何人都可以建议我如何实现这一目标。

谁能给我建议一下

【问题讨论】:

    标签: visual-c++ mfc


    【解决方案1】:

    对于不应接收任何输入的窗口,只需 EnableWindow(FALSE)。它仍会显示并且其内容会更新,但鼠标和键盘事件不会到达此窗口。

    【讨论】:

      猜你喜欢
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多