C#:同步运行多个请求

时间:2017-08-24 21:22:59

标签: c# .net multithreading winforms

我正在尝试让多个浏览器同时运行任务。我创建了一种测试应用程序,看看我是否可以将它作为一项任务给自己。我做得很好,但似乎它放慢了很多,等待第一个浏览器在第二个加载页面之前完成。

在任何人建议使用除WebBrowser以外的其他人之前,我只想说我选择WebBrowser的决定有充分的理由。

我有一个名为" Slave"要向Google发送请求,我使用下面显示的代码创建它们:

public void LoadSlaves()
{
    for (int i = Program.GetServer().GetConfigHandler().GetValueByKeyInt("slaves_count"); i > 0; i--)
    {
        _slaves.Add(_slaves.Count, new Slave());
    }

    Logger.Warn("Loaded " + _slaves.Count + " slaves.");
}

public void StartSlaves()
{
    foreach (var slave in _slaves.Values)
    {
        slave.Start();
    }
}

类别:

using NLog;
using System;
using System.Threading;
using System.Windows.Forms;

namespace Test.Root.Base.Slaves
{
    class Slave
    {
        private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

        private Thread _slaveThread;
        private WebBrowser _browser;

        public Slave()
        {
        }

        public void Start()
        {
            _slaveThread = new Thread(new ThreadStart(OnCycle));
            _slaveThread.SetApartmentState(ApartmentState.STA);
            _slaveThread.Start();
        }

        public void OnCycle()
        {
            while (true)
            {
                if (_browser == null)
                {
                    _browser = new WebBrowser();
                    _browser.ScriptErrorsSuppressed = true;
                    _browser.DocumentCompleted += browser_DocumentCompleted;
                    _browser.Navigate("http://google.com");

                    Application.Run();
                }
            }

        }

        private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (_browser.Url == e.Url)
            {
                Logger.Trace("Navigated to {0} on " + Thread.CurrentThread.ManagedThreadId, e.Url);
                _browser.Navigate("http://google.com");
            }
        }
    }
}

以下是一些记录:

  22:17:56 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=f0KfWZ29L8HHXo_vm_gI&gws_rd=ssl on 7
  22:17:58 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=gkKfWdzoAcjHXpyEl2g&gws_rd=ssl on 8
  22:18:01 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=g0KfWaffJMjHXpyEl2g&gws_rd=ssl on 7
  22:18:02 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=hUKfWd33KMjHXpyEl2g&gws_rd=ssl on 8
  22:18:06 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=iUKfWf6xE8jHXpyEl2g&gws_rd=ssl on 8
  22:18:07 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=iEKfWfPPFMjHXpyEl2g&gws_rd=ssl on 7
  22:18:10 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=jUKfWcHGLMjHXpyEl2g&gws_rd=ssl on 7
  22:18:12 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=jUKfWdbLB8jHXpyEl2g&gws_rd=ssl on 8
  22:18:15 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=kEKfWezHPMjHXpyEl2g&gws_rd=ssl on 7
  22:18:16 - Navigated to https://www.google.co.uk/?gfe_rd=cr&ei=kkKfWe35OcjHXpyEl2g&gws_rd=ssl on 8

正如你可以看到它在请求之间等待几秒钟(通常是2-3秒),我怎样才能让两个浏览器同步运行?

1 个答案:

答案 0 :(得分:0)

你是否会因为线程开始永不关闭而减速。看起来你不再需要它,但它还在循环中。我之前没有使用过WebBrowser,但是,除非有充分的理由,否则你所做的任何线程都应该让它优雅地完成。 这样的事情对你有用吗?

    ...

    public void OnCycle()
    {
        while (_browser == null)
        {

    ...

另外只是为了澄清一下,只有很多_slaves在运行才会变慢?