cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.87k stars 2.92k forks source link

Using GetSourceAsync() WinForms #1151

Closed Xohn closed 9 years ago

Xohn commented 9 years ago

I tries to get html source via GetSourceAsync() after page load completed,but it failed to get anything.And if i try to load another uri ,it win't react if i have GetSourceAsync() in my code. I'm wondering what's wrong to my code?

cefsharp version:41.0.0 My code:

    private void OnBrowserNavStateChanged(object sender, NavStateChangedEventArgs args)
    {
        if (browser.IsLoading == false)
        {
            if( browser.Address.ToString() != "https://www.facebook.com/groups/closeout/")
            {
                Form2 frm = new Form2();
                frm.Show();
            }
            else
            {
                //var result = MessageBox.Show("", "",  MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                var task1 = browser.GetSourceAsync();
                task1.Wait();
                string WebContent = task1.Result;
                var result = MessageBox.Show("", "", MessageBoxButtons.YesNo,   MessageBoxIcon.Question); 
            }
      }
 }

For my test,I put two MessageBox ,one before GetSourceAsync() ,and one after GetSourceAsync() . The first MessageBox will show,and the second never show up.

amaitland commented 9 years ago

What exactly is Cef.Form? Do you mean WinForms?

If you try the CefSharp.WinForms.Example, there is Edit -> Copy Source To Clipboard (async), does that work for you?

https://github.com/cefsharp/CefSharp/blob/cefsharp/41/CefSharp.WinForms.Example/BrowserTabUserControl.cs#L178

Xohn commented 9 years ago

Yeah i means WinForms.

Try to Call Edit -> Copy Source To Clipboard (async) into my code. And i check my clopboard,nothing happened to it.

amaitland commented 9 years ago

Hmm, works fine for me.

Anyways, your original problem is likely one of threading. NavStateChanged is executed on CEF UI thread, and your also trying to Wait on code that will be executed on a CEF thread.

You can either

Xohn commented 9 years ago

Thanks a lot. I use Continewith and everything works fine. Really appreciate your help ,and wish my experience can help other people.

amaitland commented 9 years ago

Really appreciate your help ,and wish my experience can help other people.

No problem. Threading issues are pretty common, I've marked this as faq-able, in the hopes it's easier to find for others.

leavesgreen commented 8 years ago

@Xohn , @amaitland

Hi, buddy, I meet the same problem, and I tried the CefSharp.WinForms.Example, there is Edit -> Copy Source To Clipboard (async) , It works fine. in that kind, the code seems in the same thread of winform UI, but why that happend? I tried to click the button before the browser finished the page, and this time it didn't work ,and the whole application seemed died already. I want to say, in this example , the GetSourceAsync() code is in the same thread of winform UI? right? if I accept waiting for a while. like I saw the page finished and then click the button. how can I do that in the code?