【问题标题】:c# Multithreading Issue / vshost32-clr.exe has stopped workingc#多线程问题/vshost32-clr.exe已停止工作
【发布时间】:2011-09-12 19:01:48
【问题描述】:

我是 C# 和多线程的新手,最近我在编写的工具中遇到了一些障碍。该工具旨在生成和启动一堆 HttpWebRequest。现在它在单线程中运行良好,但是当我开始在 3 个左右的工作线程中分配任务时,程序崩溃并给我以下消息。

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

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

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

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");
    }
}

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

【问题讨论】:

  • 您使用相同的数组索引值0 来访问versionList-array:Document[] docs1 = (Document[])versionList[0];,因此您的线程正在处理相同的数据。
  • 与您的问题完全不相关...但是如果您正在启动一个新的线程应用程序并且可以使用 .NET 4,我建议您使用任务并行库。它使管理线程任务变得更加简单。
  • 修复了 Document[] 错字。我为那个感到很惭愧。
  • 另外,感谢 Eric J 的提示。我会在前进的过程中牢记这一点。
  • 您在调试过程中是否收到此错误?

标签: c# multithreading crash vshost32


【解决方案1】:

如果您有戴尔机器,您可能需要查看此链接,因为“vshost32-clr.exe 已停止工作”

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

继续AppInit_dlls

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

~bhupendra

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 2017-01-06
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    相关资源
    最近更新 更多