c# Multithreading Issue / vshost32-clr.exe 已停止工作

发布于 2024-12-03 23:45:27 字数 2423 浏览 1 评论 0原文

我对 C# 和多线程都很陌生,最近我在编写的工具中遇到了一些障碍。该工具旨在生成并启动一堆 HttpWebRequest。现在它在单线程上工作得很好,但是当我开始在 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");
    }
}

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

I'm new to both c# and multi-threading and I've recently hit a bit of a roadblock in the tool I'm writing. The tool is designed to generate and launch a bunch of HttpWebRequests. Right now it's working fine single threaded, but as soon as I started to divy out the task amongst 3 or so worker threads, the program crashes giving me the following message.

"vshost32-clr.exe has stopped working"

I'm wondering if it has to do with how I'm going about making these threads?

Here is the c# code snippet I'm using. Any recommendations for making this a bit less shoddy would be greatly appreciated.

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

What this is doing (or at least what it should be doing) is fetching arrays of "Document" objects, and launching 3 threads for every set of 3 arrays, then waiting for them to finish generating those WebDAV PUT Requests. This is really rough code written just for the purpose of testing threading out, but I assumed that in this state it was still fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戈亓 2024-12-10 23:45:27

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

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

进一步AppInit_dlls

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

~bhupendra

You may want to check this link if you have a dell machine, for "vshost32-clr.exe has stopped working"

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

Further on AppInit_dlls

I hope this helps...

~bhupendra

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文