【MFC】CStatusBar线程中调用崩溃问题

xiao___sun 2018-07-29 09:11:12
我做了个解决方案,为了方便在各个子对话框(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;
}
...全文
512 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
向立天 2018-08-02
  • 打赏
  • 举报
回复 1
不要在线程中操作控件,发消息给主线程去完成操作
schlafenhamster 2018-07-30
  • 打赏
  • 举报
回复
"定义在了App文件中(CStatusBar m_statusBar)。" 是另一个 m_statusBar
与 MainFrame 中的 m_statusBar 毫不相干 !
MainFrame 中 做个 函数, inline CStatusBar *GetstSatusBar(){ return &m_statusBar;}
xiao___sun 2018-07-30
  • 打赏
  • 举报
回复
引用 5 楼 schlafenhamster 的回复:
"定义在了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
};
没有任何 关系 !

创建是成功的,只是线程中调用、修改会崩溃

我看了https://bbs.csdn.net/topics/110192663,找到了原因
Eleven 2018-07-30
  • 打赏
  • 举报
回复
工作线程中不要直接操作UI线程中创建的控件
schlafenhamster 2018-07-29
  • 打赏
  • 举报
回复
"定义在了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
};
没有任何 关系 !
走好每一步 2018-07-29
  • 打赏
  • 举报
回复
引用 楼主 qq_30866987 的回复:
我做了个解决方案,为了方便在各个子对话框(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;
}



不要在工作线程里操作UI,PostThreadMessage,让UI线程处理该消息
schlafenhamster 2018-07-29
  • 打赏
  • 举报
回复
p->statusBar =0 ?
zgl7903 2018-07-29
  • 打赏
  • 举报
回复
设置断点,看看指针是否都正常, 看调用堆栈是否有其他信息
struct_MyInfo *p = new struct_MyInfo; 建议把p作为类变量,否则delete时不好管理
xiao___sun 2018-07-29
  • 打赏
  • 举报
回复
我在线程中尝试用主对话框指针SetTimer(...),然后在OnTimer(...)中进行m_statusBar操作就可以

15,465

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧