编程崩溃原因未知
所以我有一个用 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);
}
}
So I have a basic application written in C#. It basically writes a file of inventory. It will just stop half way through creating the file. The I am really confused on what is going on here because if I run it in the IDE it will just stop working. The file is stopped at different stops in the file so it is not a singular event. I am using a threadpool if that makes a different. I have a loop that goes through a file and reads the file and cues a new thread. It is just really hard to debug something if there is no error.
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()));
}
My method making the thread
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;
}
Scraping Class
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的最佳猜测,基于这样的事实:这里有很多额外的代码,如果没有看到整个代码,我们就不可能解释这些代码(这就是为什么最好发布最小的示例来重现问题,如 Jon Skeet 如此精彩)在他的博客文章 写出完美的问题)。
但这是我的猜测。我猜测,并且感觉非常强烈,你在这里有失控的递归。您的方法
scrapping1()
在出现异常和未从参数解释的某些条件时对其自身进行递归调用。因为这些递归调用取决于局部变量或操作而不是参数,所以很难安全地控制递归将执行的操作,并且在这种情况下您可能不应该进行递归调用。
你可以做的另一
件事是用 try/catch 包围 scraping1() 方法的主体,这样你至少可以在屏幕上看到异常并知道它发生在哪一行。
但是,如果它是递归,则会导致 StackOverflowException ,CLR 将终止您的进程。在这种情况下,请注释掉那些递归调用并考虑您真正想要做什么。我认为在这种情况下你真的不想进行递归。您只是想“重试”吗?
Here's my best guess based on the fact that you have a LOT of extra code here that we can't possibly interpret without seeing the whole code (which is why it's best to post the most minimal example that recreates the issue as Jon Skeet so wonderfully articulated in his blog post Writing the Perfect S.O. Question).
But here's my guess. I'm guessing, and feeling pretty strongly, that you have runaway recursion here. Your method
scrapping1()
makes recursive calls to itself on exceptions and certain conditions that are not interpreted from the parameters.Because these recursive calls are depending on local variables or actions and not a parameter, it makes it VERY hard to safely control what recursion will do and you should probably not be making them in this case.
And
Another thing you can do would be to surround your scraping1() method's body with a try/catch so that you can at least see the exception on screen and know what line its happening in.
If it is recursion, though, causing a StackOverflowException, the CLR will terminate your process. In that case comment out those recursive calls and think of what you're really trying to do. I don't think you really want to do recursion in this case. Are you just trying to "retry?"
排队到线程池的任务中发生的任何未处理的异常都将导致您的应用程序关闭。将
scraping1
方法的整个主体放入 try/catch 块中,在 catch 的顶部设置一个断点,您至少应该能够看到抛出了什么异常。Any unhandled exception that occurs in a task queued to the ThreadPool will cause your application to shut down. Put the whole body of your
scraping1
method inside a try/catch block, set a breakpoint at the top of the catch, and you should at least be able to see what exception is being thrown.