C#列表。添加System.InvalidOperationException

C#列表。添加System.InvalidOperationException,c#,C#,我正在处理父窗体中的子窗体中的事件,当我尝试从处理程序的事件参数中包含的列表中添加项(以下代码中为ScraperForm_SiteScraped)时,我在控制台中收到异常System.InvalidoOperationException 有趣的是,它在第一次添加时似乎成功了,但没有后续尝试 public partial class ProxyTesterView : UserControl { private BindingList<Proxy> proxies = new

我正在处理父窗体中的子窗体中的事件,当我尝试从处理程序的事件参数中包含的列表中添加项(以下代码中为ScraperForm_SiteScraped)时,我在控制台中收到异常System.InvalidoOperationException

有趣的是,它在第一次添加时似乎成功了,但没有后续尝试

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);
        }
    }
}
公共部分类ProxyTesterView:UserControl
{
私有BindingList代理=新建BindingList();
private BindingList pudges=new BindingList();
私有BindingList测试=新建BindingList();
专用PauseOrCancelTokenSource pcts=新的PauseOrCancelTokenSource();
私有ProxyScraperForm scraperForm=新ProxyScraperForm();
公共ProxyTesterView()
{
初始化组件();
proxies.ListChanged+=proxies\u ListChanged;
scraperForm.SiteScraped+=scraperForm_SiteScraped;
}
私有无效代理\u ListChanged(对象发送方,ListChangedEventArgs e)
{
ProxiesDataGridView.RowCount=代理.Count;
}
私有void AddFromScraperToolStripMenuItem\u单击(对象发送方,事件参数e)
{
scraperForm.Show();
}
私有void ScraperForm_SiteScraped(对象发送方,SiteScrapedEventArgs e)
{
foreach(e.DrapedProxies中的var代理)
{
代理。添加(代理);
}
}
}
子表单

public分部类代理scraperform:Form
{
私有BindingList站点=新建BindingList();
公共整数间隔{get;set;}=60000;
公共事件事件处理程序站点已删除;
公共ProxyScraperForm()
{
初始化组件();
添加(新的ProxyScraperSiteUsProxyOrg());
Add(新的proxysrapersitefreeproxylistnet());
添加(新的ProxyScrapersiteFreeProxySnet());
Add(新的ProxyScraperSiteHideMyName());
添加(新的ProxyScraperSiteHidester());
ScraperDataGridView.DataSource=站点;
}
私有无效按钮\u单击(对象发送者,事件参数e)
{
foreach(站点中的var站点)
{
Task.Run(异步()=>
{
while(true)
{
var driver=SeleniumUtility.CreateDefaultFirefoxDriver();
var newProxies=wait site.scrapasync(驱动程序);
driver.Quit();
现场清理(新代理);
等待任务。延迟(5000);
Status=$“等待{ScrapeInterval/1000}秒…”;
等待任务。延迟(间隔);
}
});
}
}
站点上的私有无效已清除(列出已清除的副本)
{
如果(SiteScraped!=null)
{
SiteScraped(此,新SiteScrapedDeventargs(scrapedProxies));
}
}
}

从我们的评论来看,这是一个线程问题。作为一种良好的做法,当代码块中可能出现异常时,请始终使用try/catch块。:)


另外,如果您使用的是VisualStudio,则可以通过按CTRL+ALT+E并选中复选框,在出现更多异常时启用VS break。您可以阅读有关异常中断的更多信息。

错误消息是什么?是否有伴随异常的消息?嗯。。。能否添加一个try/catch并在catch中放置一个断点,以便更详细地检查异常?我们需要知道异常的类型及其引发的确切行。Stacktrace也可能有帮助,也可能没有帮助。消息本身通常是异常中最不重要的部分。ToString()调用的完整结果非常有用。正如你在我写完这篇文章之前学到的:)@折衷主义者谢谢!把foreach放在BeginInvoke通话中起作用了。我不知道为什么我的IDE没有中断,这会告诉我到底发生了什么,我会在这里搜索解决这个问题的方法。
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));
        }
    }
}