Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating objects in a thread.

Creating objects in a thread.

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestion
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nikz2
    wrote on last edited by
    #1

    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.

    N A 2 Replies Last reply
    0
    • N Nikz2

      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.

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • N Nikz2

        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.

        A Offline
        A Offline
        Adam Roderick J
        wrote on last edited by
        #3

        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 [^]

        Величие не Бога может быть недооценена.

        N 1 Reply Last reply
        0
        • A Adam Roderick J

          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 [^]

          Величие не Бога может быть недооценена.

          N Offline
          N Offline
          Nikz2
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups