简体   繁体   中英

c# DocumentLoadedEvent not firing

I want my program to get the latitude and longitude of the ISS from a website, so I have the following code:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Diagnostics;
 using System.Threading;
 using System.Speech.Synthesis;
 using System.Windows.Forms;
 using System.Net;

 namespace myprogram
 {
        class Program
        {
                static void Main(string[] args)
                {
                       try
                       {
                              WebBrowser browser1 = new WebBrowser();
                              browser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

                              browser1.Url = new Uri("https://isstracker.spaceflight.esa.int/");
                              browser1.Visible = true;
                              browser1.ScrollBarsEnabled = false;
                              browser1.ScriptErrorsSuppressed = true;
                              browser1.AllowNavigation = false;
                       }
                       catch (Exception e) 
                       {
                            Console.WriteLine(e.ToString());
                       }

                       Main(null); //don't know if this is the best way to keep the program from closing
                }
                static void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
                {
                     Console.WriteLine("iss page loaded");
                     if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
                         return;
                     string lat = (sender as WebBrowser).Document.GetElementById("isst_lat").InnerText;
                     string lon = (sender as WebBrowser).Document.GetElementById("isst_lon").InnerText;
                     Console.WriteLine("latitude is" + lat + ",longitude is" + lon);
        
                }
        }
 }

When I run the program, the WebBrowserDocumentCompletedEvent doesn't fire. I have looked for solutions, but have not found one. If anyone has a soultion, thank you in advance.

Dude, your program exits before it does, actually, fires "document loaded" event, or even, it throws a Stack Overflow exception (not rly... it's maximum call stack exceeded, i guess... can't remember it).

Now, answering your questions:

1st. See, your program is a Console App, instead of a Windows Forms App. check out each the difference between them ( https://docs.microsoft.com/pt-br/dotnet/csharp/tutorials/console-teleprompter and https://docs.microsoft.com/en-us/windows/uwp/launch-resume/app-lifecycle ) and notice they are way way different. So If you want to see the web browser it self, you should be using windows forms application. Instead, if you are just willing to parse and transform the Response of a Web Request (I guess thats the case here), google "WebClient.DownloadString Tutorial" and enjoy. ;)

2nd. This is not the best way to keep your program from closing... Since you've commented it ("//don't know if this is the best way to keep the program from closing") I'll point you to some clarifying directions...

Back in the day when we programmers used to drink soda in cork closed bottles, ride horses bareback, hunt our food and stuff, programs used to be called "command prompt"... is like git bash where you need to know the commands to use them in a "no user interface" environment...

yep! there was NO USER INTERFACE , you had to really type each command (like clicking a button), and pass the parameters (the stuff you put inside a textbox, or a checkbox checked state. for instance) in a single line to do anything.., It has been like forever, but the scars (lessons learned) are still with us!

One way to keep your console from closing (can't tell you the "best". sure can tell you the one I like best.) is to listen to commands.,, While the user inputs text lines. you check if this line is a valid command, and tells him if it's not. If the user inputs your valid "Exit" command. it shuts.:. (hint: the words "While" and "If" are actual programming patterns to be implemented)

So, this is a "guidelines answer", not the "programming done it self"... if wou need it done, just comment here and I can give it a try for you... Just an advice: It is better learning than being prisioner to other's help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM