2011-08-19 80 views
0

所以我有一个用C#编写的基本应用程序。它基本上写了一个库存文件。它会在创建文件时停下来。我对这里发生的事情感到困惑,因为如果我在IDE中运行它,它将停止工作。该文件在文件的不同停止处停止,因此它不是一个单独的事件。如果使用不同的话,我正在使用线程池。我有一个循环遍历文件并读取文件并提示新线程。如果没有错误,就很难调试某些东西。编程崩溃原因未知

static void Main(string[] args) 
    { 
     //string asins; 
     Readfile r = new Readfile(); 
     r.read(); 

     Console.WriteLine("Press any key to exit."); 
     System.Console.ReadKey(); 

     //Thread.Sleep(60000); 

     //createoutward c = new createoutward(); 
     // c.read(); 

     //p.print(s.scrap(r.read())); 

    } 

我的方法使得线程

public string[] read() 
    { 
     ThreadPool.SetMaxThreads(10, 100); 
     string[] asins; 

     string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Joe T\Desktop\AmazonAsins.csv"); 

     using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"\\WIN-UWPWZ7Z3RKX\wwwroot\repricer\WriteLines2a.txt")) 
      file.WriteLine("Product-ID\tPrice1\tPrice2\tRank\tAFN\t" + DateTime.Now); 
     prices = new string[lines.Length, 2]; 
     int i = 0; 
     asins = new string[lines.Length]; 

     foreach (string line in lines) 
     { 
      scraping s = new scraping(); 


      char[] tabs = { '\t' }; 
      string asin; 
      string[] words = line.Split(tabs); 
      asin = words[1]; 
      asins[i] = asin; 

      Thread.Sleep(1000); 
      ThreadPool.QueueUserWorkItem(new WaitCallback(s.scraping1), asin); 

      ++i; 


     } 


     return asins; 
    } 

刮痧类

 public void scraping1(object a) 
    { 
     string AFN = "N"; 

     string asin = (string)a; 

     double price, price2; 
     string sprice; 
     string context; 
     string page = "*****" + asin; 
     try 
     { 
      WebZinc WebZincProduct = new WebZinc(); 
      WebZincProduct.OpenPage(page); 

      context = WebZincProduct.CurrentPage.Text; 
     } 
     catch 
     { 
      scraping1(a); 
      return; 
     } 

     Regex regex11 = new Regex("****\r\n((.|\n)*?)****", 
      RegexOptions.IgnoreCase); 
     Match oP1 = regex11.Match(context); 

     if (oP1.Value.Contains("*******")) 
     { 
      AFN = "Y"; 
     } 
     Regex reg = new Regex(@"[0-9]+\.[0-9]+"); 
     MatchCollection mc = reg.Matches(oP1.Value); 


     double cost = 0.0; 
     double cost2 = 0.0; 
     double shipping2 = 0.0; 
     double shipping = 0.0; 
     int j = 0; 
     int j3 = 0; 

     foreach (Match m in mc) 
     { 
      if (j == 0) cost = Convert.ToDouble(m.Value); 
      if (j == 1) shipping = Convert.ToDouble(m.Value); 
      Console.WriteLine("{0}", m.Value); 
      ++j; 
     } 


     Regex regex4 = new Regex("****\r\n\r\n((.|\n)*?)****", 
      RegexOptions.IgnoreCase); 
     Match oP4 = regex4.Match(context); 

     MatchCollection mc4 = reg.Matches(oP4.Value); 

     foreach (Match m in mc4) 
     { 
      if (j3 == 0) cost2 = Convert.ToDouble(m.Value); 
      if (j3 == 1) shipping2 = Convert.ToDouble(m.Value); 
      Console.WriteLine("{0}", m.Value); 
      ++j3; 
     } 



     price2 = cost2 + shipping2; 
     price = cost + shipping; 
     if (price == 0.0 && i != 5) 
     { 
      scraping1(a); 
     } 



     string rank = rankAFN(asin); 
     lock (Program._locker) 
     { 

       using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"\\WIN-UWPWZ7Z3RKX\wwwroot\repricer\WriteLines2a.txt", true)) 
        file.WriteLine(asin + "\t" + price + "\t" + price2 + "\t" + rank + "\t" + AFN); 

    } 
} 
+6

当我们看不到您的代码时,也很难知道发生了什么;) – slandau

+0

@slandau添加了代码 –

+0

当您说你在IDE中运行它时,是否使用“开始调试”[F5]或“开始不调试“[Ctrl] + [F5] –

回答

1

这是我最好的猜测,基于这样一个事实,即您在这里有很多额外的代码,我们无法在不看到整个代码的情况下解释这些代码(这就是为什么最好发布最简单的示例来重新创建问题为Jon Skeet在他的博客文章Writing the Perfect S.O. Question中如此精彩地表达)。

但这是我的猜测。我猜测,并感觉很强烈,你有这里失控递归。您的方法scrapping1()对异常和某些未从参数解释的条件进行递归调用。

因为这些递归调用取决于局部变量或动作而不是参数,所以它使得很难安全地控制递归会做什么,而且在这种情况下你可能不应该做它们。

catch 
    { 
     // recursion here passing the SAME arg, what is to stop this? 
     scraping1(a); 
     return; 
    } 

而且

// WHERE does this 'i' come from? I don't see where it's incrementing! 
    // possible unsafe recursion... 
    if (price == 0.0 && i != 5) 
    { 
     // recursion here passing the SAME arg, no stop condition? 
     scraping1(a); 
    } 

你可以做的是围绕你的scraping1()方法的身体有一个try/catch这样你至少可以看到屏幕上的异常,并知道什么线的另一件事它发生在。

public void scraping1(object a) 
{ 
    try 
    { 
      // your method logic 
    } 
    catch (Exception ex) 
    { 
      Console.WriteLine(ex); 
      Console.Out.Flush(); 
    } 
} 

如果是递归,但是,导致StackOverflowException,CLR将终止您的进程。在这种情况下,注释掉这些递归调用并思考你真正想做的事情。我不认为你真的想在这种情况下做递归。你只是想“重试?”

+0

在删除了递归,并添加了一些尝试/捕获的代码行我想重试,而不是一遍又一遍地调用一个方法。 –

0

,在一个任务排队到线程池会导致应用程序关闭时发生的任何未处理的异常。将你的scraping1方法的全部内容放在try/catch块中,在catch的顶部设置一个断点,并且至少应该能够看到抛出的异常。