How Can Read Web Page Using WebBrowser control

血红的双手。 提交于 2019-11-27 15:51:14
noseratio

From the comment:

.. but async or await is not supported i think iam using vs2010 and i already installed Nuget but still iam finding async keyword, please help

If you can't use async/await, then you can't use for loop for asynchronous WebBrowser navigation, unless resorting to deprecated hacks with DoEvents. Use the state pattern, that's what C# 5.0 compiler generates behind the scene for async/await.

Alternatively, if you're adventurous enough, you can simulate async/await with yield, as described here.

Updated, below is another way of exploiting the C# enumerator state machine (compatible with C# 2.0 and later):

using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsForms_22296644
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        IEnumerable GetNavigator(string[] urls, MethodInvoker next)
        {
            WebBrowserDocumentCompletedEventHandler handler =
                delegate { next(); };

            this.webBrowser.DocumentCompleted += handler;
            try
            {
                foreach (var url in urls)
                {
                    this.webBrowser.Navigate(url);
                    yield return Type.Missing;
                    MessageBox.Show(this.webBrowser.Document.Body.OuterHtml);
                }
            }
            finally
            {
                this.webBrowser.DocumentCompleted -= handler;
            }
        }

        void StartNavigation(string[] urls)
        {
            IEnumerator enumerator = null;
            MethodInvoker next = delegate { enumerator.MoveNext(); };
            enumerator = GetNavigator(urls, next).GetEnumerator();
            next();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            StartNavigation(new[] { 
                "http://example.com",
                "http://example.net",
                "http://example.org" });
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!