c#多线程问题/ vshost32-clr.exe已停止工作

时间:2011-09-12 19:01:49

标签: c# multithreading crash vshost32

我是c#和多线程的新手,我最近在我写的工具中遇到了一些障碍。该工具旨在生成和启动一堆HttpWebRequests。现在它的单线程工作得很好,但是当我开始在3个左右的工作线程中执行任务时,程序崩溃就会给我以下消息。

  

“vshost32-clr.exe已停止工作”

我想知道它是否与我如何制作这些线程有关?

这是我正在使用的c#代码段。任何建议,使这一点不那么粗制滥造将不胜感激。

private void CreateDocuments()
{
    int docCount = 0;
    ArrayList vsIDs = docRepo.GetVersionIDList();
    ArrayList versionList = new ArrayList();

    foreach (string vsID in vsIDs)
    {
        Document[] docs = docRepo.GetVersionSeries(vsID);
        versionList.Add(docs);

        if (versionList.Count == 3)
        {
            Console.WriteLine("Launch Thread");

            Document[] docs1 = (Document[])versionList[0];
            Document[] docs2 = (Document[])versionList[1];
            Document[] docs3 = (Document[])versionList[2];

            Worker w1 = new Worker(docs1);
            Worker w2 = new Worker(docs2);
            Worker w3 = new Worker(docs3);
            Thread t1 = new Thread(new ThreadStart(w1.Start));
            Thread t2 = new Thread(new ThreadStart(w2.Start));
            Thread t3 = new Thread(new ThreadStart(w3.Start));
            Console.WriteLine("Threads Started");
            t1.Start();
            t2.Start();
            t3.Start();
            //Wait until all threads have started
            while (!t1.IsAlive || !t2.IsAlive || !t3.IsAlive) { Console.WriteLine("Waiting for Threads to Start"); }
            Console.WriteLine("Wait on Threads");
            t1.Join();
            docCount += docs1.Length;
            t2.Join();
            docCount += docs2.Length;
            t3.Join();
            docCount += docs3.Length;
            log.Info(docCount + " Documents Imported");
            versionList.RemoveRange(0, 3);
        }               
    }
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
}

public class Worker
{
    ImportToolWebDAV itwd = new ImportToolWebDAV();
    Document[] docs;
    public Worker(Document[] _docs)
    {
        docs = _docs;
    }
    public void Start()
    {
        HttpStatusCode status = HttpStatusCode.OK;
        foreach (Document doc in docs)
        {
            status = itwd.createDocument(doc);
        }
        Console.WriteLine("Thread finished");
    }
}

这是做什么(或者至少它应该做什么)是获取“Document”对象的数组,并为每组3个数组启动3个线程,然后等待它们完成生成那些WebDAV PUT请求。这是为了测试线程而编写的粗略代码,但我认为在这种状态下它仍然很好。

1 个答案:

答案 0 :(得分:0)

如果您有dell计算机,可能需要检查此链接,因为“vshost32-clr.exe已停止工作”

http://social.msdn.microsoft.com/Forums/en-NZ/vbide/thread/308a2e5b-f486-4eb6-9276-5cc816665b86

进一步AppInit_dlls

我希望这会有所帮助......

bhupendra