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. A thread that creates multiple dialogs

A thread that creates multiple dialogs

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncareerworkspace
13 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.
  • B Offline
    B Offline
    bu7ch3r
    wrote on last edited by
    #1

    Hi, I have a dialog, a main dialog(MainDlg) that on press of a button creates another 12 dialogs, hidden. All 12 dialogs hava about 20-25 controls on them and when i all Create for each of them the maindlg freezes for 1-3 seconds. I am thinking of creating a thread that creates all 12 dialogs when MainDlg begins to initialize. So what I am doing now and it's not working. I have created a new class derived from CWinThread. OnInitialise I have put: create(FirstDialog:IDD,g_thisParent)...g_thisParent is CWnd * pointing to 'this'. I made a function, Setup(CWnd *, Cdialog **dialogs); this is the pointer sent from OnInit of the maindlg functio. On mainDlg ->Oninit, i have created CDialog *dialogs[12]; all dialogs are NULL and then dialogs[1]->new....(this); anyhow: i start the thread with AfxBeginThread suspended, i call for Setup, and then I resume the thread. I get assertion faild when I try to create using this as parent, but not when i use (CWnd*)this->GetParent wich is 0 . I don't want my dialogs to be the child of desktop window, I want them to inherit maidlg....what shall i do? Is this a corect approach of the problem?

    M A E 3 Replies Last reply
    0
    • B bu7ch3r

      Hi, I have a dialog, a main dialog(MainDlg) that on press of a button creates another 12 dialogs, hidden. All 12 dialogs hava about 20-25 controls on them and when i all Create for each of them the maindlg freezes for 1-3 seconds. I am thinking of creating a thread that creates all 12 dialogs when MainDlg begins to initialize. So what I am doing now and it's not working. I have created a new class derived from CWinThread. OnInitialise I have put: create(FirstDialog:IDD,g_thisParent)...g_thisParent is CWnd * pointing to 'this'. I made a function, Setup(CWnd *, Cdialog **dialogs); this is the pointer sent from OnInit of the maindlg functio. On mainDlg ->Oninit, i have created CDialog *dialogs[12]; all dialogs are NULL and then dialogs[1]->new....(this); anyhow: i start the thread with AfxBeginThread suspended, i call for Setup, and then I resume the thread. I get assertion faild when I try to create using this as parent, but not when i use (CWnd*)this->GetParent wich is 0 . I don't want my dialogs to be the child of desktop window, I want them to inherit maidlg....what shall i do? Is this a corect approach of the problem?

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      Do not create UI stuff (dialogs, controls, ...) from you secondary thread. In you thread, you need to notify (for example using PostMessage) your main dialog to create the child dialogs. read : http://www.flounder.com/workerthreads.htm[^] excerpt : "That's right. A worker thread must not touch a GUI object. This means that you should not query the state of a control, add something to a list box, set the state of a control, etc."

      Watched code never compiles.

      B 1 Reply Last reply
      0
      • B bu7ch3r

        Hi, I have a dialog, a main dialog(MainDlg) that on press of a button creates another 12 dialogs, hidden. All 12 dialogs hava about 20-25 controls on them and when i all Create for each of them the maindlg freezes for 1-3 seconds. I am thinking of creating a thread that creates all 12 dialogs when MainDlg begins to initialize. So what I am doing now and it's not working. I have created a new class derived from CWinThread. OnInitialise I have put: create(FirstDialog:IDD,g_thisParent)...g_thisParent is CWnd * pointing to 'this'. I made a function, Setup(CWnd *, Cdialog **dialogs); this is the pointer sent from OnInit of the maindlg functio. On mainDlg ->Oninit, i have created CDialog *dialogs[12]; all dialogs are NULL and then dialogs[1]->new....(this); anyhow: i start the thread with AfxBeginThread suspended, i call for Setup, and then I resume the thread. I get assertion faild when I try to create using this as parent, but not when i use (CWnd*)this->GetParent wich is 0 . I don't want my dialogs to be the child of desktop window, I want them to inherit maidlg....what shall i do? Is this a corect approach of the problem?

        A Offline
        A Offline
        Avi Berger
        wrote on last edited by
        #3

        I don't think this will work as you are creating them as part of the second (temporary?) thread when you want them to be part of the main GUI thread. Instead, since these dialogs start out hidden, why not defer their creation. Set up a wrapper to create them on first display. This distributes the work so that you don't take the full hit all at once and it should be less noticeable. You could also set up something to create them one at a time in CWinApp::OnIdle(), though you will need to make sure they are not used until created. You could combine this with the create on first demand scheme if you want.

        Please do not read this signature.

        B 1 Reply Last reply
        0
        • M Maximilien

          Do not create UI stuff (dialogs, controls, ...) from you secondary thread. In you thread, you need to notify (for example using PostMessage) your main dialog to create the child dialogs. read : http://www.flounder.com/workerthreads.htm[^] excerpt : "That's right. A worker thread must not touch a GUI object. This means that you should not query the state of a control, add something to a list box, set the state of a control, etc."

          Watched code never compiles.

          B Offline
          B Offline
          bu7ch3r
          wrote on last edited by
          #4

          But the problem is that I must create the dialogs at sometime. I have a menu Backup Restore shedule whatever backup has 12 dialogs........ if i click on backup button i must wait. I cannot avoid the creation of those dialogs. when clicking on backup they must be created, and when i create them, each oninit function in the dialog starts from 1 to 12. In thread everything is ok....i win a lot of time(3 seconds) and when I click on backup......i seems it instantly loads. The only problem I get is that i can't set the parent right...:(

          M 1 Reply Last reply
          0
          • A Avi Berger

            I don't think this will work as you are creating them as part of the second (temporary?) thread when you want them to be part of the main GUI thread. Instead, since these dialogs start out hidden, why not defer their creation. Set up a wrapper to create them on first display. This distributes the work so that you don't take the full hit all at once and it should be less noticeable. You could also set up something to create them one at a time in CWinApp::OnIdle(), though you will need to make sure they are not used until created. You could combine this with the create on first demand scheme if you want.

            Please do not read this signature.

            B Offline
            B Offline
            bu7ch3r
            wrote on last edited by
            #5

            Good point, but using UI thread it's ok, my question is why i cannot set the parrent right, I think it has something to do with sending 'this' as param in Setup function....should I sent a handle to the window instead....the problem is I don't know how, I'm new in MFC GUI;))

            M A 2 Replies Last reply
            0
            • B bu7ch3r

              But the problem is that I must create the dialogs at sometime. I have a menu Backup Restore shedule whatever backup has 12 dialogs........ if i click on backup button i must wait. I cannot avoid the creation of those dialogs. when clicking on backup they must be created, and when i create them, each oninit function in the dialog starts from 1 to 12. In thread everything is ok....i win a lot of time(3 seconds) and when I click on backup......i seems it instantly loads. The only problem I get is that i can't set the parent right...:(

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              Why do you need to create ALL your dialogs when the main dialog starts ? Me think it's bad design to do so, only create dialogs when you need them. Anyway, unless you are doing some weird things, creating a dialog should not take that much time.

              Watched code never compiles.

              B 1 Reply Last reply
              0
              • B bu7ch3r

                Hi, I have a dialog, a main dialog(MainDlg) that on press of a button creates another 12 dialogs, hidden. All 12 dialogs hava about 20-25 controls on them and when i all Create for each of them the maindlg freezes for 1-3 seconds. I am thinking of creating a thread that creates all 12 dialogs when MainDlg begins to initialize. So what I am doing now and it's not working. I have created a new class derived from CWinThread. OnInitialise I have put: create(FirstDialog:IDD,g_thisParent)...g_thisParent is CWnd * pointing to 'this'. I made a function, Setup(CWnd *, Cdialog **dialogs); this is the pointer sent from OnInit of the maindlg functio. On mainDlg ->Oninit, i have created CDialog *dialogs[12]; all dialogs are NULL and then dialogs[1]->new....(this); anyhow: i start the thread with AfxBeginThread suspended, i call for Setup, and then I resume the thread. I get assertion faild when I try to create using this as parent, but not when i use (CWnd*)this->GetParent wich is 0 . I don't want my dialogs to be the child of desktop window, I want them to inherit maidlg....what shall i do? Is this a corect approach of the problem?

                E Offline
                E Offline
                Eugen Podsypalnikov
                wrote on last edited by
                #7

                bu7ch3r wrote:

                ...that on press of a button creates another 12 dialogs... ...the maindlg freezes for 1-3 seconds. ...what shall i do?

                Do not press any button to create them, just create them all in CMainDlg::OnInitDialog() :)

                virtual void BeHappy() = 0;

                B 1 Reply Last reply
                0
                • B bu7ch3r

                  Good point, but using UI thread it's ok, my question is why i cannot set the parrent right, I think it has something to do with sending 'this' as param in Setup function....should I sent a handle to the window instead....the problem is I don't know how, I'm new in MFC GUI;))

                  M Offline
                  M Offline
                  Maximilien
                  wrote on last edited by
                  #8

                  In your thread, just post a message to your main dialog with (no data) and in that message handler in your main dialog you should be able to create your dialogs without problems.

                  Watched code never compiles.

                  1 Reply Last reply
                  0
                  • M Maximilien

                    Why do you need to create ALL your dialogs when the main dialog starts ? Me think it's bad design to do so, only create dialogs when you need them. Anyway, unless you are doing some weird things, creating a dialog should not take that much time.

                    Watched code never compiles.

                    B Offline
                    B Offline
                    bu7ch3r
                    wrote on last edited by
                    #9

                    Yes indeed the design is bad, and the problem problem started from weird things. There were some variables uninitialized entering in a loop from 0 to a few milions;)) so i was suggested to use a thread to create dialogs so i got stuck on this ideea:P the problem is, that i can jump from a dialog to another.

                    1 Reply Last reply
                    0
                    • E Eugen Podsypalnikov

                      bu7ch3r wrote:

                      ...that on press of a button creates another 12 dialogs... ...the maindlg freezes for 1-3 seconds. ...what shall i do?

                      Do not press any button to create them, just create them all in CMainDlg::OnInitDialog() :)

                      virtual void BeHappy() = 0;

                      B Offline
                      B Offline
                      bu7ch3r
                      wrote on last edited by
                      #10

                      yes, but that makes the gui start slow .

                      E 1 Reply Last reply
                      0
                      • B bu7ch3r

                        yes, but that makes the gui start slow .

                        E Offline
                        E Offline
                        Eugen Podsypalnikov
                        wrote on last edited by
                        #11

                        Place a good splash window in another thread for your start :)

                        virtual void BeHappy() = 0;

                        B 1 Reply Last reply
                        0
                        • E Eugen Podsypalnikov

                          Place a good splash window in another thread for your start :)

                          virtual void BeHappy() = 0;

                          B Offline
                          B Offline
                          bu7ch3r
                          wrote on last edited by
                          #12

                          =))))) i am creating a serious application for my university degree:P I's a good ideea anyhow:D

                          1 Reply Last reply
                          0
                          • B bu7ch3r

                            Good point, but using UI thread it's ok, my question is why i cannot set the parrent right, I think it has something to do with sending 'this' as param in Setup function....should I sent a handle to the window instead....the problem is I don't know how, I'm new in MFC GUI;))

                            A Offline
                            A Offline
                            Avi Berger
                            wrote on last edited by
                            #13

                            After doing a little checking, evidently you can do it with child windows on a 2nd UI thread. You do have to be careful about communications between the threads since there is a possibility of creating a deadlock between the threads. I would still tend to favor using OnIdle() instead of a 2nd thread to try and deal with your performance issue. Apart from some quibbling over precise wording, I don't see the problem with what you are doing. As far as my quibbling goes, in the parent window code you call a setup function passing "this" as a parameter so that the thread class can save its value in a member variable called g_thisParent. g_thisParent is not a pointer to "this", it is a CWnd * to the parent window object. Also, in the second thread, you never, ever use "this" in an attempt to refer to the parent window, you use the variable you have named g_thisParent. Other than that, my only thought would be some sort of synchronization issue - but I don't see what it would be.

                            Please do not read this signature.

                            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