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#
  4. Set MdiParent form BackgroundWorker

Set MdiParent form BackgroundWorker

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 3 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.
  • D Offline
    D Offline
    ddecoy
    wrote on last edited by
    #1

    backgroundWorker1.RunWorkerAsync(mdiForm);

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
    mLoadForm = new LoadForm
    {
    StartPosition = FormStartPosition.Manual,
    Location = mNewLocation,
    TopMost = true

                            };
            mLoadForm.MdiParent = ((MdiForm)e.Argument);
            //((HotelForm)e.Argument).MdiChildren\[0\] = mLoadForm;
            Application.Run(mLoadForm); 
        }
    

    When I try to set the MdiParent for the loadform by passing the mdiform thru the RunWorkerASync parameter, the Application.Run(mLoadForm) is never reached nor is there an error being thrown ... :( This should be possible right?

    R 1 Reply Last reply
    0
    • D ddecoy

      backgroundWorker1.RunWorkerAsync(mdiForm);

      void worker_DoWork(object sender, DoWorkEventArgs e)
      {
      mLoadForm = new LoadForm
      {
      StartPosition = FormStartPosition.Manual,
      Location = mNewLocation,
      TopMost = true

                              };
              mLoadForm.MdiParent = ((MdiForm)e.Argument);
              //((HotelForm)e.Argument).MdiChildren\[0\] = mLoadForm;
              Application.Run(mLoadForm); 
          }
      

      When I try to set the MdiParent for the loadform by passing the mdiform thru the RunWorkerASync parameter, the Application.Run(mLoadForm) is never reached nor is there an error being thrown ... :( This should be possible right?

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      You're attempting to do Application.Run within a background worker??! I don't know what would happen if you do that, but its going to be weird.

      Regards, Rob Philpott.

      D 2 Replies Last reply
      0
      • R Rob Philpott

        You're attempting to do Application.Run within a background worker??! I don't know what would happen if you do that, but its going to be weird.

        Regards, Rob Philpott.

        D Offline
        D Offline
        ddecoy
        wrote on last edited by
        #3

        If i replace Application.Run(mLoadForm) by mLoadForm.Show() then the Form isn't displayed corretly (I'm running an animated gif in that form) Bu that doesn't change the fact that I cannot set the MdiParent....

        1 Reply Last reply
        0
        • R Rob Philpott

          You're attempting to do Application.Run within a background worker??! I don't know what would happen if you do that, but its going to be weird.

          Regards, Rob Philpott.

          D Offline
          D Offline
          ddecoy
          wrote on last edited by
          #4

          Also, please explain why this "will be weird", because I didn't notice anything "weird" ...

          R 1 Reply Last reply
          0
          • D ddecoy

            Also, please explain why this "will be weird", because I didn't notice anything "weird" ...

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            Well, Application.Run is traditionally used from the main() entry point to the application. It creates a standard message loop on the current thread which then responds to user input and rendering requests and the like. It's certainly normal to only have one such message loop, so using two (concurrently) opens up the dubious world of not having a single GUI thread. I've not seen it done before (except in modal dialogs etc.) And although it *may* be possible to use it, its certainly not the correct approach IMO. You might end up with MTA/STA problems maybe as well.

            Regards, Rob Philpott.

            D 1 Reply Last reply
            0
            • R Rob Philpott

              Well, Application.Run is traditionally used from the main() entry point to the application. It creates a standard message loop on the current thread which then responds to user input and rendering requests and the like. It's certainly normal to only have one such message loop, so using two (concurrently) opens up the dubious world of not having a single GUI thread. I've not seen it done before (except in modal dialogs etc.) And although it *may* be possible to use it, its certainly not the correct approach IMO. You might end up with MTA/STA problems maybe as well.

              Regards, Rob Philpott.

              D Offline
              D Offline
              ddecoy
              wrote on last edited by
              #6

              But that was the purpose: I wanted to create a thread that contains another form while the other was currently busy.So I could show an animated gif in front of the form that is busy... This seemed to be working great, except that if u minimize or moved the 'busy' form, the form in the new thread with the animated gif stays on screen, so I thought by setting the mdiParent would solve this, but apparently this can't be done... I will change my design and let the main form do its work async so I can just put a picturebox in front till the work has been done or something ...

              D D 2 Replies Last reply
              0
              • D ddecoy

                But that was the purpose: I wanted to create a thread that contains another form while the other was currently busy.So I could show an animated gif in front of the form that is busy... This seemed to be working great, except that if u minimize or moved the 'busy' form, the form in the new thread with the animated gif stays on screen, so I thought by setting the mdiParent would solve this, but apparently this can't be done... I will change my design and let the main form do its work async so I can just put a picturebox in front till the work has been done or something ...

                D Offline
                D Offline
                ddecoy
                wrote on last edited by
                #7

                Also thanks for your input ;)

                R 1 Reply Last reply
                0
                • D ddecoy

                  Also thanks for your input ;)

                  R Offline
                  R Offline
                  Rob Philpott
                  wrote on last edited by
                  #8

                  No problem. I think that's the correct approach to go. You may be familiar with the notion of 'only the thread that creates a window can access it'. This means that you can never create another window on a seperate thread then make that a child of one on another. If the two windows are completely seperate, you could use two Application.Run()s, although I'd explicity create a thread to do this and call that on it.

                  Regards, Rob Philpott.

                  D 1 Reply Last reply
                  0
                  • R Rob Philpott

                    No problem. I think that's the correct approach to go. You may be familiar with the notion of 'only the thread that creates a window can access it'. This means that you can never create another window on a seperate thread then make that a child of one on another. If the two windows are completely seperate, you could use two Application.Run()s, although I'd explicity create a thread to do this and call that on it.

                    Regards, Rob Philpott.

                    D Offline
                    D Offline
                    ddecoy
                    wrote on last edited by
                    #9

                    Rob Philpott wrote:

                    his means that you can never create another window on a seperate thread then make that a child of one on another.

                    This makes a lot of sense. Thank you !

                    1 Reply Last reply
                    0
                    • D ddecoy

                      But that was the purpose: I wanted to create a thread that contains another form while the other was currently busy.So I could show an animated gif in front of the form that is busy... This seemed to be working great, except that if u minimize or moved the 'busy' form, the form in the new thread with the animated gif stays on screen, so I thought by setting the mdiParent would solve this, but apparently this can't be done... I will change my design and let the main form do its work async so I can just put a picturebox in front till the work has been done or something ...

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Because you cannot do UI stuff, like creating forms and showing them, on anything other that the app's startup thread, also known as the UI thread. I do NOT care that "it works for me!". You or, more importantly, your customers WILL eventually find a problem that only shows up under certain circumstances that you're not going to be able to duplicate and, if you do, will be very weird and nearly impossible to track down. You just do NOT do it if you want your app to be as stable as possible. You're going about this backwords. You do not keep the long-running work on the UI thread in your main form. You move THAT work to a background thread, freeing up the UI thread to handle putting up a "progress" form and updating it. UI stuff stays on the UI thread. Actual work stuff goes on background threads. Using an MdiParent form to do this is just silly.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007, 2008
                      But no longer in 2009...

                      D 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        Because you cannot do UI stuff, like creating forms and showing them, on anything other that the app's startup thread, also known as the UI thread. I do NOT care that "it works for me!". You or, more importantly, your customers WILL eventually find a problem that only shows up under certain circumstances that you're not going to be able to duplicate and, if you do, will be very weird and nearly impossible to track down. You just do NOT do it if you want your app to be as stable as possible. You're going about this backwords. You do not keep the long-running work on the UI thread in your main form. You move THAT work to a background thread, freeing up the UI thread to handle putting up a "progress" form and updating it. UI stuff stays on the UI thread. Actual work stuff goes on background threads. Using an MdiParent form to do this is just silly.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007, 2008
                        But no longer in 2009...

                        D Offline
                        D Offline
                        ddecoy
                        wrote on last edited by
                        #11

                        Dave Kreskowiak wrote:

                        You're going about this backwords. You do not keep the long-running work on the UI thread in your main form. You move THAT work to a background thread, freeing up the UI thread to handle putting up a "progress" form and updating it.

                        I already had come to that conclusion if u had read everything.

                        Dave Kreskowiak wrote:

                        Because you cannot do UI stuff, like creating forms and showing them, on anything other that the app's startup thread, also known as the UI thread.

                        It IS possible, I had a form popup in front of my main form , created on another thread, with the Application.Run , and I had never ever got an error or something 'weird', because all it did was simply popup, show animated gif and close again.And I have this tested for weeks now...

                        D 1 Reply Last reply
                        0
                        • D ddecoy

                          Dave Kreskowiak wrote:

                          You're going about this backwords. You do not keep the long-running work on the UI thread in your main form. You move THAT work to a background thread, freeing up the UI thread to handle putting up a "progress" form and updating it.

                          I already had come to that conclusion if u had read everything.

                          Dave Kreskowiak wrote:

                          Because you cannot do UI stuff, like creating forms and showing them, on anything other that the app's startup thread, also known as the UI thread.

                          It IS possible, I had a form popup in front of my main form , created on another thread, with the Application.Run , and I had never ever got an error or something 'weird', because all it did was simply popup, show animated gif and close again.And I have this tested for weeks now...

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          ddecoy wrote:

                          It IS possible

                          Re-read what I posted. I said you may not get any problems, but you will eventually find some issue. Not essentially in this app, but if you continue to use this technique in other situations, you will find problems.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007, 2008
                          But no longer in 2009...

                          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