简体   繁体   English

C#List.Add System.InvalidOperationException

[英]C# List.Add System.InvalidOperationException

I am handling an event from a child form in its parent form, and when I try adding items from the list contained within the event args of the handler (ScraperForm_SiteScraped in the code below), I am receiving the exception System.InvalidOperationException in my console. 我正在处理来自其父窗体的子窗体中的事件,当我尝试从处理程序的事件args中包含的列表中添加项目时(在下面的代码中为ScraperForm_SiteScraped),我在控制台中收到异常System.InvalidOperationException 。

Interestingly enough, it seems to succeed on the first add, but no subsequent attempts. 有趣的是,它似乎在第一次添加时成功,但是没有后续尝试。

public partial class ProxyTesterView : UserControl
{

    private BindingList<Proxy> proxies = new BindingList<Proxy>();
    private BindingList<ProxyJudge> pudges = new BindingList<ProxyJudge>();
    private BindingList<ProxyTest> tests = new BindingList<ProxyTest>();
    private PauseOrCancelTokenSource pcts = new PauseOrCancelTokenSource();
    private ProxyScraperForm scraperForm = new ProxyScraperForm();

    public ProxyTesterView()
    {
        InitializeComponent();

        proxies.ListChanged += Proxies_ListChanged;
        scraperForm.SiteScraped += ScraperForm_SiteScraped;
    }

    private void Proxies_ListChanged(object sender, ListChangedEventArgs e)
    {
        ProxiesDataGridView.RowCount = proxies.Count;
    }

    private void AddFromScraperToolStripMenuItem_Click(object sender, EventArgs e)
    {
        scraperForm.Show();
    }

    private void ScraperForm_SiteScraped(object sender, SiteScrapedEventArgs e)
    {
        foreach (var proxy in e.ScrapedProxies)
        {
            proxies.Add(proxy);
        }
    }
}

Child Form 子表格

public partial class ProxyScraperForm : Form
{

    private BindingList<IProxyScraperSite> sites = new BindingList<IProxyScraperSite>();

    public int ScrapeInterval { get; set; } = 60000;

    public event EventHandler<SiteScrapedEventArgs> SiteScraped;

    public ProxyScraperForm()
    {
        InitializeComponent();

        sites.Add(new ProxyScraperSiteUsProxyOrg());
        sites.Add(new ProxyScraperSiteFreeProxyListNet());
        sites.Add(new ProxyScraperSiteFreeProxyListsNet());
        sites.Add(new ProxyScraperSiteHideMyName());
        sites.Add(new ProxyScraperSiteHidester());
        ScraperDataGridView.DataSource = sites;
    }

    private void ScrapeButton_Click(object sender, EventArgs e)
    {
        foreach (var site in sites)
        {
            Task.Run(async () =>
            {
                while (true)
                {
                    var driver = SeleniumUtility.CreateDefaultFirefoxDriver();
                    var newProxies = await site.ScrapeAsync(driver);
                    driver.Quit();
                    OnSiteScraped(newProxies);
                    await Task.Delay(5000);
                    site.Status = $"Waiting {ScrapeInterval / 1000} seconds...";
                    await Task.Delay(ScrapeInterval);
                }
            });
        }
    }

    private void OnSiteScraped(List<Proxy> scrapedProxies)
    {
        if (SiteScraped != null)
        {
            SiteScraped(this, new SiteScrapedEventArgs(scrapedProxies));
        }
    }
}

From our comments, turns out that this was a threading issue. 从我们的评论中可以看出,这是一个线程问题。 As a good practice, always use a try/catch block when there's a chance that an exception can occur in a block of code. 作为一种好习惯,当代码块中可能出现异常时,请始终使用try / catch块。 :) :)

Also, if you're using Visual Studio, you can make VS break on more exceptions by pressing CTRL+ALT+E and selecting the checkboxes. 另外,如果您使用的是Visual Studio,则可以通过按CTRL + ALT + E并选中复选框来使VS中断更多的异常。 You can read more about exception breaking here . 您可以在这里阅读更多有关异常中断的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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