多线程同步问题

好人吗 2012-05-21 06:02:57
我在一个对话框中添加了一个文本框和一个按钮,在点击这个按钮之后启动一个线程在文本框上动态显示“正在连接……”,后面的小数点一个一个的显示。先贴代码:
自定义类:

struct ThreadInfo
{
BOOL m_connect;
CCsDlg m_dlg;
};
在CCsDlg类中,我做了如下修改:
protect:
ThreadInfo* info;
CWinThread* pThread;
private:
CCriticalSection m_CCritical_Section;
增加了一个一个事件处理函数:
通过IDE增加。
void CCsDlg::OnBtnStart()
{
// TODO: Add your control notification handler code here
UpdateData();
info->m_connect =FALSE;
info->m_dlg=this;
pThread=AfxBeginThread(ThreadFunc,&info);
UpdateData(FALSE);
}

增加了全局函数:

UINT ThreadFunc(LPVOID* a)
{
int i;
i=0;
ThreadInfo* temp=(ThreadInfo*)a;
temp->m_dlg.m_CCritical_Section.Lock();
temp->m_dlg.m_CStrMess="正在等待客户端连接";
while(!temp->m_connect)
{
if(6==i)
{
i=0;
}
for(int j=0;j<i;j++)
{
temp->m_dlg.m_CStrMess+=". "; //m_CStrMess和详细编辑框关联
}
temp->m_dlg.UpdateData(FALSE);
Sleep(50);
}
temp->m_dlg.m_CCritical_Section.Unlock();

return 0;
}
现在编译出错:
c:\documents and settings\administrator\桌面\cs\cs\csdlg.h(28) : warning C4150: deletion of pointer to incomplete type 'ThreadInfo'; no destructor called
c:\documents and settings\administrator\桌面\cs\cs\csdlg.h(19) : see declaration of 'ThreadInfo'
C:\Documents and Settings\Administrator\桌面\cs\cs\csDlg.cpp(22) : error C2248: 'm_CCritical_Section' : cannot access private member declared in class 'CCsDlg'
c:\documents and settings\administrator\桌面\cs\cs\csdlg.h(62) : see declaration of 'm_CCritical_Section'
C:\Documents and Settings\Administrator\桌面\cs\cs\csDlg.cpp(37) : error C2248: 'm_CCritical_Section' : cannot access private member declared in class 'CCsDlg'
c:\documents and settings\administrator\桌面\cs\cs\csdlg.h(62) : see declaration of 'm_CCritical_Section'
C:\Documents and Settings\Administrator\桌面\cs\cs\csDlg.cpp(136) : error C2582: 'CCsDlg' : 'operator =' function is unavailable

请问为什么?
我QQ37642052
...全文
164 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
好人吗 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

线程函数中最好不要直接操作控件,改用发送自定义消息到UI界面吧
[/Quote]
我在构造函数里面初始化了指针,有new ThreadInfo的语句。
现在我能在线程中访问对话框类的public 成员变量,但是在线程中UpdateData(FALSE)时候就报错了。
代码如下:
UINT ThreadFunc(LPVOID a)
{
int i;
i=0;
ThreadInfo* temp=(ThreadInfo*)a;
temp->m_dlg->m_CCritical_Section.Lock();
(temp->m_dlg)->m_CStrMess="正在等待客户端连接";
(temp->m_dlg)->UpdateData(FALSE);
temp->m_dlg->m_CCritical_Section.Unlock();

return 0;
}
好人吗 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

你的info 没有new ,你贴出来的代码只看到你定义了ThreadInfo* info;
指针。在onintidlg 里面info = new ThreadInfo
[/Quote]
我在构造函数里面初始化了指针,有new ThreadInfo的语句。
现在我能在线程中访问对话框类的public 成员变量,但是在线程中UpdateData(FALSE)时候就报错了。
代码如下:
UINT ThreadFunc(LPVOID a)
{
int i;
i=0;
ThreadInfo* temp=(ThreadInfo*)a;
temp->m_dlg->m_CCritical_Section.Lock();
(temp->m_dlg)->m_CStrMess="正在等待客户端连接";
(temp->m_dlg)->UpdateData(FALSE);
temp->m_dlg->m_CCritical_Section.Unlock();

return 0;
}
好人吗 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

能编过去了就调试一下
看看具体哪里出了问题
[/Quote]
我在构造函数里面初始化了指针,有new ThreadInfo的语句。
现在我能在线程中访问对话框类的public 成员变量,但是在线程中UpdateData(FALSE)时候就报错了。
代码如下:
UINT ThreadFunc(LPVOID a)
{
int i;
i=0;
ThreadInfo* temp=(ThreadInfo*)a;
temp->m_dlg->m_CCritical_Section.Lock();
(temp->m_dlg)->m_CStrMess="正在等待客户端连接";
(temp->m_dlg)->UpdateData(FALSE);
temp->m_dlg->m_CCritical_Section.Unlock();

return 0;
}
明寿 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
不要在线程内访问对话框资源,很多资源都不能在线程内正常使用的,尤其是涉及消息处理方面的函数调用。
[/Quote]
+1
zhengjiankang 2012-05-22
  • 打赏
  • 举报
回复
这种程序建议使用Timer。
点击时候SetTimer(...)
然后响应Timer消息即可。
ls443085074 2012-05-22
  • 打赏
  • 举报
回复
你的info 没有new ,你贴出来的代码只看到你定义了ThreadInfo* info;
指针。在onintidlg 里面info = new ThreadInfo
dahaiI0 2012-05-22
  • 打赏
  • 举报
回复
temp->m_dlg.UpdateData(FALSE);
这句注释掉应该就可以了
向立天 2012-05-22
  • 打赏
  • 举报
回复
能编过去了就调试一下
看看具体哪里出了问题
liyafeng1976 2012-05-22
  • 打赏
  • 举报
回复
不要在线程内访问对话框资源,很多资源都不能在线程内正常使用的,尤其是涉及消息处理方面的函数调用。
好人吗 2012-05-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

你的m_dlg应该声明成指针
[/Quote]
现在我改了一些东西,编译不报错了,但是还有一些警告,并且运行后我一点击按钮程序就挂了。
d:\cproject\cs\csdlg.h(28) : warning C4150: deletion of pointer to incomplete type 'ThreadInfo'; no destructor called
d:\cproject\cs\csdlg.h(19) : see declaration of 'ThreadInfo'
csDlg.cpp
d:\cproject\cs\csdlg.h(28) : warning C4150: deletion of pointer to incomplete type 'ThreadInfo'; no destructor called
d:\cproject\cs\csdlg.h(19) : see declaration of 'ThreadInfo'
Generating Code...
Linking...
Creating browse info file...

cs.exe - 0 error(s), 0 warning(s)
Eleven 2012-05-21
  • 打赏
  • 举报
回复
线程函数中最好不要直接操作控件,改用发送自定义消息到UI界面吧
向立天 2012-05-21
  • 打赏
  • 举报
回复
你的m_dlg应该声明成指针
dahaiI0 2012-05-21
  • 打赏
  • 举报
回复
额。又看了下,要改的还不止这里
temp->m_dlg.UpdateData(FALSE);这句有问题,线程里发消息给主窗口,让主窗口来调用UpdateData
dahaiI0 2012-05-21
  • 打赏
  • 举报
回复
private:改成public:
CCriticalSection m_CCritical_Section;

15,979

社区成员

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

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