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. How to start a form from another thread

How to start a form from another thread

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelptutorialannouncement
5 Posts 2 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

    Hi, I am writing a forms based application, which has been working fine, until I moved my project to another computer with the same version of Visual studio. Now my initial form does not show up at all. This is what I have done: I have a console application that starts three forms, one after the other. Each form implements two methods invloved in starting the form in its own separate thread. class UIForm : .... { Thread formThread = null; public void OpenForm() { // formThread is a private member of the current form formThread = new Thread( new ThreadStart( this.ShowForm ) ); formThread.Start(); } private void ShowForm() { Application.Run( this ); } . . . } The console application then starts the form like this: UIForm theForm = new UIForm(); theForm.OpenForm(); This worked fine until I moved my project to another machine. Any help, comments or advise is much appreciated!! Thanks!

    M 1 Reply Last reply
    0
    • S SebbaP

      Hi, I am writing a forms based application, which has been working fine, until I moved my project to another computer with the same version of Visual studio. Now my initial form does not show up at all. This is what I have done: I have a console application that starts three forms, one after the other. Each form implements two methods invloved in starting the form in its own separate thread. class UIForm : .... { Thread formThread = null; public void OpenForm() { // formThread is a private member of the current form formThread = new Thread( new ThreadStart( this.ShowForm ) ); formThread.Start(); } private void ShowForm() { Application.Run( this ); } . . . } The console application then starts the form like this: UIForm theForm = new UIForm(); theForm.OpenForm(); This worked fine until I moved my project to another machine. Any help, comments or advise is much appreciated!! Thanks!

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      Given my limited understanding of all things .Net, take what I say with a grain of salt. The thread that creates the window must be the one that updates the window. The thread that creates the window must also participate in a message loop. Although you may get away with violating these rules some of the time, it is guarenteed to bite you in the butt sometime in the future. Unless you are doing something really special, I would have one thread handle all of the UI and have the other threads doing non-UI stuff. Use Form.Invoke() to handle communication between the threads.

      S 1 Reply Last reply
      0
      • M Michael Potter

        Given my limited understanding of all things .Net, take what I say with a grain of salt. The thread that creates the window must be the one that updates the window. The thread that creates the window must also participate in a message loop. Although you may get away with violating these rules some of the time, it is guarenteed to bite you in the butt sometime in the future. Unless you are doing something really special, I would have one thread handle all of the UI and have the other threads doing non-UI stuff. Use Form.Invoke() to handle communication between the threads.

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

        I agree to what you say, but I don't see how I violate any of the rules in the startup phase. The form is started in its own thread, which should then be bound to the message loop. Or have I misunderstood things?

        M 1 Reply Last reply
        0
        • S SebbaP

          I agree to what you say, but I don't see how I violate any of the rules in the startup phase. The form is started in its own thread, which should then be bound to the message loop. Or have I misunderstood things?

          M Offline
          M Offline
          Michael Potter
          wrote on last edited by
          #4

          You created the window using one thread then executed another to make it visible. This violates the first principle I stated above. Try creating the window with the new thread.

          S 1 Reply Last reply
          0
          • M Michael Potter

            You created the window using one thread then executed another to make it visible. This violates the first principle I stated above. Try creating the window with the new thread.

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

            Thank you very much! I discovered your solution by trial and error, but I did not understand why. Now I now that there is something behind it. Again, thank you.

            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