编程崩溃原因未知

时间:2011-08-19 13:14:08

标签: c# threadpool

所以我有一个用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);

    }
}

2 个答案:

答案 0 :(得分: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);
    }

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

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

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

答案 1 :(得分:0)

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

相关问题