How to Access data of Doc in a thread
-
Hi,everyone: i access my data of Doc using following code:
CMainFrame* pMainFrmWnd = (CMainFrame*)AfxGetMainWnd(); ASSERT(pMainFrmWnd != NULL); CWnd* pchildWnd = pMainFrmWnd->GetWindow(GW_CHILD); ASSERT(pchildWnd->GetSafeHwnd() != NULL); CvcydcjView *pMyView = NULL; while(pchildWnd->GetSafeHwnd() != NULL) { ASSERT(pchildWnd->GetSafeHwnd() != NULL); if(pchildWnd->GetRuntimeClass()->m_lpszClassName == "CvcydcjView") { pMyView = (CvcydcjView*)pchildWnd; break; } pchildWnd = pchildWnd->GetWindow(GW_HWNDNEXT); } ASSERT(pMyView != NULL); CvcydcjDoc *pMydoc; pMydoc=pMyView->GetDocument(); ASSERT(pMydoc != NULL); pMydoc->MyData="demo";
but when i use it in a thread, it didn't work, it seems stopped atpMydoc=pMyView->GetDocument();
can you give me some suggestions? Thx! - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVE -
Hi,everyone: i access my data of Doc using following code:
CMainFrame* pMainFrmWnd = (CMainFrame*)AfxGetMainWnd(); ASSERT(pMainFrmWnd != NULL); CWnd* pchildWnd = pMainFrmWnd->GetWindow(GW_CHILD); ASSERT(pchildWnd->GetSafeHwnd() != NULL); CvcydcjView *pMyView = NULL; while(pchildWnd->GetSafeHwnd() != NULL) { ASSERT(pchildWnd->GetSafeHwnd() != NULL); if(pchildWnd->GetRuntimeClass()->m_lpszClassName == "CvcydcjView") { pMyView = (CvcydcjView*)pchildWnd; break; } pchildWnd = pchildWnd->GetWindow(GW_HWNDNEXT); } ASSERT(pMyView != NULL); CvcydcjDoc *pMydoc; pMydoc=pMyView->GetDocument(); ASSERT(pMydoc != NULL); pMydoc->MyData="demo";
but when i use it in a thread, it didn't work, it seems stopped atpMydoc=pMyView->GetDocument();
can you give me some suggestions? Thx! - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVEI don't have the answer to your question but a little suggestion. When you create your thread, why don't you pass the document (through the use of the LPVOID pParam) to the thread function and then use it there ?
-
Hi,everyone: i access my data of Doc using following code:
CMainFrame* pMainFrmWnd = (CMainFrame*)AfxGetMainWnd(); ASSERT(pMainFrmWnd != NULL); CWnd* pchildWnd = pMainFrmWnd->GetWindow(GW_CHILD); ASSERT(pchildWnd->GetSafeHwnd() != NULL); CvcydcjView *pMyView = NULL; while(pchildWnd->GetSafeHwnd() != NULL) { ASSERT(pchildWnd->GetSafeHwnd() != NULL); if(pchildWnd->GetRuntimeClass()->m_lpszClassName == "CvcydcjView") { pMyView = (CvcydcjView*)pchildWnd; break; } pchildWnd = pchildWnd->GetWindow(GW_HWNDNEXT); } ASSERT(pMyView != NULL); CvcydcjDoc *pMydoc; pMydoc=pMyView->GetDocument(); ASSERT(pMydoc != NULL); pMydoc->MyData="demo";
but when i use it in a thread, it didn't work, it seems stopped atpMydoc=pMyView->GetDocument();
can you give me some suggestions? Thx! - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVEDick.leeSolucky wrote: can you give me some suggestions? Using MFC documents/windows in a thread is not recommended. In fact, most of the time it simply won't work. You'll have to pass a pointer to your data to your thread routine.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
I don't have the answer to your question but a little suggestion. When you create your thread, why don't you pass the document (through the use of the LPVOID pParam) to the thread function and then use it there ?
Thank you for your suggestion. i create the thread in my view class with following code:
m_hCallthread = CreateThread( NULL, // pointer to security attributes NULL, // initial thread stack size CallOut, // pointer to thread function NULL, // argument for new thread 0, // creation flags NULL // pointer to receive thread ID );
could you tell me how to pass the pointer? thx? i try replace last NULL with this, it didn't work. :doh: sorry, i'm a new bie of vc... wait for your answer... Thank you very much! - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVE -
Dick.leeSolucky wrote: can you give me some suggestions? Using MFC documents/windows in a thread is not recommended. In fact, most of the time it simply won't work. You'll have to pass a pointer to your data to your thread routine.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Thank you for your reply. we need have a thread dealing with something(like initialize drives,and connect to voice_board and dial ...) and our main functions are all in View class, for example: displaymessage... we store the data in doc class. may i create the thread in my doc class or view class? then i can access the data easiler, but it seems if we create the callout function in view class,we get these errors:
D:\VC YDCJ\vcydcjView.cpp(431) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'int (void)' to 'unsigned long (__stdcall *)(void *)' None of the functions with this name in scope match the target type
thank you and waiting for your reply... thx! - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVE -
Thank you for your suggestion. i create the thread in my view class with following code:
m_hCallthread = CreateThread( NULL, // pointer to security attributes NULL, // initial thread stack size CallOut, // pointer to thread function NULL, // argument for new thread 0, // creation flags NULL // pointer to receive thread ID );
could you tell me how to pass the pointer? thx? i try replace last NULL with this, it didn't work. :doh: sorry, i'm a new bie of vc... wait for your answer... Thank you very much! - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVECreate your thread like that (supposed you have a pointer to your document that is called pDoc):
m_hCallthread = CreateThread( NULL, // pointer to security attributes NULL, // initial thread stack size CallOut, // pointer to thread function pDoc, // argument for new thread (the pointer to your document class) !! 0, // creation flags NULL // pointer to receive thread ID );
Then in your thread function you'll need to cast your void pointer to your document again:CYourDoc* pDocument = (CYourDoc*)pParam;
-
Create your thread like that (supposed you have a pointer to your document that is called pDoc):
m_hCallthread = CreateThread( NULL, // pointer to security attributes NULL, // initial thread stack size CallOut, // pointer to thread function pDoc, // argument for new thread (the pointer to your document class) !! 0, // creation flags NULL // pointer to receive thread ID );
Then in your thread function you'll need to cast your void pointer to your document again:CYourDoc* pDocument = (CYourDoc*)pParam;
Thank you very much! It works :laugh: - - - - - - - - - - - - - - - - - - - - - - - - - - Many nights we prayed with no proof anyone could hear, we were moving moutains long before we knew we could ! Yet now i'm standing here, although we know there's much to fear, hope seems like summer bird too swiftly flown away ... There can be miracles! When you believe, though hope is frail, it's hard to kill ! Who knows what miracles, you can achieve! When you believe, somehow you will ... YOU WILL WHEN YOU BELIEVE