我做了個解決方案,為了方便在各個子對話框(Style: Child)中呼叫CStatusBar,我將它定義在了App檔案中(CStatusBar m_statusBar)。
我首先在主對話框對m_statusBar做初始化操作,沒有問題,但是當我在一個子對話框的執行緒中(我需要在這個執行緒中對m_statusBar進行操作)呼叫時卻崩潰了。
后面是代碼:
//定義的執行緒資訊結構體
struct struct_MyInfo
{
......
CStatusBar *statusBar;
};
//對話框中OnInitDialog()
BOOL CXXXDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
......
theApp.m_statusBar.SetPaneText(0, L"正在獲取版本資訊...");//<---這里沒事
struct_MyInfo *p = new struct_MyInfo;
......
p->statusBar = &(theApp.m_statusBar);//<---m_statusBar我是定義在App中的
CWinThread *pThread = AfxBeginThread(MyThread, (LPVOID)p);
......
return TRUE; // return TRUE unless you set the focus to a control
// 例外: OCX 屬性頁應回傳 FALSE
}
//執行緒
UINT MyThread(LPVOID lpParam)
{
struct_MyInfo *p = (struct_MyInfo *)lpParam;
......
p->statusBar->SetPaneText(0, L"就緒");//<---就這里會崩潰
return 0;
}
uj5u.com熱心網友回復:
我在執行緒中嘗試用主對話框指標SetTimer(...),然后在OnTimer(...)中進行m_statusBar操作就可以uj5u.com熱心網友回復:
設定斷點,看看指標是否都正常, 看呼叫堆疊是否有其他資訊struct_MyInfo *p = new struct_MyInfo; 建議把p作為類變數,否則delete時不好管理
uj5u.com熱心網友回復:
p->statusBar =0 ?uj5u.com熱心網友回復:
不要在作業執行緒里操作UI,PostThreadMessage,讓UI執行緒處理該訊息
uj5u.com熱心網友回復:
"定義在了App檔案中(CStatusBar m_statusBar)。"與 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 中的
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
};
沒有任何 關系 !
uj5u.com熱心網友回復:
創建是成功的,只是執行緒中呼叫、修改會崩潰
我看了https://bbs.csdn.net/topics/110192663,找到了原因
uj5u.com熱心網友回復:
"定義在了App檔案中(CStatusBar m_statusBar)。" 是另一個 m_statusBar與 MainFrame 中的 m_statusBar 毫不相干 !
MainFrame 中 做個 函式, inline CStatusBar *GetstSatusBar(){ return &m_statusBar;}
uj5u.com熱心網友回復:
作業執行緒中不要直接操作UI執行緒中創建的控制元件uj5u.com熱心網友回復:
不要在執行緒中操作控制元件,發訊息給主執行緒去完成操作轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/65495.html
標籤:進程/線程/DLL
上一篇:求助:MFC用HTMLView類打開網頁,怎樣訪問網頁中的物件?
下一篇:急需幫忙
