locked
My question is regarding C#

    Question

  • Hi there, I am trying to write a simple program but I have a problem

    This program will tell me and my friends if our servers are online or offline based on http://IPAddress:Port when a port is open it normally shows a number 1 this is how the test should work If webBrowser1 = 1 then “Online” and if it times out or can’t display the page then show “offline!. I tried;

     

             if (webBrowser1 = 1) label1.Text = "Online";

              else label1.Text = "offline!";

     

    But that never worked it just gave me an error so I managed to get it working using this;

     

            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                if (webBrowser1.Document.Focused == true) textBox1.Text = "Online";

                else textBox1.Text = "offline!";

                if (webBrowser1.Document.Focused == true) textBox2.Text = "English Help Channel ";

                else textBox2.Text = "offline!";

            }

     

    It doesn’t give me an error but even when webBrowser1 times out its showing Online where it should be showing offline!

     

    Is it possible some one might be able to help me see what I am doing wrong?

     

    Thanks.

    Wednesday, December 24, 2008 4:15 AM

Answers

  • Try something like this.  What you appear to be creating is proxy behavior for your web browser.

    void browserBuffer_DocumentCompleted(  
                object sender,   
                WebBrowserDocumentCompletedEventArgs e)  
            {  
                if (browserBuffer.ReadyState == WebBrowserReadyState.Complete)  
                {  
                    MessageBox.Show("DocumentCompleted()");  
                }  
                return;  
            } 

    You must check the ReadyState to see if there is more stuff to download, like additional frames.
    Each frame gets loaded and fires the the DocumentCompleted event.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by RapsMX Saturday, December 27, 2008 10:58 PM
    Saturday, December 27, 2008 3:49 PM

All replies

  • Hi!

    In the first sample you use the "=" operator instead of the "==" operator. Try this:

      if (webBrowser1 == 1) label1.Text = "Online";  
     
              else label1.Text = "offline!";  
     

    The second sample should be something like this, i think:

     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 

        System.Windows.Forms.HtmlDocument 
    document =  
                this.webBrowser1.Document;  
        if (document != null)  
            textBox1.Text = "Online";
        else
            textBox1.Text = "Offline";
    }
    If this does not help, please come back with more data about your app.
    Regards,
    Lucian Baciu, MCTS, http://studentclub.ro/lucians_weblog
    Thursday, December 25, 2008 1:32 AM
  • Hi there, thanks for your replay that’s how I want it almost however I tried using your code;

     

                System.Windows.Forms.HtmlDocument document =

                this.webBrowser1.Document;

                if (document != null)

                    textBox1.Text = "Online";

                else

                    textBox1.Text = "Offline";

     

    But it still gives me a fault result
     
    My IP address and port are http://217.36.216.25:6691

    When my self or my friends put this in a web browser if the port is open its result will show a number 1,
    In our case if the page can’t be displayed or times out I’m trying to make it so that will show "Offline"
    And if its result is a 1 to show "online"

     

    Thanks you very much and Happy Christmas!

    Thursday, December 25, 2008 2:21 AM
  • Hi!

    See the article I wrote here: 

    http://studentclub.ro/lucians_weblog/archive/2008/06/22/c-check-if-a-http-server-is-running-on-a-network-machine.aspx

    Regards,
    Lucian Baciu, MCTS, http://studentclub.ro/lucians_weblog
    Thursday, December 25, 2008 2:40 PM
  • Hi there

     

    Thank you for your reply; I took a look at the article you wrote here:

     

    http://studentclub.ro/lucians_weblog/archive/2008/06/22/c-check-if-a-http-server-is-running-on-a-network-machine.aspx

     

    Unfortunately that looks to confusing for me I only started using the Visual Studio tools a few months back and in class we are still on the introduction stage of Visual Web Developer 2008

     

    The app I am trying to make is a WindowsFormsApplication

     

    The first thing I dragged onto my Form1 is a webBrowser1 then I selected the webBrowser1’s properties. Then for the URL I added my IP and Port like so

     

    http://217.36.216.25:6692

     

    When I test my app now I see a number 1 in the top left corner “IF” that IP Address is online or if the port is open

     

    When I change the URL from webBrowser1 to http://000.000.0.0:0000

    And test my app again I see; “Internet Explorer cannot display the webpage”

     

    Now I drop textBox1 and textBox2 onto my Form1 then click on webBrowser1

     

    Now I am in Form1.cs and I see;

     

            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                              

            }

     

    Firstly the code I tried to use was;

     

            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                if (webBrowser1 = 1) textBox1.Text = "English Help Channel";

                else textBox1.Text = "offline!";

                if (webBrowser1 = 1) textBox2.Text = "Online!";

                else textBox2.Text = "offline!";

            }

     

    This wouldn’t work due to the error from number 1, the error message is;

     

    Error    1          Cannot implicitly convert type 'int' to 'System.Windows.Forms.WebBrowser'

     

     

            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;

                if (document != null)

                    textBox1.Text = "English Help Channel_000000000000";

                else

                    textBox1.Text = "Offline";

                if (document != null)

                    textBox2.Text = "Online!";

                else

                    textBox2.Text = "Offline";

                

            }

     

     

     

    This way it runs with out any errors how ever the result of webBrowser1 is wrong

     

    When webBrowser1 loads and shows a number 1 I need that to show for textBox1

    “English Help Room”

     

    Then if webBrowser1 loads up and its result is “Internet Explorer cannot display the webpage” I need that to show Offline


    Thanks

    Friday, December 26, 2008 6:58 PM
  • Try something like this.  What you appear to be creating is proxy behavior for your web browser.

    void browserBuffer_DocumentCompleted(  
                object sender,   
                WebBrowserDocumentCompletedEventArgs e)  
            {  
                if (browserBuffer.ReadyState == WebBrowserReadyState.Complete)  
                {  
                    MessageBox.Show("DocumentCompleted()");  
                }  
                return;  
            } 

    You must check the ReadyState to see if there is more stuff to download, like additional frames.
    Each frame gets loaded and fires the the DocumentCompleted event.

    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by RapsMX Saturday, December 27, 2008 10:58 PM
    Saturday, December 27, 2008 3:49 PM
  • Hi there, I just tried to work with the sample of code you left yet I still don’t understand sorry

     

    I tried to use your sample 3 different ways This is a sample of what the finished look should look like;


    http://www.winmx4u.com/index.2.jpg


    This is the code I am using for it at the moment;


            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

            {

                System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;

                if (document != null)

                    textBox1.Text = "English Help Channel_000000000000";

                else

                    textBox1.Text = "Offline";

                if (document != null)

                    textBox2.Text = "Online!";

                else

                    textBox2.Text = "Offline";            

            }


    Thanks and sorry for being a pain.

    Saturday, December 27, 2008 5:57 PM
  • Thank you so much for your help, I understand it now and I am able to continue with my app

     

    Thanks again, I really appreciate this

    Saturday, December 27, 2008 11:03 PM
  • Just one more question if I may

     

    How do I write it so if the website is online it will show Online in textBox1 and If IE is showing "Internet Explorer cannot display the webpage" then the message Offline goes in textBox2


    like;

                              textBox1.Show("English Help Room");

                 else

                 textBox2.Show("Offline");

                             

     

    Thanks.

    Sunday, December 28, 2008 12:01 AM
  • WinMX4U_ said:

    Just one more question if I may

     

    How do I write it so if the website is online it will show Online in textBox1 and If IE is showing "Internet Explorer cannot display the webpage" then the message Offline goes in textBox2


    like;

                              textBox1.Show("English Help Room");

                 else

                 textBox2.Show("Offline");

                             

     

    Thanks.



    Proxy Pattern

    As I noted above, what you are doing, or perhaps needing, is called a Proxy.  A Proxy takes the place of a real object so that access to the actual object can be controlled.  A Proxy class typically inherits from the class that it is staniding in for.

    In your case, you would most likely need to inherit form Uri, which means creating a class called UriProxy or something.  You would use objects of this class in the place of Uri objects.  This proxy class could use a WebBrowser control to perform the actual loading of a page. 

    Use MS Word or some other editor to create a web page or HTML document to display whatever "Page is Loading" message you want.  When the actual page is finally loaded, the Proxy Class would then pass that along instead of the temporary page.  Use this property....
    .
    public System.Uri Url { get;  set; }  
        Member of System.Windows.Forms.WebBrowser  
     
    Summary:  
    Gets or sets the URL of the current document.  
     
    Return Values:  
    A System.Uri representing the URL of the current document.  
     

    ...to display your temporary page until the desired page is loaded.  If the page cannot load that probably means your server is offline.  When the server comes back on line, a web page would actually load and the actual page loaded in to the proxy class' internal buffer would be set as that property value.  Text boxes are probably not needed, just HTML pages.

    This is not the appropriate forum Visual Web Developer.  Use the ASP.net forum.

    Hope this helps.

    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, December 29, 2008 2:10 PM