簡體   English   中英

C#List.Add System.InvalidOperationException

[英]C# List.Add System.InvalidOperationException

我正在處理來自其父窗體的子窗體中的事件,當我嘗試從處理程序的事件args中包含的列表中添加項目時(在下面的代碼中為ScraperForm_SiteScraped),我在控制台中收到異常System.InvalidOperationException 。

有趣的是,它似乎在第一次添加時成功,但是沒有后續嘗試。

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);
        }
    }
}

子表格

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));
        }
    }
}

從我們的評論中可以看出,這是一個線程問題。 作為一種好習慣,當代碼塊中可能出現異常時,請始終使用try / catch塊。 :)

另外,如果您使用的是Visual Studio,則可以通過按CTRL + ALT + E並選中復選框來使VS中斷更多的異常。 您可以在這里閱讀更多有關異常中斷的信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM