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. Show Form from (non-GUI) Thread

Show Form from (non-GUI) Thread

Scheduled Pinned Locked Moved C#
questionhelpdiscussion
4 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.
  • L Offline
    L Offline
    LiamD
    wrote on last edited by
    #1

    I have a thread (which is not the main GUI thread )that I want to be able to create a form using the Show method. The thread basically loops for commands and responds to them. Some of the commands are to display a form - and then wait for the next command. Now when I try to do this the Form freezes i.e. not responding. Now I assume that is because the thread is blocking waiting (Monitor.Wait) for the next command. I suppose this blocking prevent the GUI from responding to events. So my question is related to this problem. 1. Is is good practice to create Form from a thread other than the main GUI 2. Can I do a for.Show method and spawn it into a worker thread? All thoughts and suggestions would be appreciated.

    N 1 Reply Last reply
    0
    • L LiamD

      I have a thread (which is not the main GUI thread )that I want to be able to create a form using the Show method. The thread basically loops for commands and responds to them. Some of the commands are to display a form - and then wait for the next command. Now when I try to do this the Form freezes i.e. not responding. Now I assume that is because the thread is blocking waiting (Monitor.Wait) for the next command. I suppose this blocking prevent the GUI from responding to events. So my question is related to this problem. 1. Is is good practice to create Form from a thread other than the main GUI 2. Can I do a for.Show method and spawn it into a worker thread? All thoughts and suggestions would be appreciated.

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello

      LiamD wrote:

      Is is good practice to create Form from a thread other than the main GUI

      If you want your working thread to be blocked by the shown forms, you can use it to show these forms. Simple, isn't it??:) OTOH I don't think is what you want. So, simply make a thread for each form that is shown. The thread should be alive as long as the form is visible. Once the form is closed, abort the thread nd dispose it. And yes of course!! Sometimes this is a good practice, depending on the structure of your program.

      LiamD wrote:

      Can I do a for.Show method and spawn it into a worker thread?

      I don't know what is a "for.Show" method, but you can -and probably should- show each form on its own thread as above.

      Regards:rose:

      L 1 Reply Last reply
      0
      • N Nader Elshehabi

        Hello

        LiamD wrote:

        Is is good practice to create Form from a thread other than the main GUI

        If you want your working thread to be blocked by the shown forms, you can use it to show these forms. Simple, isn't it??:) OTOH I don't think is what you want. So, simply make a thread for each form that is shown. The thread should be alive as long as the form is visible. Once the form is closed, abort the thread nd dispose it. And yes of course!! Sometimes this is a good practice, depending on the structure of your program.

        LiamD wrote:

        Can I do a for.Show method and spawn it into a worker thread?

        I don't know what is a "for.Show" method, but you can -and probably should- show each form on its own thread as above.

        Regards:rose:

        L Offline
        L Offline
        LiamD
        wrote on last edited by
        #3

        I was concerned because i have never seen any examples of projects creating a thread for each form. Can you please give an example of code for this? Something liek this ... Form f = new Form(); t = new Thread(new ThreadStart(f)); t.Start(); I just want to make sure that I will not run into problem using this method when running in a multi threaded project. By the way I meant to write Form.Show() method above. Thanks, Liam

        N 1 Reply Last reply
        0
        • L LiamD

          I was concerned because i have never seen any examples of projects creating a thread for each form. Can you please give an example of code for this? Something liek this ... Form f = new Form(); t = new Thread(new ThreadStart(f)); t.Start(); I just want to make sure that I will not run into problem using this method when running in a multi threaded project. By the way I meant to write Form.Show() method above. Thanks, Liam

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello Well, running into trouble in multithreading is as easy as slipping in the mud in a rainy day!! There are two scenarios. One if you want to keep track of each thread -for several reasons I suppose-. This is the hard way, so I'd rather skip it for now:-D. The easy way is to launch each thread and let it die as it finishes without tracking -Thank God for the GarbageCollector!!-. Here is a sample code:

          private void button2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
          {
          Thread MyThread = new Thread(new ThreadStart(MyThreadMethod));
          MyThread.Start();
          }

          private void MyThreadMethod()
          {
          Form1 MyForm = new Form1();
          MyForm.ShowDialog();//To Block the thread excution
          }

          The Thread will die and get collected once the form is closed. Carful not to open too many forms though.

          Regards:rose:

          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