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. What is the mistake with this code???

What is the mistake with this code???

Scheduled Pinned Locked Moved C#
questioncomtutorial
11 Posts 6 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
    shivamkalra
    wrote on last edited by
    #1

    Hi, I've just started to learn multi-threading. I'm trying to make a console program that makes a web browser and then navigate to some page and post some data and invoke submit button and in method it waits for the response of the web browser. I'm using code below, could someone tell me how to do it correctly.

    private AutoResetEvent ar;
    private WebBrowser brs;

        \[STAThread\]
        private void Contribute\_Load(object sender, EventArgs e)
        {
            ar = new AutoResetEvent(false);
            brs = new WebBrowser();
            brs.Url = new Uri("http://www.gmail.com");
            brs.Navigated +=new WebBrowserNavigatedEventHandler(brs\_Navigated);
            ar.WaitOne();
            Console.WriteLine("Navigated to gmail.com");
        }
    
        void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            ar.Set();
        }
    

    This code never prints navigated to gmail. Thank you Shivam

    L D D B 4 Replies Last reply
    0
    • S shivamkalra

      Hi, I've just started to learn multi-threading. I'm trying to make a console program that makes a web browser and then navigate to some page and post some data and invoke submit button and in method it waits for the response of the web browser. I'm using code below, could someone tell me how to do it correctly.

      private AutoResetEvent ar;
      private WebBrowser brs;

          \[STAThread\]
          private void Contribute\_Load(object sender, EventArgs e)
          {
              ar = new AutoResetEvent(false);
              brs = new WebBrowser();
              brs.Url = new Uri("http://www.gmail.com");
              brs.Navigated +=new WebBrowserNavigatedEventHandler(brs\_Navigated);
              ar.WaitOne();
              Console.WriteLine("Navigated to gmail.com");
          }
      
          void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
          {
              ar.Set();
          }
      

      This code never prints navigated to gmail. Thank you Shivam

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

      this can't work, your Load handler is running on the main (aka GUI) thread and so is the Navigated handler; this means the Navigated handler will not execute as long as the Load handler hasn't finished. You are deadlocked. Most often it is a bad idea (or plain wrong) to have blocking calls (such as Thread.Sleep or AutoResetEvent.WaitOne) inside an event handler. probable solution: end your Load handler with the navigation command (you should first set the Navigated handler, then the URL); put everything that needs to be done after navigation in the Navigated handler. That is what it is for. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      S 1 Reply Last reply
      0
      • L Luc Pattyn

        this can't work, your Load handler is running on the main (aka GUI) thread and so is the Navigated handler; this means the Navigated handler will not execute as long as the Load handler hasn't finished. You are deadlocked. Most often it is a bad idea (or plain wrong) to have blocking calls (such as Thread.Sleep or AutoResetEvent.WaitOne) inside an event handler. probable solution: end your Load handler with the navigation command (you should first set the Navigated handler, then the URL); put everything that needs to be done after navigation in the Navigated handler. That is what it is for. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

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

        Hi. Thanks for the quick response. I've just started learning thread on c sharp. So could you please explain more? Basically I want a method "ValidateStudent" that will create a webbrowser object, navigate to school's website then post ID and Password and check if login succeed. But during this whole process the method should wait.

        private bool nav = false;
        private WebBrowser brs;

            public ValidateStudent()
            {
                ar = new AutoResetEvent(false);
                ar1 = new AutoResetEvent(false);
        
                brs = new WebBrowser();
                brs.Navigated += new WebBrowserNavigatedEventHandler(brs\_Navigated); 
                brs.Url = new Uri("https://rooms.library.dc-uoit.ca/studyrooms/myreservations.aspx");
        

        Console.Write(nav);

            }
        
            private void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
                nav = true;
               
            }
        

        This code is printing "False" because it does't wait for event to complete and prints "False" instantly. I want it to wait until event completes. THANK YOU Shivam

        L D 2 Replies Last reply
        0
        • S shivamkalra

          Hi, I've just started to learn multi-threading. I'm trying to make a console program that makes a web browser and then navigate to some page and post some data and invoke submit button and in method it waits for the response of the web browser. I'm using code below, could someone tell me how to do it correctly.

          private AutoResetEvent ar;
          private WebBrowser brs;

              \[STAThread\]
              private void Contribute\_Load(object sender, EventArgs e)
              {
                  ar = new AutoResetEvent(false);
                  brs = new WebBrowser();
                  brs.Url = new Uri("http://www.gmail.com");
                  brs.Navigated +=new WebBrowserNavigatedEventHandler(brs\_Navigated);
                  ar.WaitOne();
                  Console.WriteLine("Navigated to gmail.com");
              }
          
              void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
              {
                  ar.Set();
              }
          

          This code never prints navigated to gmail. Thank you Shivam

          D Offline
          D Offline
          David1987
          wrote on last edited by
          #4

          Even if it could work (but see Luc's reply), there would be a race condition. What if the navigation would be done before you add the event handler?

          L 1 Reply Last reply
          0
          • D David1987

            Even if it could work (but see Luc's reply), there would be a race condition. What if the navigation would be done before you add the event handler?

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

            that is just a minor issue that gets fixed by swapping the two relevant statements. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            D 1 Reply Last reply
            0
            • L Luc Pattyn

              that is just a minor issue that gets fixed by swapping the two relevant statements. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              D Offline
              D Offline
              David1987
              wrote on last edited by
              #6

              that's true

              1 Reply Last reply
              0
              • S shivamkalra

                Hi. Thanks for the quick response. I've just started learning thread on c sharp. So could you please explain more? Basically I want a method "ValidateStudent" that will create a webbrowser object, navigate to school's website then post ID and Password and check if login succeed. But during this whole process the method should wait.

                private bool nav = false;
                private WebBrowser brs;

                    public ValidateStudent()
                    {
                        ar = new AutoResetEvent(false);
                        ar1 = new AutoResetEvent(false);
                
                        brs = new WebBrowser();
                        brs.Navigated += new WebBrowserNavigatedEventHandler(brs\_Navigated); 
                        brs.Url = new Uri("https://rooms.library.dc-uoit.ca/studyrooms/myreservations.aspx");
                

                Console.Write(nav);

                    }
                
                    private void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
                    {
                        nav = true;
                       
                    }
                

                This code is printing "False" because it does't wait for event to complete and prints "False" instantly. I want it to wait until event completes. THANK YOU Shivam

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

                a handler is not supposed to wait at all, it should be swift: its purpose is to handle one event as fast as it can, so your app is ready to handle the next event, which could be anything from a keyboard event, a mouse moving, a window uncovering yours, etc. Do not wait for anything inside a handler!!! :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                S 1 Reply Last reply
                0
                • S shivamkalra

                  Hi, I've just started to learn multi-threading. I'm trying to make a console program that makes a web browser and then navigate to some page and post some data and invoke submit button and in method it waits for the response of the web browser. I'm using code below, could someone tell me how to do it correctly.

                  private AutoResetEvent ar;
                  private WebBrowser brs;

                      \[STAThread\]
                      private void Contribute\_Load(object sender, EventArgs e)
                      {
                          ar = new AutoResetEvent(false);
                          brs = new WebBrowser();
                          brs.Url = new Uri("http://www.gmail.com");
                          brs.Navigated +=new WebBrowserNavigatedEventHandler(brs\_Navigated);
                          ar.WaitOne();
                          Console.WriteLine("Navigated to gmail.com");
                      }
                  
                      void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
                      {
                          ar.Set();
                      }
                  

                  This code never prints navigated to gmail. Thank you Shivam

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

                  You may also run into a problem where the WebBrowser control will not work on a non-GUI thread. I've never tried it myself, but it's a possibility.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    a handler is not supposed to wait at all, it should be swift: its purpose is to handle one event as fast as it can, so your app is ready to handle the next event, which could be anything from a keyboard event, a mouse moving, a window uncovering yours, etc. Do not wait for anything inside a handler!!! :)

                    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                    S Offline
                    S Offline
                    shivamkalra
                    wrote on last edited by
                    #9

                    OK FINE. What is the solution for this problem? Suggest some alternative way of doing it.

                    1 Reply Last reply
                    0
                    • S shivamkalra

                      Hi. Thanks for the quick response. I've just started learning thread on c sharp. So could you please explain more? Basically I want a method "ValidateStudent" that will create a webbrowser object, navigate to school's website then post ID and Password and check if login succeed. But during this whole process the method should wait.

                      private bool nav = false;
                      private WebBrowser brs;

                          public ValidateStudent()
                          {
                              ar = new AutoResetEvent(false);
                              ar1 = new AutoResetEvent(false);
                      
                              brs = new WebBrowser();
                              brs.Navigated += new WebBrowserNavigatedEventHandler(brs\_Navigated); 
                              brs.Url = new Uri("https://rooms.library.dc-uoit.ca/studyrooms/myreservations.aspx");
                      

                      Console.Write(nav);

                          }
                      
                          private void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
                          {
                              nav = true;
                             
                          }
                      

                      This code is printing "False" because it does't wait for event to complete and prints "False" instantly. I want it to wait until event completes. THANK YOU Shivam

                      D Offline
                      D Offline
                      Dev RoOo7
                      wrote on last edited by
                      #10

                      I agree with this answer ... Thank you.

                      1 Reply Last reply
                      0
                      • S shivamkalra

                        Hi, I've just started to learn multi-threading. I'm trying to make a console program that makes a web browser and then navigate to some page and post some data and invoke submit button and in method it waits for the response of the web browser. I'm using code below, could someone tell me how to do it correctly.

                        private AutoResetEvent ar;
                        private WebBrowser brs;

                            \[STAThread\]
                            private void Contribute\_Load(object sender, EventArgs e)
                            {
                                ar = new AutoResetEvent(false);
                                brs = new WebBrowser();
                                brs.Url = new Uri("http://www.gmail.com");
                                brs.Navigated +=new WebBrowserNavigatedEventHandler(brs\_Navigated);
                                ar.WaitOne();
                                Console.WriteLine("Navigated to gmail.com");
                            }
                        
                            void brs\_Navigated(object sender, WebBrowserNavigatedEventArgs e)
                            {
                                ar.Set();
                            }
                        

                        This code never prints navigated to gmail. Thank you Shivam

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #11

                        Don't try to wait for the event, instead do the processing in the event handler. In your cut down example here, move the Console.WriteLine into the Navigated event handler.

                        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