在尝试线程连接之前,您是否需要睡眠?

时间:2013-12-05 19:39:26

标签: c# multithreading nullreferenceexception

目前我的程序中只有一个奇怪的发布崩溃。它似乎在NullReferenceException上抛出webPageLoader[i].Join();,但我似乎没有在调试中失败。

我的问题可能与线程没有机会启动的事实有关,因此无法加入?有没有人得到任何关于如何调试它的提示,因为它似乎根本没有在调试中显示出来。

如果我在sleep(50)之前加webPageLoader[i].Join();它似乎没有崩溃。但我希望它不会在没有sleep的情况下崩溃。

for (int i = 0; i < pagesToGet; i++)
{
    crawlContext.vistedURLs.AddURL(CurrentURL);                                
    document[i] = new DocumentToScrape();
    webPageLoader[i] = new Thread(document[i].LoadPage);
    webPageLoader[i].Start(CurrentURL);

    if (!crawlContext.unVistedURLs.MoveNext())
    {
        break;
    }
}

for (int i = 0; i < pagesToGet; i++)
{
    **webPageLoader[i].Join();**
    if (document[i].Doc != null)
    {
        document[i].Doc = GetSubSetXPath(document[i].Doc, crawlContext.xPath);                      
        if (document[i].Doc != null)
        {
            crawlContext.scrapedUrls = ScrapeURLS(crawlContext.scrapedUrls, document[i]);                                      
        }
        else
        {
            //System.Windows.Forms.MessageBox.Show("Null Page Found");
        }

    }
}

0 个答案:

没有答案