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. .Net WebBrowser control wont instanciate on form in new thread?

.Net WebBrowser control wont instanciate on form in new thread?

Scheduled Pinned Locked Moved C#
helpcsharpcomquestion
9 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.
  • A Offline
    A Offline
    Abydosgater
    wrote on last edited by
    #1

    Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..

    System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
    MainFormThread.Start();
    System.Threading.Thread.Sleep(1500);

    Where OpenMainForm() just has Application.Run(new MainForm()); The MainForm has a webbrowser control, and it will not compile due to the error:

    ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

    Does anyone know how i can solve this issue? Thanks for reading. Andy

    M L 2 Replies Last reply
    0
    • A Abydosgater

      Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..

      System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
      MainFormThread.Start();
      System.Threading.Thread.Sleep(1500);

      Where OpenMainForm() just has Application.Run(new MainForm()); The MainForm has a webbrowser control, and it will not compile due to the error:

      ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

      Does anyone know how i can solve this issue? Thanks for reading. Andy

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      The Web Browser control is designed to run in a Single threaded fashion. You can not use multi threading when using Web Broswer Control. :( BTW, why do you need a seperate thread for this? You can use a timer instead.

      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • A Abydosgater

        Hi, In my application i need to open 2 forms. After the first form is loaded, iti loads a second form with the following..

        System.Threading.Thread MainFormThread = new System.Threading.Thread(new System.Threading.ThreadStart(OpenMainForm));
        MainFormThread.Start();
        System.Threading.Thread.Sleep(1500);

        Where OpenMainForm() just has Application.Run(new MainForm()); The MainForm has a webbrowser control, and it will not compile due to the error:

        ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

        Does anyone know how i can solve this issue? Thanks for reading. Andy

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

        Hi, your situation is not clear to me. if you have two Application.Run() statements, I think you are on the wrong track. If the first form is just a splash screen, I know you are doing it wrong. A splash screen does not need an Application.Run, just create one and Show() it. Then do Application.Run(new Form2()); And make sure your static main() is decorated with a [STAThread] :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        A 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, your situation is not clear to me. if you have two Application.Run() statements, I think you are on the wrong track. If the first form is just a splash screen, I know you are doing it wrong. A splash screen does not need an Application.Run, just create one and Show() it. Then do Application.Run(new Form2()); And make sure your static main() is decorated with a [STAThread] :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          A Offline
          A Offline
          Abydosgater
          wrote on last edited by
          #4

          My apologies for not explaining my situation clearly. It is a small client application, the first form is a login form which shows the user a dropdown list which they select an account name from. Then I wish to pass this account name to the main form. It has been awhile since i have done any work with multi-threading but you cant just use form.Show() can you? I just tried and the form shows and then closes straight away. As far as I was aware if you are using multiple forms, you needed to use Application.Run()? Please correct me if I am wrong.

          L 1 Reply Last reply
          0
          • A Abydosgater

            My apologies for not explaining my situation clearly. It is a small client application, the first form is a login form which shows the user a dropdown list which they select an account name from. Then I wish to pass this account name to the main form. It has been awhile since i have done any work with multi-threading but you cant just use form.Show() can you? I just tried and the form shows and then closes straight away. As far as I was aware if you are using multiple forms, you needed to use Application.Run()? Please correct me if I am wrong.

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

            Abydosgater wrote:

            if you are using multiple forms, you needed to use Application.Run()?

            wrong. A Windows app that has modeless forms needs a single Application.Run, so you get one message pump.

            Abydosgater wrote:

            use form.Show()

            right. form.Show() will show the form until the user closes it, the app closes it or the app exits. it is wise to keep the form's reference alive though. Remember: A single message pump can serve multiple modeless forms (i.e. those shown by calling Show).

            Abydosgater wrote:

            multi-threading

            haven't seen your reasons for even considering multi-threading.(there is no need AFAIK). :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            A 1 Reply Last reply
            0
            • L Luc Pattyn

              Abydosgater wrote:

              if you are using multiple forms, you needed to use Application.Run()?

              wrong. A Windows app that has modeless forms needs a single Application.Run, so you get one message pump.

              Abydosgater wrote:

              use form.Show()

              right. form.Show() will show the form until the user closes it, the app closes it or the app exits. it is wise to keep the form's reference alive though. Remember: A single message pump can serve multiple modeless forms (i.e. those shown by calling Show).

              Abydosgater wrote:

              multi-threading

              haven't seen your reasons for even considering multi-threading.(there is no need AFAIK). :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              A Offline
              A Offline
              Abydosgater
              wrote on last edited by
              #6

              Ah I see. Well when i use form.Show() it shows the form and closes it straight away. It doesnt give the user a chance to select an account. I have tried form.ShowDialog() and that seems to keep the form active. LoginForm loginForm = new LoginForm(); loginForm.ShowDialog(); I then added OpenMainForm(string accountname) to the Program class: public static void OpenMainForm(string accountname) { Application.Run(new MainForm(accountname)); } So when the program launches it shows the login form, and then when the user selects an account it calls the static OpenMainForm in the Program Class which uses Application.Run(), but thats gives errors. Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead. Considering the invalid methods I have been using, would it be more logical for me to launch the main form with Application.Run(), have it hidden and show the login form as a dialog from within the main form? Andy

              L 1 Reply Last reply
              0
              • A Abydosgater

                Ah I see. Well when i use form.Show() it shows the form and closes it straight away. It doesnt give the user a chance to select an account. I have tried form.ShowDialog() and that seems to keep the form active. LoginForm loginForm = new LoginForm(); loginForm.ShowDialog(); I then added OpenMainForm(string accountname) to the Program class: public static void OpenMainForm(string accountname) { Application.Run(new MainForm(accountname)); } So when the program launches it shows the login form, and then when the user selects an account it calls the static OpenMainForm in the Program Class which uses Application.Run(), but thats gives errors. Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead. Considering the invalid methods I have been using, would it be more logical for me to launch the main form with Application.Run(), have it hidden and show the login form as a dialog from within the main form? Andy

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

                Hi, I suggest you study this example:

                public class Form2 : Form {
                static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                new Form2("a").Show();
                new Form2("b").Show();
                Application.Run(new Form2("c"));
                }
                public Form2(string title) {
                Text=title;
                }
                }

                Find out the difference between those forms. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                A 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, I suggest you study this example:

                  public class Form2 : Form {
                  static void Main() {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  new Form2("a").Show();
                  new Form2("b").Show();
                  Application.Run(new Form2("c"));
                  }
                  public Form2(string title) {
                  Text=title;
                  }
                  }

                  Find out the difference between those forms. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  A Offline
                  A Offline
                  Abydosgater
                  wrote on last edited by
                  #8

                  Ah thank you, I see where my mistake is. form.Show() only works if you have Application.Run on another form. ie: new Form2("b").Show(); Application.Run(new Form2("c")); Thank you very much =]

                  L 1 Reply Last reply
                  0
                  • A Abydosgater

                    Ah thank you, I see where my mistake is. form.Show() only works if you have Application.Run on another form. ie: new Form2("b").Show(); Application.Run(new Form2("c")); Thank you very much =]

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

                    Abydosgater wrote:

                    form.Show() only works if you have Application.Run on another form.

                    Not quite.

                    new Form2("b").Show();
                    new Form2("c").Show();
                    Application.Run();

                    also works, but behaves slightly differently. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    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