在使用C#,Excel和Selenium时知道如何/何时处理内存pipe理?

我最近一直在使用C#和Selenium来创build循环,将数据导入到网站的search参数中,这样可以将结果粘贴到Excel表格中(全部在Visual Studio中)。 我注意到,随着我越来越多的search(如n ++;),程序的速度稳步下降,甚至对于我在工作表顶部的进度条的帧速下降,这导致我相信程序的速度越来越慢。 在程序运行的每个小时后,我也一直在收集结果,似乎我的程序完成的search次数随着时间的推移而下降。

这是由于垃圾收集器没有按照我的预期工作,由于缺乏知识? 我应该修改内存pipe理吗? 这个循环是否足以担心内存pipe理? 我知道垃圾收集器应该为我分配空间,我也假设它也删除不再使用的对象。 我对内存pipe理很less有任何经验,所以我只是在寻求澄清。 如果这是我应该处理的事情,是否有人能指出我正确的方向,并帮助我学习如何做?

这里是我的脚本供参考(我在Excel中通过约16,000行来完成这个特定的脚本)。

编辑:额外的问题:这是否与我正在使用像一个Try-Catch一样的内存重调用的事实?

public partial class Sheet1 { private void Sheet1_Startup(object sender, System.EventArgs e) { } private void Sheet1_Shutdown(object sender, System.EventArgs e) { } #region VSTO Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.button1.Click += new System.EventHandler(this.button1_Click); this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork); this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged_1); this.Startup += new System.EventHandler(this.Sheet1_Startup); this.Shutdown += new System.EventHandler(this.Sheet1_Shutdown); } #endregion private void button1_Click(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); var CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } //Globals -- for DOBFormatter(); string day = ""; string month = ""; string year = ""; int n = 0; int l = 0; string errorfix = ""; int red = 0; int green = 0; int r = 6; int total = 0; private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { object misValue = System.Reflection.Missing.Value; var Excel = Globals.ThisWorkbook.Application; var activesheet = Excel.ActiveSheet; string z = textBox1.Text.ToString(); n = Convert.ToInt32(z); String ActiveCell = Excel.ActiveSheet.Range["A" + n, misValue].Text; IWebDriver driver = new FirefoxDriver(); if (r == 6) { while (activesheet.Range["A" + r].Text.ToString().Length > 0) { l++; r++; activesheet.Range["E" + 4].Value = "Counting Number of Searches: " + r; } } total = red + green; activesheet.Range["E" + 1].Value = "Successful Searches: " + green; activesheet.Range["E" + 2].Value = "Failed Searches: " + red; activesheet.Range["E" + 3].Value = "Total Searches: " + total; activesheet.Range["E" + 4].Value = "Searches Remaining: " + r; progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Maximum = l; }); //Login to Homepage driver.Navigate().GoToUrl("url"); driver.FindElement(By.Name("login-send")).Click(); driver.FindElement(By.Name("EMAIL")).SendKeys("####"); driver.FindElement(By.Name("PASSWORD")).SendKeys("####"); driver.FindElement(By.Id("submitButton")).Click(); Thread.Sleep(TimeSpan.FromSeconds(10)); driver.SwitchTo().Frame("homeFrame"); driver.FindElement(By.XPath("html/body/div[2]/div[3]/div[1]/h3/a[1]")).Click(); driver.FindElement(By.XPath("html/body/div[2]/div[3]/section/ul/li[2]/a/img")).Click(); driver.SwitchTo().Window(driver.WindowHandles[1]); driver.FindElement(By.XPath("html/body/table[2]/tbody/tr[4]/td[3]/input")).Click(); driver.Navigate().GoToUrl("url"); while (activesheet.Range["A" + n].Text.ToString().Length > 0) { //Update %, progress bar, and details. progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = progressBar1.Value + 1; }); //Variables string DOS = activesheet.Range["B" + n].Text; string PolicyNum = activesheet.Range["C" + n].Text; string DOB = activesheet.Range["D" + n].Text; string TaxIdOwner = activesheet.Range["A" + n].Text; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Aqua); //To get the textboxes in the correct thread that it was created on. textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = "Currently on row: " + n; }); textBox2.Invoke((MethodInvoker)delegate { textBox2.Text = "Successful searches: " + green; }); textBox3.Invoke((MethodInvoker)delegate { textBox3.Text = "Failed Searches: " + red; }); textBox4.Invoke((MethodInvoker)delegate { textBox4.Text = "Total Count: " + total; }); textBox5.Invoke((MethodInvoker)delegate { textBox5.Text = "Searches Left: " + r; }); activesheet.Range["E" + 1].Value = textBox2.Text; activesheet.Range["E" + 2].Value = textBox3.Text; activesheet.Range["E" + 3].Value = textBox4.Text; activesheet.Range["E" + 4].Value = textBox5.Text; //Select the Correct Search (Policy# + DOB) driver.FindElement(By.XPath("//tr[2]/td[1]/div/div/table[2]/tbody/tr/td/form[2]/table/tbody/tr[2]/td[2]/input")).Click(); DOBFormatter(DOB); //Making a selectors, because it makes the code more simple. IWebElement element = driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[7]/td[2]/select")); SelectElement selector = new SelectElement(element); if (TaxIdOwner == "Company1" || TaxIdOwner == "Company2") { selector.SelectByText("Option1"); Thread.Sleep(2000); driver.FindElement(By.XPath("//tr[9]/td[2]/select")).Click(); driver.FindElement(By.XPath("//tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.Enter); } else if (TaxIdOwner == "Company3" || TaxIdOwner == "Company 4") { selector.SelectByText("Option2"); Thread.Sleep(2000); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).Click(); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.Enter); } else if (TaxIdOwner == "Company5") { selector.SelectByText("Option3"); Thread.Sleep(2000); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).Click(); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.Enter); } else if (TaxIdOwner == "Company6") { selector.SelectByText("Option4"); Thread.Sleep(2000); driver.FindElement(By.XPath("//tbody/tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).Click(); driver.FindElement(By.XPath("//tbody/tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tbody/tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tbody/tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.ArrowDown); driver.FindElement(By.XPath("//tbody/tr/td/form[2]/table/tbody/tr[9]/td[2]/select")).SendKeys(OpenQA.Selenium.Keys.Enter); } Thread.Sleep(3000); driver.FindElement(By.Name("checkDate")).Clear(); driver.FindElement(By.Name("checkDate")).SendKeys(DOS); driver.FindElement(By.Name("endDate")).Clear(); driver.FindElement(By.Name("endDate")).SendKeys(DOS); driver.FindElement(By.Name("enrolleeNo")).Clear(); driver.FindElement(By.Name("enrolleeNo")).SendKeys(PolicyNum); driver.FindElement(By.Name("dobMM")).Clear(); driver.FindElement(By.Name("dobMM")).SendKeys(month); driver.FindElement(By.Name("dobDD")).Clear(); driver.FindElement(By.Name("dobDD")).SendKeys(day); driver.FindElement(By.Name("dobYYYY")).Clear(); driver.FindElement(By.Name("dobYYYY")).SendKeys(year); driver.FindElement(By.Name("Submit")).Click(); if (driver.FindElements(By.XPath("//tr[2]/td[1]/div/div/table[2]/tbody/tr/td/form[2]/table/tbody/tr[1]/td/b/font")).Count > 0) { activesheet.Range["E" + n].Value = errorfix; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); red++; } else if (driver.FindElements(By.XPath("//tr/td/form[2]/table/tbody/tr[1]/td/font/b")).Count > 0) { errorfix = driver.FindElement(By.XPath("//tr/td/form[2]/table/tbody/tr[1]/td/font/b")).Text; activesheet.Range["E" + n].Value = errorfix; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); red++; } else if (driver.FindElements(By.XPath("//table/tbody/tr[1]/td/b[font[contains(text(), 'No claims found.')]]")).Count > 0) { activesheet.Range["E" + n].Value = "No claims found."; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); red++; } else if (driver.FindElements(By.XPath("//tr[3]/td/div[2]/table/tbody/tr[1]/td[9]")).Count > 0) { if (driver.FindElements(By.XPath("//tr[3]/td/div[2]/table/tbody/tr[1]/td[9][contains(text(), 'Denied')]")).Count > 0) { activesheet.Range["F" + n].Value = driver.FindElement(By.XPath("//tr[3]/td/div[2]/table/tbody/tr[1]/td[9][contains(text(), 'Denied')]")).Text; driver.FindElement(By.XPath("//tbody/tr/td[4]/div/table[3]/tbody/tr[3]/td/div[2]/table/tbody/tr[1]/td[12]/a")).Click(); activesheet.Range["G" + n].Value = driver.FindElement(By.XPath("//tbody/tr[1]/td[1]/table[1]/tbody/tr[4]/td/table/tbody/tr/td/span/span")).Text; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange); red++; } else { activesheet.Range["F" + n].Value = "Approved"; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightGreen); green++; } } else { activesheet.Range["E" + n].Value = "No claims were found"; activesheet.Range["A" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["B" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["C" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["D" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["E" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["F" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); activesheet.Range["G" + n].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightPink); red++; } total++; r--; driver.Navigate().GoToUrl("url"); driver.Navigate().GoToUrl("url"); n++; } } catch { progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = progressBar1.Value - 1; }); n--; } } private void DOBFormatter(string date) { int count = 0; int position = 0; while (count < 3) { //First go around, grab the first 2 numbers -- the month. if (count == 0) { month = date.Substring(position, 2); } //Second time, grab the day. else if (count == 1) { day = date.Substring(position, 2); } //Last go through grabs the year. else if (count == 2) { year = date.Substring(position, 4); } position = position + 3; count++; } } 

尝试抓住这里不是很多的原因。 在你的代码上花费很多的东西之一就是调用外部对象(excel和FireFox)的次数。我会好奇的在你的代码上运行一个分析器,看看它在哪里变得越来越慢。 这可能是因为你怀疑应用程序的内存没有被释放(调用/ COM对象可以是它的源头),或者只是简单地调用对于不明原因的重要的Excel / Firefox的调用。

PS:这不是一个真正的答案,但更多的评论。 希望它仍然可以帮助你