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. Problem with "Create a FrameWnd inside a thread"

Problem with "Create a FrameWnd inside a thread"

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
11 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    Stephan Pilz
    wrote on last edited by
    #1

    Hello, I create some instances of a window based on CFrameWnd inside a thread. The windows are created successful and I can show the windows. But after _endthread () all windows are closed and destroyed automaticly. Why that and what I should do ? Example: CViewDiffDlg *diffdlg = new CViewDiffDlg (); diffdlg->Create (NULL, ""); // Setting some window members here diffdlg->ShowWindow (SW_SHOW); _endthread (); Thank you for helping and ideas Stephan

    K 1 Reply Last reply
    0
    • S Stephan Pilz

      Hello, I create some instances of a window based on CFrameWnd inside a thread. The windows are created successful and I can show the windows. But after _endthread () all windows are closed and destroyed automaticly. Why that and what I should do ? Example: CViewDiffDlg *diffdlg = new CViewDiffDlg (); diffdlg->Create (NULL, ""); // Setting some window members here diffdlg->ShowWindow (SW_SHOW); _endthread (); Thank you for helping and ideas Stephan

      K Offline
      K Offline
      khan
      wrote on last edited by
      #2

      I think you can send a message to the main thread to create the windows for you, and give the pointer back to your thread, so you can do whatever you want with it. That way the created windows should not die with the thread. this is this.

      S 1 Reply Last reply
      0
      • K khan

        I think you can send a message to the main thread to create the windows for you, and give the pointer back to your thread, so you can do whatever you want with it. That way the created windows should not die with the thread. this is this.

        S Offline
        S Offline
        Stephan Pilz
        wrote on last edited by
        #3

        Thank you for the answer, but that don't solve my problem. I've try your solution before I write the question to CP. The effect is the same. Window(s) are created and I see all what I want, but if the thread is closed, all windows are destroyed. I don't need the window pointer anywhere. The window should be open and process his messages. Thats all. Any other ideas Best regards Stephan

        H S 2 Replies Last reply
        0
        • S Stephan Pilz

          Thank you for the answer, but that don't solve my problem. I've try your solution before I write the question to CP. The effect is the same. Window(s) are created and I see all what I want, but if the thread is closed, all windows are destroyed. I don't need the window pointer anywhere. The window should be open and process his messages. Thats all. Any other ideas Best regards Stephan

          H Offline
          H Offline
          HumanOsc
          wrote on last edited by
          #4

          Hello... The problem is that you create Windows inside from a worker thread which can produce unexpected situations... You shouldn't directly create and manipulate gui objects in a worker thread... Try to create and manipulate the window in the main thread... F.E.: Create custom user messages and send them to mainframe. like: #define WM_CREATEFRAME WM_USER + 200 #define WM_UPDATEFRAME WM_USER + 201 #define WM_DESTROYFRAME WM_USER + 202 in your worker thread: void CMainFrame::workProc(LPVOID data) { // main window CMainFrame *fr = static_cast(data); ::PostMessage(fr->m_hWnd,WM_CREATEFRAME,NULL,NULL); while(anyThing) { // do a lot of calculations ::PostMessage(fr->m_hWnd,WM_UPDATEFRAME,NULL,NULL); ::Sleep(10); } ::PostMessage(fr->m_hWnd,WM_DESTROYFRAME,NULL,NULL); } The mainframe process the custom messages and do the work on gui objects. Best regards from germany

          S 2 Replies Last reply
          0
          • S Stephan Pilz

            Thank you for the answer, but that don't solve my problem. I've try your solution before I write the question to CP. The effect is the same. Window(s) are created and I see all what I want, but if the thread is closed, all windows are destroyed. I don't need the window pointer anywhere. The window should be open and process his messages. Thats all. Any other ideas Best regards Stephan

            S Offline
            S Offline
            Sheng Jiang
            wrote on last edited by
            #5

            I don't have any problem with MFC dialogs. Maybe you need to look into the message pump in CWinThread::Run to see what happens when a main window of an MFC thread closes. http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng Command what is yours Conquer what is not ---Kane

            S 1 Reply Last reply
            0
            • H HumanOsc

              Hello... The problem is that you create Windows inside from a worker thread which can produce unexpected situations... You shouldn't directly create and manipulate gui objects in a worker thread... Try to create and manipulate the window in the main thread... F.E.: Create custom user messages and send them to mainframe. like: #define WM_CREATEFRAME WM_USER + 200 #define WM_UPDATEFRAME WM_USER + 201 #define WM_DESTROYFRAME WM_USER + 202 in your worker thread: void CMainFrame::workProc(LPVOID data) { // main window CMainFrame *fr = static_cast(data); ::PostMessage(fr->m_hWnd,WM_CREATEFRAME,NULL,NULL); while(anyThing) { // do a lot of calculations ::PostMessage(fr->m_hWnd,WM_UPDATEFRAME,NULL,NULL); ::Sleep(10); } ::PostMessage(fr->m_hWnd,WM_DESTROYFRAME,NULL,NULL); } The mainframe process the custom messages and do the work on gui objects. Best regards from germany

              S Offline
              S Offline
              Stephan Pilz
              wrote on last edited by
              #6

              Thank you for the answer, the idea is good but it doesn'nt solve the problem. Shit. In my case I call SendMessage instead of PostMessage, because the thread should wait until window creation process is finished. If I debug and stop execution before I call _endthread() I see my window in the tasklist. After _endthread() window is killed. Greetings back from germany to germany :) Stephan

              1 Reply Last reply
              0
              • S Sheng Jiang

                I don't have any problem with MFC dialogs. Maybe you need to look into the message pump in CWinThread::Run to see what happens when a main window of an MFC thread closes. http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng Command what is yours Conquer what is not ---Kane

                S Offline
                S Offline
                Stephan Pilz
                wrote on last edited by
                #7

                Thank you for the answer, To try this, I must rewrite my code, because I don't use CWinThread now. I'm from "old school" like using _beginthread () with global Threadfunctions. And so a function should terminate intern via _endthread() (extern TerminateThread()). I think, here is my problem, because the documentation describes, that _endthread() will clear up all objects from stack (and may be from heap too) they are created inside the thread. I will try your sugestion with the message pump. May be I can see some mistakes. Stephan

                S S 2 Replies Last reply
                0
                • S Stephan Pilz

                  Thank you for the answer, To try this, I must rewrite my code, because I don't use CWinThread now. I'm from "old school" like using _beginthread () with global Threadfunctions. And so a function should terminate intern via _endthread() (extern TerminateThread()). I think, here is my problem, because the documentation describes, that _endthread() will clear up all objects from stack (and may be from heap too) they are created inside the thread. I will try your sugestion with the message pump. May be I can see some mistakes. Stephan

                  S Offline
                  S Offline
                  Sheng Jiang
                  wrote on last edited by
                  #8

                  MFC thread state is needed by MFC objects. You need begin the thread with AfxBeginThread. http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng Command what is yours Conquer what is not ---Kane

                  1 Reply Last reply
                  0
                  • S Stephan Pilz

                    Thank you for the answer, To try this, I must rewrite my code, because I don't use CWinThread now. I'm from "old school" like using _beginthread () with global Threadfunctions. And so a function should terminate intern via _endthread() (extern TerminateThread()). I think, here is my problem, because the documentation describes, that _endthread() will clear up all objects from stack (and may be from heap too) they are created inside the thread. I will try your sugestion with the message pump. May be I can see some mistakes. Stephan

                    S Offline
                    S Offline
                    Stephan Pilz
                    wrote on last edited by
                    #9

                    Yo baby yo baby yo :) It works. The solution of HumanOsc was good. I had a mistake in the MessageMap. Thanks to all for helping Stephan

                    H 1 Reply Last reply
                    0
                    • H HumanOsc

                      Hello... The problem is that you create Windows inside from a worker thread which can produce unexpected situations... You shouldn't directly create and manipulate gui objects in a worker thread... Try to create and manipulate the window in the main thread... F.E.: Create custom user messages and send them to mainframe. like: #define WM_CREATEFRAME WM_USER + 200 #define WM_UPDATEFRAME WM_USER + 201 #define WM_DESTROYFRAME WM_USER + 202 in your worker thread: void CMainFrame::workProc(LPVOID data) { // main window CMainFrame *fr = static_cast(data); ::PostMessage(fr->m_hWnd,WM_CREATEFRAME,NULL,NULL); while(anyThing) { // do a lot of calculations ::PostMessage(fr->m_hWnd,WM_UPDATEFRAME,NULL,NULL); ::Sleep(10); } ::PostMessage(fr->m_hWnd,WM_DESTROYFRAME,NULL,NULL); } The mainframe process the custom messages and do the work on gui objects. Best regards from germany

                      S Offline
                      S Offline
                      Stephan Pilz
                      wrote on last edited by
                      #10

                      Yo baby yo baby yo :) It works. Your solution was good. I had a mistake in the MessageMap. Thanks to all for helping Stephan

                      1 Reply Last reply
                      0
                      • S Stephan Pilz

                        Yo baby yo baby yo :) It works. The solution of HumanOsc was good. I had a mistake in the MessageMap. Thanks to all for helping Stephan

                        H Offline
                        H Offline
                        HumanOsc
                        wrote on last edited by
                        #11

                        Hello again... My solution is based on the great Articel from Joseph M. Newcomer: http://www.codeproject.com/threads/usingworkerthreads.asp[^] This is a very nice Introduction about worker threads. Be carefull about SendMessage, it can also produce unexpected situations like a deadlock from your app... Always prever the use of PostMessage to provide a safe gui access. But Sometimes it's recommend (before or after the end of a worker thread) to get sure thats no (old) messages exists in the message queue. In this situations use PeekMessage, GetMessage and DispatchMessage in main thread to remove (process) all old Message from Queue... Best regards... :-D

                        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