简体   繁体   中英

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.

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. :)

Also, if you're using Visual Studio, you can make VS break on more exceptions by pressing CTRL+ALT+E and selecting the checkboxes. You can read more about exception breaking here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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