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. One application with multiple forms

One application with multiple forms

Scheduled Pinned Locked Moved C#
designsysadminquestion
8 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.
  • S Offline
    S Offline
    SebbaP
    wrote on last edited by
    #1

    What I am trying to do is to first start a form, which gives the user a choice between starting the application as a server or as a client. When the user has made this choice, the form should terminate. Now I wish to start another form, which provides another user interface. Like this: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); } The first form works well, but the other one (ui) does not show up at all. What am I doing wrong?

    B 1 Reply Last reply
    0
    • S SebbaP

      What I am trying to do is to first start a form, which gives the user a choice between starting the application as a server or as a client. When the user has made this choice, the form should terminate. Now I wish to start another form, which provides another user interface. Like this: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); } The first form works well, but the other one (ui) does not show up at all. What am I doing wrong?

      B Offline
      B Offline
      Brian Nottingham
      wrote on last edited by
      #2

      You are not doing anything wrong, this is a bug in the framework. When Application.Run finishes, a bit is set indicating that the message pump on the thread has shutdown. This bit is sticky, and any subsequent calls to Application.Run see this bit set and immediately terminate. However, this should workaround your problem: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); }

      S 1 Reply Last reply
      0
      • B Brian Nottingham

        You are not doing anything wrong, this is a bug in the framework. When Application.Run finishes, a bit is set indicating that the message pump on the thread has shutdown. This bit is sticky, and any subsequent calls to Application.Run see this bit set and immediately terminate. However, this should workaround your problem: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); Application.Run(serverOrClient); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); UserInterface ui = new UserInterface(); Application.Run(ui); // pass information to and from ui obj = ui.GetInfo(); // ... ui.SetInfo(obj); }

        S Offline
        S Offline
        SebbaP
        wrote on last edited by
        #3

        Thank you very much for your reply! Regrettably, your solution does not work for me. It still behaves as it did before. I have changed the code into the following: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); System.Threading.Thread.Sleep(1000); UserInterface ui = new UserInterface(); ui.ShowDialog(); } I can clearly see the second form flashing by after the 1 second delay. The first form, however, works fine.

        B 1 Reply Last reply
        0
        • S SebbaP

          Thank you very much for your reply! Regrettably, your solution does not work for me. It still behaves as it did before. I have changed the code into the following: static void Main() { ServerOrClientForm serverOrClient = new ServerOrClientForm(); serverOrClient.ShowDialog(); serverOrClient.InputSelection(); System.Threading.Thread.Sleep(1000); UserInterface ui = new UserInterface(); ui.ShowDialog(); } I can clearly see the second form flashing by after the 1 second delay. The first form, however, works fine.

          B Offline
          B Offline
          Brian Nottingham
          wrote on last edited by
          #4

          Please look at the code I had posted. For the "UserInterface" class, do not use ShowDialog(), but rather Application.Run(ui).

          S 1 Reply Last reply
          0
          • B Brian Nottingham

            Please look at the code I had posted. For the "UserInterface" class, do not use ShowDialog(), but rather Application.Run(ui).

            S Offline
            S Offline
            SebbaP
            wrote on last edited by
            #5

            I have tried that, and it did not work either. That's why I tried to use ShowDialog() on both of them.

            B 1 Reply Last reply
            0
            • S SebbaP

              I have tried that, and it did not work either. That's why I tried to use ShowDialog() on both of them.

              B Offline
              B Offline
              Brian Nottingham
              wrote on last edited by
              #6

              What does the function "InputSelection" do? In a simple test application, this works fine for me: static void Main() { Form1 form1 = new Form1(); form1.ShowDialog(); Application.Run(new Form2()); }

              S 1 Reply Last reply
              0
              • B Brian Nottingham

                What does the function "InputSelection" do? In a simple test application, this works fine for me: static void Main() { Form1 form1 = new Form1(); form1.ShowDialog(); Application.Run(new Form2()); }

                S Offline
                S Offline
                SebbaP
                wrote on last edited by
                #7

                I found the source to my troubles. When the user presses the OK button in my ServerOrClientForm its clicked handler issued a call to Application.Exit(). Now I call this.Close() instead, which obviously was the better choice. But I am not sure why. Do you know why? InputSelection is just a way of blocking the caller until ServerOrClientForm has completed its task (merely a busy-wait loop in which I am checking a 'done' flag and if it's not set, put the thread to sleep for a little while). The thing is, I want the form to produce a result and then feed that result to anyone that call theForm.GetResult, and at the same time block the caller until the result is ready. Can you come up with a good solution? Thank you very much for helping me with my original problem.

                H 1 Reply Last reply
                0
                • S SebbaP

                  I found the source to my troubles. When the user presses the OK button in my ServerOrClientForm its clicked handler issued a call to Application.Exit(). Now I call this.Close() instead, which obviously was the better choice. But I am not sure why. Do you know why? InputSelection is just a way of blocking the caller until ServerOrClientForm has completed its task (merely a busy-wait loop in which I am checking a 'done' flag and if it's not set, put the thread to sleep for a little while). The thing is, I want the form to produce a result and then feed that result to anyone that call theForm.GetResult, and at the same time block the caller until the result is ready. Can you come up with a good solution? Thank you very much for helping me with my original problem.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  Application.Exit posts the WM_QUIT message which signals to windows to shut down (note, that's "windows", not "Windows"). The application will ultimately exit. It's typically not a good idea to call Application.Exit. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles]

                  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