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. showing form from thread

showing form from thread

Scheduled Pinned Locked Moved C#
helpquestion
19 Posts 8 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 Sunshine Always

    Hi In my application, the Main form starts a thread in another class. In this thread I need to show another form as modeless. If i call it in the thread, the form shows but in a hanged state. no controls are visible and it is in not responding state. How can i achieve my requirement. Please help. Thanks.

    A Offline
    A Offline
    AspDotNetDev
    wrote on last edited by
    #10

    private void button1_Click(object sender, EventArgs e)
    {
    System.Threading.Thread t = new System.Threading.Thread(
    new System.Threading.ThreadStart(delegate
    {
    Form1 frm = new Form1();
    frm.ShowDialog();
    }));
    t.Start();
    }

    That seems to work for me.

    [Forum Guidelines]

    D 1 Reply Last reply
    0
    • A AspDotNetDev

      private void button1_Click(object sender, EventArgs e)
      {
      System.Threading.Thread t = new System.Threading.Thread(
      new System.Threading.ThreadStart(delegate
      {
      Form1 frm = new Form1();
      frm.ShowDialog();
      }));
      t.Start();
      }

      That seems to work for me.

      [Forum Guidelines]

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

      It may work for you, but it's not going to work in all instances and environments and it WILL produce bugs that are difficult, if not impossible, to track down. The short answer is you just don't put up UI elements on anything other than the UI thread.

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

      A 1 Reply Last reply
      0
      • D Dave Kreskowiak

        It may work for you, but it's not going to work in all instances and environments and it WILL produce bugs that are difficult, if not impossible, to track down. The short answer is you just don't put up UI elements on anything other than the UI thread.

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

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #12

        Dave Kreskowiak wrote:

        it WILL produce bugs

        You sure about that? Do you have a concrete example?

        [Forum Guidelines]

        D T 2 Replies Last reply
        0
        • A AspDotNetDev

          Dave Kreskowiak wrote:

          it WILL produce bugs

          You sure about that? Do you have a concrete example?

          [Forum Guidelines]

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

          There's no such thing as a "concrete" example of this. All you have to do is ask around.

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

          A 1 Reply Last reply
          0
          • D Dave Kreskowiak

            There's no such thing as a "concrete" example of this. All you have to do is ask around.

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

            A Offline
            A Offline
            AspDotNetDev
            wrote on last edited by
            #14

            Word on the street, huh? Oooooo....k.

            [Forum Guidelines]

            D T 2 Replies Last reply
            0
            • A AspDotNetDev

              Word on the street, huh? Oooooo....k.

              [Forum Guidelines]

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

              Ask around HERE. You want the professional experience behind it? Ask around HERE. Don't take just my word for it. Ask everyone with an MVP icon next to their name.

              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
              • A AspDotNetDev

                Word on the street, huh? Oooooo....k.

                [Forum Guidelines]

                T Offline
                T Offline
                The Man from U N C L E
                wrote on last edited by
                #16

                Ask anyone who have ever done anything with threading. The most common bug I come across where I work is anything accessing the UI from another thread, either creating stuff, or just calling methods and properties across threads. It is worth noting that the debug environment runs in a rather safe mode (somehow) and so threading issues rarely show up during development, unless they are really bad. Worse, you cannot step through them easily as the timing of actions on different threads makes a big difference. In my experience 50% of threading issues break the application on the first time they are run in a release environment. Of the other 50% it depends on the hardware and network environment. We had a threading issue that only came to light a year after release when the end user upgraded their machine. Suddenly one of the threads ran faster (probably off on another core, or just from the faster processor) and the bug climbed out of hiding. World of pain!

                If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                1 Reply Last reply
                0
                • A AspDotNetDev

                  Dave Kreskowiak wrote:

                  it WILL produce bugs

                  You sure about that? Do you have a concrete example?

                  [Forum Guidelines]

                  T Offline
                  T Offline
                  The Man from U N C L E
                  wrote on last edited by
                  #17

                  Bug number 1. You used ShowDialog which should produce a modal dialog, however it is modeless, due to running on a separate thread. Therefore you can spawn hundreds of these secondary dialogs, oops. Not an execution bug in this case, but clearly a process flow bug. Having said that changing your code to Show doesn't produce any visible form at all.

                  If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                  1 Reply Last reply
                  0
                  • T The Man from U N C L E

                    You have to create all your forms on the GUI thread, of which there can only be one in the application. I suggest you invoke a method back onto the Main Form thread to create the new modeless form.

                    If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                    S Offline
                    S Offline
                    Sunshine Always
                    wrote on last edited by
                    #18

                    I tried calling a delegate.BeginInvoke in the thread for the main form in which i called the new form.show. but same results. the new form is in hanged state.

                    T 1 Reply Last reply
                    0
                    • S Sunshine Always

                      I tried calling a delegate.BeginInvoke in the thread for the main form in which i called the new form.show. but same results. the new form is in hanged state.

                      T Offline
                      T Offline
                      The Man from U N C L E
                      wrote on last edited by
                      #19

                      BeginInvoke is not needed as the Show is not going to block any threads. Just call MainForm.Invoke with the delegate to show the newForm. eg.

                              Thread thread = new Thread(new ThreadStart(StartNewThread));
                              thread.Start();
                          }
                      
                          void StartNewThread(){
                              //Application.Run(new Form2());
                              this.Invoke(new MethodInvoker(this.LaunchForm2));
                          }	
                          
                          void LaunchForm2(){
                      		Form2 frm = new Form2();
                      		frm.Show();
                          }
                      

                      I know as a standalone example my code would be daft, why start a new thread just to invoke back to the original thread, but I assume you are doing some other processing in the secondary thread to make it worthwhile.

                      If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles]  [My Website]

                      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