C# 程序崩溃原因未知
因此,我有一个用C#编写的基本应用程序。它基本上是写一个库存文件。它将在创建文件的中途停止。我真的很困惑这里发生了什么,因为如果我在IDE中运行它,它将停止工作。文件在文件中的不同停止点停止,因此它不是单一事件。我正在使用一个线程池,如果这使一个不同的。我有一个循环,它遍历一个文件,读取该文件并提示一个新线程。如果没有错误,调试某些东西真的很困难C# 程序崩溃原因未知,c#,threadpool,C#,Threadpool,因此,我有一个用C#编写的基本应用程序。它基本上是写一个库存文件。它将在创建文件的中途停止。我真的很困惑这里发生了什么,因为如果我在IDE中运行它,它将停止工作。文件在文件中的不同停止点停止,因此它不是单一事件。我正在使用一个线程池,如果这使一个不同的。我有一个循环,它遍历一个文件,读取该文件并提示一个新线程。如果没有错误,调试某些东西真的很困难 static void Main(string[] args) { //string asins; Rea
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);
}
}
在线程池队列中的任务中发生的任何未经处理的异常都将导致应用程序关闭。将
scraping1
方法的整个主体放在一个try/catch块中,在catch的顶部设置一个断点,您至少应该能够看到抛出了什么异常。以下是我的最佳猜测,因为这里有很多额外的代码,如果不看到整个代码,我们就无法解释这些代码(这就是为什么最好发布一个最简单的例子来重现Jon Skeet在其博客文章中精彩阐述的问题)
但这是我的猜测。我猜测,而且感觉非常强烈,这里有失控的递归。您的方法scrasting1()
对异常和某些条件进行递归调用,这些异常和条件不是从参数解释的
因为这些递归调用依赖于局部变量或操作,而不是参数,所以很难安全地控制递归将执行的操作,在这种情况下,您可能不应该执行这些操作
catch
{
// recursion here passing the SAME arg, what is to stop this?
scraping1(a);
return;
}
及
您可以做的另一件事是在scraping1()方法的主体周围使用try/catch,这样您至少可以在屏幕上看到异常,并知道它发生在哪一行
public void scraping1(object a)
{
try
{
// your method logic
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.Out.Flush();
}
}
但是,如果是递归导致StackOverflowException,CLR将终止您的进程。在这种情况下,请注释掉这些递归调用,并思考您真正想要做的事情。我认为在这种情况下,您并不是真的想要执行递归。您只是在尝试“重试”吗当我们看不到你的代码时,也很难知道发生了什么;)当你说你在IDE中运行它时,你是使用“开始调试”[F5]还是“开始不调试”[Ctrl]+[F5]什么是“刮片”类?@James只是播放按钮,但它在生产中崩溃了too@Joe-你确定是撞车吗?会不会只是过早地结束了节目?是否有堆栈跟踪或弹出的“程序异常结束”对话框?在中,删除了递归,并在我想重试的代码行上添加了几个try/catch,而不是反复调用方法。
public void scraping1(object a)
{
try
{
// your method logic
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.Out.Flush();
}
}