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. Dialog Box

Dialog Box

Scheduled Pinned Locked Moved C#
csharpquestion
7 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.
  • T Offline
    T Offline
    tsaunders
    wrote on last edited by
    #1

    I am fairly new to C#, how do I get a dialog box to fully display before executing any code. There is no user interaction, but I display messages during the execution, but they never are seen since the box does not finish displaying until the code is finished.

    L C S 3 Replies Last reply
    0
    • T tsaunders

      I am fairly new to C#, how do I get a dialog box to fully display before executing any code. There is no user interaction, but I display messages during the execution, but they never are seen since the box does not finish displaying until the code is finished.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, the code you put in the form's constructor, or in its Load handler, will execute before the form is visible. And any code you put in any of its event handlers will run on the main or "GUI thread" at the expense of a good user interaction. If you have a lenghty operation, you really should use one (or more) separate threads, either simply instances of Thread, or members of ThreadPool, or a BackgroundWorker. That way the long operation is handled independent of the user interaction, so the form would continue to respond to moves, resizes, repaints, etc. If the long operation needs to start all by itself, launch the thread(s) from inside the form's Load handler. If it should be launched by say a button click, you obviously would put the thread launch code in the Button's Click handler. Warning: as soon as you start using separate threads you will need Control.InvokeRequired and Control.Invoke() to allow such threads to access the Form or its Controls, because only one thread is allowed to directly access Controls (the thread that created them). There are plenty of examples of this, at CodeProject and everywhere else. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Sorry for any delays in replying, I currently don't always get e-mail notifications.


      C 1 Reply Last reply
      0
      • T tsaunders

        I am fairly new to C#, how do I get a dialog box to fully display before executing any code. There is no user interaction, but I display messages during the execution, but they never are seen since the box does not finish displaying until the code is finished.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Hard to know for sure, but I'd guess that the problem is that you've got a form showing modelessly ( show instead of showdialog ) and it doesn't show while the thread is busy with other processing you do directly after. Call ShowDialog and your code will stop until the form is closed. Use a seperate thread to do processing and keep your UI working, or call Application.DoEvents to force a paint event. Hard to say if this is the problem, without seeing the code.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        1 Reply Last reply
        0
        • T tsaunders

          I am fairly new to C#, how do I get a dialog box to fully display before executing any code. There is no user interaction, but I display messages during the execution, but they never are seen since the box does not finish displaying until the code is finished.

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #4

          Do all your work in the OnShown method of your main form (which happens after the form is, well, shown). The next step would be to take Luc's advice and port your initialization code to a separate thread which could be called from the construtor or the OnLoad event.

          Sounds like somebody's got a case of the Mondays -Jeff

          T 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, the code you put in the form's constructor, or in its Load handler, will execute before the form is visible. And any code you put in any of its event handlers will run on the main or "GUI thread" at the expense of a good user interaction. If you have a lenghty operation, you really should use one (or more) separate threads, either simply instances of Thread, or members of ThreadPool, or a BackgroundWorker. That way the long operation is handled independent of the user interaction, so the form would continue to respond to moves, resizes, repaints, etc. If the long operation needs to start all by itself, launch the thread(s) from inside the form's Load handler. If it should be launched by say a button click, you obviously would put the thread launch code in the Button's Click handler. Warning: as soon as you start using separate threads you will need Control.InvokeRequired and Control.Invoke() to allow such threads to access the Form or its Controls, because only one thread is allowed to directly access Controls (the thread that created them). There are plenty of examples of this, at CodeProject and everywhere else. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Sorry for any delays in replying, I currently don't always get e-mail notifications.


            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            This always happens - I start a reply, get distracted, finish it, and look like I replied when I had nothing to add... :)

            Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

            L 1 Reply Last reply
            0
            • C Christian Graus

              This always happens - I start a reply, get distracted, finish it, and look like I replied when I had nothing to add... :)

              Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              "second that" is fine too ;P

              Luc Pattyn [Forum Guidelines] [My Articles]


              Sorry for any delays in replying, I currently don't always get e-mail notifications.


              1 Reply Last reply
              0
              • S Skippums

                Do all your work in the OnShown method of your main form (which happens after the form is, well, shown). The next step would be to take Luc's advice and port your initialization code to a separate thread which could be called from the construtor or the OnLoad event.

                Sounds like somebody's got a case of the Mondays -Jeff

                T Offline
                T Offline
                tsaunders
                wrote on last edited by
                #7

                OnShown is the perfect answer. Now the user can see all the messages as the program runs. Thank you so much. :-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