子线程调用主线程函数以及变量问题

dianzi011sh 2009-12-07 10:46:32
已经参考了很多朋友的提问以及答案了,现在编译时有以下问题:
error C2673: ‘Action1’: global functions do not have 'this' pointers
请高手详细解释一下其中的调用过程

test.h:
static DWORD Scrap1(); (这里如果不是定义static,就报error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)')

test.cpp:
DWORD Action1()
{
((CBatchScrapDlg*)CWnd::FromHandle(this->m_hWnd))->Scrap();
return 0;
}

void CTestDlg::OnAction()
{

hThread=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)Action1,
NULL,
0,
NULL);
}


一个朋友说如下结论,我的疑问是:FuncTest() 他在testdlg.h中如何定义
假设你的对话框是CTestDlg,成员是FuncTest()
可以这样用:
((CTestDlg*)CWnd::FromHandle(this->m_hWnd))->FuncTest();

...全文
906 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Allen_zhang 2009-12-07
  • 打赏
  • 举报
回复
1、线程函数是静态的,只能使用静态的参数及函数,所以类的隐藏指针this是不能在静态函数中使用的
如果要使用的话需要你将这个指针传递进来,这个1楼已经说了

2、3楼中的错误虽然把this指针传进去了,但是用法不对

DWORD Scrap1(CBatchScrapDlg* ptr)
{
CBatchScrapDlg * _this = (CBatchScrapDlg *)ptr;
// ((CBatchScrapDlg*)CWnd::FromHandle(this->m_hWnd))->Scrap(); (1688)
// 直接改为下面这句就可以了
_this->Scrap();
return 0;
}


3、6楼的问题是你这个对话框创建了没有啊?在线程起来的时候
dianzi011sh 2009-12-07
  • 打赏
  • 举报
回复
是在后面的
UpdateData(false); 挂了。。。。
[Quote=引用 6 楼 dianzi011sh 的回复:]
现在在scrap()中调用如下,程序直接挂了。。。。。请教了
GetDlgItem(IDSCRAP)->EnableWindow(FALSE); 
GetDlgItem(IDSCRAPCANCEL)->EnableWindow(FALSE);


引用 4 楼 dianzi011sh 的回复:
DWORD Scrap1(CBatchScrapDlg* ptr) 改成DWORD Scrap1(LPVOID  ptr)

引用 3 楼 dianzi011sh 的回复:
感谢!不过还有错误:BatchScrapDlg.cpp(1688) : error C2673: 'Scrap1' : global functions do not have 'this' pointers
(.cpp)
DWORD Scrap1(CBatchScrapDlg* ptr)
....
  (


[/Quote]
hallowwar 2009-12-07
  • 打赏
  • 举报
回复
Mark,学习了。
dianzi011sh 2009-12-07
  • 打赏
  • 举报
回复
现在在scrap()中调用如下,程序直接挂了。。。。。请教了
GetDlgItem(IDSCRAP)->EnableWindow(FALSE);
GetDlgItem(IDSCRAPCANCEL)->EnableWindow(FALSE);


[Quote=引用 4 楼 dianzi011sh 的回复:]
DWORD Scrap1(CBatchScrapDlg* ptr) 改成DWORD Scrap1(LPVOID  ptr)

引用 3 楼 dianzi011sh 的回复:
感谢!不过还有错误:BatchScrapDlg.cpp(1688) : error C2673: 'Scrap1' : global functions do not have 'this' pointers
(.cpp)
DWORD Scrap1(CBatchScrapDlg* ptr)
....
  (

[/Quote]
fego_gl 2009-12-07
  • 打赏
  • 举报
回复
((CBatchScrapDlg*)CWnd::FromHandle(this->m_hWnd))->Scrap();
==>
((CBatchScrapDlg*)CWnd::FromHandle(_this->m_hWnd))->Scrap();
dianzi011sh 2009-12-07
  • 打赏
  • 举报
回复
DWORD Scrap1(CBatchScrapDlg* ptr) 改成DWORD Scrap1(LPVOID ptr)

[Quote=引用 3 楼 dianzi011sh 的回复:]
感谢!不过还有错误:BatchScrapDlg.cpp(1688) : error C2673: 'Scrap1' : global functions do not have 'this' pointers
(.cpp)
DWORD Scrap1(CBatchScrapDlg* ptr)
....
  (
[/Quote]
dianzi011sh 2009-12-07
  • 打赏
  • 举报
回复
感谢!不过还有错误:BatchScrapDlg.cpp(1688) : error C2673: 'Scrap1' : global functions do not have 'this' pointers
(.h)
class CBatchScrapDlg : public CDialog
{
public:
void Scrap();
}
static DWORD Scrap1(LPVOID lParam);

(.cpp)
DWORD Scrap1(CBatchScrapDlg* ptr)
{
CBatchScrapDlg * _this = (CBatchScrapDlg *)ptr;
((CBatchScrapDlg*)CWnd::FromHandle(this->m_hWnd))->Scrap(); (1688)
return 0;
}

void CBatchScrapDlg::OnScrap()
{
hThread=CreateThread(NULL,
0,
(unsigned long(_stdcall*)(void*))Scrap1,
// (LPTHREAD_START_ROUTINE)Scrap1,
(LPVOID)this,
0,
&ThreadID);
}
bragi523 2009-12-07
  • 打赏
  • 举报
回复
CTestDlg * _this = (CTestDlg *)lParam;
放在
DWORD Action1(LPVOID lParam)
{

((CBatchScrapDlg*)CWnd::FromHandle(_this->m_hWnd))->Scrap();
return 0;
} 里面
bragi523 2009-12-07
  • 打赏
  • 举报
回复
你要把this指针传进去
static函数不能使用外部的东西
hThread=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)Action1,
(LPVOID)this,
0,
NULL);


static DWORD Scrap1(LPVOID lParam);


CTestDlg * _this = (CTestDlg *)lParam;
DWORD Action1(LPVOID lParam)
{

((CBatchScrapDlg*)CWnd::FromHandle(_this->m_hWnd))->Scrap();
return 0;
}
ls443085074 2009-12-07
  • 打赏
  • 举报
回复
*.h:
static DWORD CALLBACK FUN(LPVOID lparam)
void _FUN();
*.cpp:
DWORD CXX:FUN(LPVOID lparam)
{
CXX *pThis = (CXX*)lparam;
pThis->_FUN();
return 0;
}
void _FUN()
{
//线程要实现的东西
}
dianzi011sh 2009-12-07
  • 打赏
  • 举报
回复

感谢!你思路很清楚,佩服。
(.cpp)
子线程中调用主线程中的Scrap(),这样使用就有问题,请教思路,谢谢。实际上此时Dlg一直存在
Scrap(){
GetDlgItem(IDRESET)->EnableWindow(FALSE);
UpdateData(false);
。。。。。
}

[Quote=引用 9 楼 allen_zhang 的回复:]
1、线程函数是静态的,只能使用静态的参数及函数,所以类的隐藏指针this是不能在静态函数中使用的
如果要使用的话需要你将这个指针传递进来,这个1楼已经说了

2、3楼中的错误虽然把this指针传进去了,但是用法不对
3、6楼的问题是你这个对话框创建了没有啊?在线程起来的时候
[/Quote]

15,471

社区成员

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

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