1

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.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • 3
    You use the same array index value `0` for the access of the versionList-array: `Document[] docs1 = (Document[])versionList[0];` so your threads are working on the same data. – Jan Sep 12 '11 at 19:08
  • Not at all related to your question... but if you're starting a new threading app and can use .NET 4, I recommend that you use the Task Parallel Library. It makes managing threaded tasks much simpler. – Eric J. Sep 12 '11 at 19:09
  • Fixed the Document[] typo. I'm feelin pretty ashamed of that one. – django_noob Sep 12 '11 at 19:14
  • Also, thanks for the tip Eric J. I'll keep that in mind as I move forward. – django_noob Sep 12 '11 at 19:15
  • Are you getting this error during debug? – VMAtm Sep 12 '11 at 19:15
  • The question is: Is your problem still there with the right indexes? Or have you typed in the code by hand on so? – Jan Sep 12 '11 at 19:15
  • I'm actually testing right now to see if the error persists. It wasn't that long ago that I rearranged the code to declare docs1,docs2,docs3 before hand instead of just passing "(Document[])versionList[x]" into the Worker constructors. I know that typo is new, but I could have sworn that I was having this same issue before I made the change. – django_noob Sep 12 '11 at 19:21
  • Well, I previously hit the vshost32-clr.exe stopped working message within 30 seconds. It's been running fine for several minutes now. Needless to say, It's better off now than it was before. Thanks for pointing that out Jan. – django_noob Sep 12 '11 at 19:27
  • You are hoping the ImportToolWebDAV class is thread-safe. Hard to guess whether it is, but a crash that the debugger won't catch certainly suggests it is not. – Hans Passant Sep 12 '11 at 19:32
  • Looks like the vshost32-clr.exe crashing is back, it just takes longer to actually get hit. Hans, I guess I'll have to look at what might be causing the hiccup in ImportToolWebDAV. – django_noob Sep 12 '11 at 20:28

1 Answers1

0

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