Programming Crashing Reason Unknown
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 = Conv开发者_Python百科ert.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);
}
}
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.
catch
{
// recursion here passing the SAME arg, what is to stop this?
scraping1(a);
return;
}
And
// 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);
}
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.
public void scraping1(object a)
{
try
{
// your method logic
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.Out.Flush();
}
}
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?"
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.
精彩评论