Creating objects in a thread.
-
Hi. Well.. another time when i need help. I am trying to create a map (made of MPictureBox(my class derived from CButton)) in a thread. I am creating modal window and clicking "ok" button and creating map on my main window (CDialog). When i did it without threads all worked just fine. But when i put the creation in a thread it make it all very well... BUT! created buttons don't show up in the main window! (they exists in the memory but didn't show). I was thinking that probably it's some kind of problem related to using main window with different threads so i wrote this
CRITICAL\_SECTION m\_cs; BOOL result2; if (theApp.m\_hThread!=NULL) { ::InitializeCriticalSection(&m\_cs); ::EnterCriticalSection(&m\_cs); result2 = this->FieldBox.Create(FieldState,fieldid,nIDResourse, ButStyle,BtSize, lpszCaption, dwStyle, rect, pParentWnd, nID, Rotate); FieldBox.ShowWindow(SW\_SHOW); ::LeaveCriticalSection(&m\_cs); ::DeleteCriticalSection(&m\_cs); }
with 0 effect. Same trouble i had when i tryed to create some dynamic objects (with "= new ..."). Somebody had same trouble? What's the problem? (When i do the same thing without any thread it all results okey).. Pls help.
-
Hi. Well.. another time when i need help. I am trying to create a map (made of MPictureBox(my class derived from CButton)) in a thread. I am creating modal window and clicking "ok" button and creating map on my main window (CDialog). When i did it without threads all worked just fine. But when i put the creation in a thread it make it all very well... BUT! created buttons don't show up in the main window! (they exists in the memory but didn't show). I was thinking that probably it's some kind of problem related to using main window with different threads so i wrote this
CRITICAL\_SECTION m\_cs; BOOL result2; if (theApp.m\_hThread!=NULL) { ::InitializeCriticalSection(&m\_cs); ::EnterCriticalSection(&m\_cs); result2 = this->FieldBox.Create(FieldState,fieldid,nIDResourse, ButStyle,BtSize, lpszCaption, dwStyle, rect, pParentWnd, nID, Rotate); FieldBox.ShowWindow(SW\_SHOW); ::LeaveCriticalSection(&m\_cs); ::DeleteCriticalSection(&m\_cs); }
with 0 effect. Same trouble i had when i tryed to create some dynamic objects (with "= new ..."). Somebody had same trouble? What's the problem? (When i do the same thing without any thread it all results okey).. Pls help.
If this is MFC then its Window Maps are created per thread and a CWnd* object in one thread is not (easily) accessible in another. So doing UI stuff in worker threads is difficult. It is much easier to do this in the main app thread, and shuffle off any lengthy non-ui processing to worker threads.
Neville Franks, Author of Surfulater www.surfulater.com "Save what you Surf" and ED for Windows www.getsoft.com
-
Hi. Well.. another time when i need help. I am trying to create a map (made of MPictureBox(my class derived from CButton)) in a thread. I am creating modal window and clicking "ok" button and creating map on my main window (CDialog). When i did it without threads all worked just fine. But when i put the creation in a thread it make it all very well... BUT! created buttons don't show up in the main window! (they exists in the memory but didn't show). I was thinking that probably it's some kind of problem related to using main window with different threads so i wrote this
CRITICAL\_SECTION m\_cs; BOOL result2; if (theApp.m\_hThread!=NULL) { ::InitializeCriticalSection(&m\_cs); ::EnterCriticalSection(&m\_cs); result2 = this->FieldBox.Create(FieldState,fieldid,nIDResourse, ButStyle,BtSize, lpszCaption, dwStyle, rect, pParentWnd, nID, Rotate); FieldBox.ShowWindow(SW\_SHOW); ::LeaveCriticalSection(&m\_cs); ::DeleteCriticalSection(&m\_cs); }
with 0 effect. Same trouble i had when i tryed to create some dynamic objects (with "= new ..."). Somebody had same trouble? What's the problem? (When i do the same thing without any thread it all results okey).. Pls help.
usually if you are using worker and if you want to update GUI, then the best you can do is, just use SendMessage or PostMessage for GUI updation. otherwise it may to crashes, hope u dont want crashes :-D Else the suitable solution is use of UI thread, just refer [^]
Величие не Бога может быть недооценена.
-
usually if you are using worker and if you want to update GUI, then the best you can do is, just use SendMessage or PostMessage for GUI updation. otherwise it may to crashes, hope u dont want crashes :-D Else the suitable solution is use of UI thread, just refer [^]
Величие не Бога может быть недооценена.
Well.. thnx for the info it helped a lot. Now i am using UI-thread. And post messages to thread, etc. And it's all cool. It works. It creates the map of buttons and etc. BUT the problem hasn't been resolved. Buttons doesn't appear on the screen! They are created in memory but doesn't appear. Like i said the same trouble i had with pointers. I create pointer theApp.object = new CObject(...). Then theApp.object->Create(..) and theApp.object->ShowWindow(SW_SHOW) and it didn't show up. But if i write CObject globalobject; and then Create it, it appears correctly. I don't know how is that previously it worked with theApp.lvl = new ... but now it doesn't. But it all exists in the memory. I don't understand. I noted that the objects actually works. I can click them, but the objects doesn't draw itselfs. P.S. спасибо за ответ =)
modified on Monday, October 26, 2009 11:59 AM