这个错误其实是MFC在DEBUG模式下的保护性代码报错(有兴趣可以去读读代码),如果用Release模式编译和运行,以你的情况应该不会有错,只不过在逻辑上存在危险。 我觉得更通用的方法应该是线程里面向主进程发自定义的消息来处理在wincore.cpp的886行有解释说明 // Note: if either of the above asserts fire and you are // writing a multithreaded application, it is likely that // you have passed a C++ object from one thread to another // and have used that object in a way that was not intended. // (only simple inline wrapper functions should be used) // // In general, CWnd objects should be passed by HWND from // one thread to another. The receiving thread can wrap // the HWND with a CWnd object by using CWnd::FromHandle. // // It is dangerous to pass C++ objects from one thread to // another, unless the objects are designed to be used in // such a manner.
which DLL are u trying to use?? because some C-runtime dll's are not thread save, let us know which DLL you are trying to use..some source code would help as well...
hmm.. fopen is a C function...
If you want to do file access in win32, try the use these functionsCreateFile() ReadFile() WriteFile()you can look up MSDN on how to use these functions...
用 ((C??Dlg*)(CWnd::FromHandle(g_hWnd)))->UpdateData(FALSE)
就出现“0xxxxxx内存不能read”的错误
但是我要在对话框上显示,怎么办?
“在线程里建立一个对话框的指针,在线程启动前把对话框的指针传递给线程里的指针对象。”
----不行
“建立一个全局的对话框指针对象,在线程里调用也行。 ”
----也不行到底是什么原因不行呢?
那样就得先取得对话框中编辑框的句柄,有没有更好的方法?看来我得加分了
主线程收到后进行updatedata操作,就这么easy
CMyDlg* pDlg = (CMyDlg *)AfxGetApp()->m_pMainWnd;
pDlg->GetDlgItem(IDC_Edit1)->SetWindowText("OK");
关键是在线程中获得主窗口指针只有通过AfxGetApp()->m_pMainWnd方式!调用UpdateData肯定死菜,不过你可以通过GetDlgItem方法获得控件指针来操作,
一样达到要求!
这与用什么方式创建线程没有关系!
不过不主张 使用 CreateThread()
UpdateData()是CWnd 的一个保护成员函数,在外面是不可以调用的,自己在程序写一个消息处理函数,在处理函数中调用 UpdateData()就可以了。
我觉得更通用的方法应该是线程里面向主进程发自定义的消息来处理在wincore.cpp的886行有解释说明
// Note: if either of the above asserts fire and you are
// writing a multithreaded application, it is likely that
// you have passed a C++ object from one thread to another
// and have used that object in a way that was not intended.
// (only simple inline wrapper functions should be used)
//
// In general, CWnd objects should be passed by HWND from
// one thread to another. The receiving thread can wrap
// the HWND with a CWnd object by using CWnd::FromHandle.
//
// It is dangerous to pass C++ objects from one thread to
// another, unless the objects are designed to be used in
// such a manner.
为什么???
欢迎到“VC专家答疑”提问:
http://bros4.top263.net
CWnd::FromHandle()不行啊
我传CMyDlg *pdlg给线程,然后pdlg->SetDlgItemText()行。
这样有什么不安全吗?
然后用CWnd::FromHandle(),就可以生成一个临时变量了
再用SetDlgItemText()
{
.................
CWnd *pWnd=CWnd::FromHandle((HWND)lpArg))
}AfxBeginThread((AFX_THREADPROC)Func,(LPVOID)this->m_hWnd);
用你的啥都没有,没显示,也没出错我传CMyDlg *pdlg给线程,然后pdlg->SetDlgItemText(),已经搞定了。
现在的问题是:
线程函数调用了另外一个DLL函数,
DLL函数读不了文件,用fopen()和CreateFile()都不能读取一个保证正确的文件
为什么?
ReadFile()
WriteFile()you can look up MSDN on how to use these functions...
提到SetDlgItemText()都得吧
在线程之间传递窗口、GDI对象和其他对象时应该通过句柄来代替指向MFC对象的指针进行传递。通常情况下,如果用传递句柄来代替传递指针,并且然后使用FromHandle()来在当前线程的临时句柄映射表中“重建”对象,则问题就会少一些。但是这样做并不意味着任何函数都能工作。例如:在线程中调用CMainFrame::GetActiveDocument()时,就会由于ASSERT_VALID(this)引起断言错误。ASSERT_VALID调用CWnd::AssertValid(),它通过确保相关联的HWND出现在永久或临时句柄映射表中来执行安全性检查,框架用这个映射表把HWND转换成CWnd。由一个CWnd转换成一个HWND是容易的,因为HWND是该类的一个数据成员,但是把一个HWND转换成一个CWnd,则只能通过映射表。并且这里有一个问题:句柄映射表对每个线程是局部的,并且对其他线程是不可见的。如果其地址被传给ASSERT_VALID的CWnd是另一个线程创建的,那么相应的HWND将不会出现在当前线程的永久或临时句柄映射表中,并且MFC将引发断言。许多MFC的非内联函数调用ASSERT_VALID,但内联函数不调用它。
在线程里面,就可以直接用value->edit->SetWindowText()来更新显示了。有多个控件就用多个变量吧。挺好用的。