0
     private void button1_Click_1(object sender, EventArgs e)
     {
         webBrowser1.Navigate("http://www.naver.com");
     }

     private void button2_Click_1(object sender, EventArgs e)
     {
         HtmlWindow wf = webBrowser1.Document.Window.Frames[0];
         string s = wf.Document.Body.OuterHtml;
         MessageBox.Show(s);
     }

.. this does work... but i need to click two buttons. i want to make thoes together. and i wrote below codes... the error comes out on second line(nullReferenceException). it seems that await doesn`t work can anybody help??

     private async void button2_Click_1(object sender, EventArgs e)
     {
         await Task.Run(() => webBrowser1.Navigate("http://www.naver.com"));
         HtmlWindow wf = webBrowser1.Document.Window.Frames[0];
         string s = wf.Document.Body.OuterHtml;
         MessageBox.Show(s);
     }
4
  • Can you put a breakpoint on line #2 and tell us what exactly is null here? Is it webBrowser1 or one of its members? Commented Aug 10, 2015 at 13:40
  • 2
    I'm assuming that webBrowser1 is a WebBrowser control? If so, then you don't need to worry about async/await, just attach to the DocumentCompleted event? Commented Aug 10, 2015 at 13:41
  • 1
    are you saying webBrowser1.Navigate("http://www.naver.com"); works but await Task.Run(() => webBrowser1.Navigate("http://www.naver.com")); doesn't? Where does the magic variable webBrowser1 come from? Commented Aug 10, 2015 at 13:42
  • 2
    I'm guessing webBrowser1.Navigate returns before the page is fully loaded. You'll have to wait until the IsBusy property returns false to show that the page has been loaded, or use the DocumentCompleted event (as DavidG said). Commented Aug 10, 2015 at 13:43

2 Answers 2

1

Navigate starts the downloading. It returns before the downloading is completed. You'll have to wait for event WebBrowser.DocumentCompleted

By the way, because Navigate does not wait until loaded it is fast, there is no need to call it asynchronously, otherwise Microsoft would have made an async version like in the WebClient task.

private void button2_Click_1(object sender, EventArgs e)
{
    webBrowser1.Navigate("http://www.naver.com");
}

private void OnDocumentCompleted(object sender, ...)
{
    HtmlWindow wf = webBrowser1.Document.Window.Frames[0];
    string s = wf.Document.Body.OuterHtml;
    MessageBox.Show(s);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want the button2 to perform automatically on button1 event, then you can use the Button.PerformClick Method

So, your code goes this way:

 private void button1_Click_1(object sender, EventArgs e)
 {
     webBrowser1.Navigate("http://www.naver.com");
     button2.PerformClick();
 }

 private void button2_Click_1(object sender, EventArgs e)
 {
     HtmlWindow wf = webBrowser1.Document.Window.Frames[0];
     string s = wf.Document.Body.OuterHtml;
     MessageBox.Show(s);
 }

If you want to make work together, then you can set those button click events on Form Load using the Load event

public Form1()
    {
        InitializeComponent();
        Load += button1_Click;
        Load += button2_Click;;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.