Visual c++ 如何使用多线程锁定资源?

Visual c++ 如何使用多线程锁定资源?,visual-c++,mfc,Visual C++,Mfc,我有一个属性表,其中有四个页面。在第二个页面中,我有一个列表控件和一个按钮。在第二个页面中,我创建了两个线程。当我在第一个页面中单击“下一步”时,我尝试使用从网络检索的一些值枚举列表控件。因此,在这里,a搜索对话框和枚举列表是在两个不同的线程中并行运行的。在页面的前面,搜索对话框弹出,后台从网络中检索值,列表用这些值进行枚举。在此期间,如果我单击客户端区域,则此搜索对话框被激活最小化。但除非关闭搜索对话框,否则这种情况不会发生。我不能被授予访问父窗口的权限(与ModalDiaolg框的情况相同,

我有一个属性表,其中有四个页面。在第二个页面中,我有一个列表控件和一个按钮。在第二个页面中,我创建了两个线程。当我在第一个页面中单击“下一步”时,我尝试使用从网络检索的一些值枚举列表控件。因此,在这里,a搜索对话框和枚举列表是在两个不同的线程中并行运行的。在页面的前面,搜索对话框弹出,后台从网络中检索值,列表用这些值进行枚举。在此期间,如果我单击客户端区域,则此搜索对话框被激活最小化。但除非关闭搜索对话框,否则这种情况不会发生。我不能被授予访问父窗口的权限(与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;
     }
boolcmodelselectionview::CreateModelThread()
{
无符号threadID;
如果(NULL==(m_hModelThread=(HANDLE)_beginthreadex(
无效的
0,
&CModelSelectView::ModelThreadProc,
重新解释(本),
0,
&threadID)))
{
返回FALSE;
}
返回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 CModelSelectView::ModelThreadProc(void*lpContext) { CModelSelectView*pSelectModelFromList= 重新解释演员阵容(lpContext)` AfxSetResourceHandle(theApp.m_hDialogResource); CSearchingView搜索视图(IDD_对话框_搜索); INT nRes=SearchView.DoModal(); ::CloseHandle(pSelectModelFromList->m_hModelThread); pSelectModelFromList->m_hModelThread=NULL; _endthreadex(0); 返回TRUE; } boolcmodelselectionview::CreateInstallerThread() { 无符号threadID; 如果(NULL==(m_hInstallerThread=(HANDLE)_beginthreadex( 无效的 0, &CModelSelectView::InstallerThreadProc, 重新解释(本), 0, &threadID))) { 返回FALSE; } 返回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;
}
UINT CModelSelectView::InstallerThreadProc(void*lpContext) { CModelSelectView*pSelectModelFromList= 重新解释演员阵容(lpContext); pSelectModelFromList->m_listCtrl.DeleteAllItems(); LVITEM LVITEM; lvitem.mask=LVIF_文本; lvitem.iItem=0; lvitem.iSubItem=0; lvitem.pszText=L“”; lvitem.cchTextMax=sizeof(lvitem.pszText); int nItem=pSelectModelFromList->m_listCtrl.InsertItem(&lvitem); ::睡眠(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”); ::睡眠(200); ::TerminateThread(pSelectModelFromList->m_hModelThread,0); ::CloseHandle(pSelectModelFromList->m_hModelThread); pSelectModelFromList->m_hModelThread=NULL; ::CloseHandle(pSelectModelFromList->m_hInstallerThread); pSelectModelFromList->m_hInstallerThread=NULL; _endthreadex(0); 返回TRUE; } 除非搜索对话框关闭,否则不允许访问父窗口。例如,当单击一个按钮时,对于我调用domodal的按钮处理程序,会出现一个子对话框弹出窗口,除非我们关闭该对话框,否则将不允许访问父窗口,类似地,我必须进入此场景

有谁能建议我怎样才能做到这一点

任何人都可以建议我如何为不应该接收任何输入的窗口启用窗口(FALSE)。它仍将显示并更新其内容,但鼠标和键盘事件不会到达此窗口