Pergunta

I am trying to use multithreading in my application. The method test5 tries to fetch some content from Internet, while the main thread waits for all threads to finish before continuing with other work.

But my main thread doesn't come back after calling test5, and my console lines Done Inside!! and thread all got back!! are never reached.

How can I resolve this issue?

class Program
{
    static void Main(string[] args)
    {
        string[] url =
        {
            "http://...", "http://...", "http://...", "http://...", "http://..."
        };

        test5(url); 
        Console.WriteLine("thread all got back!!");

        // Do some other work after all threads come back
        Console.ReadLine();
    }

    private static void test5(string[] _url)
    {
        int numThreads = _url.Length;
        ManualResetEvent resetEvent = new ManualResetEvent(false);
        int toProcess = numThreads;

        for (int i = 0; i < numThreads - 1; i++)
        {
            new Thread( delegate() {
                testWebWorking(_url[i]);
                if (Interlocked.Decrement(ref toProcess) == 0)
                    resetEvent.Set();
            }).Start();
        }
        resetEvent.WaitOne();
        Console.WriteLine("Done inside!!");
    }

    private static void test6(string[] _url)
    {
        int numThreads = _url.Length;
        var countdownEvent = new CountdownEvent(numThreads);

        for (int i = 0; i < numThreads - 1; i++)
        {
            new Thread(delegate() {
                testWebWorking(_url[i]);
                countdownEvent.Signal();
            }).Start();
        }
        countdownEvent.Wait();
        Console.WriteLine("Done inside!!");
    }

    private static void testWebWorking(object url)
    {
        Console.WriteLine("start {0}", Thread.CurrentThread.ManagedThreadId);
        string uri = (string)url;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.KeepAlive = true;
        request.Timeout = 5000;
        request.ReadWriteTimeout = 5000;
        request.Proxy = null;

        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                //Console.WriteLine(response.ContentType + "; uri = " + uri);
                Stream receiveStream = response.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                // Pipes the stream to a higher level stream reader with the required encoding format. 
                StreamReader readStream = new StreamReader(receiveStream, encode);
                //Console.WriteLine("\r\nResponse stream received.");
                Char[] read = new Char[256];
                // Reads 256 characters at a time.    
                int count = readStream.Read(read, 0, 256);
                //Console.WriteLine("HTML...\r\n");
                String str = "";
                while (count > 0)
                {
                    // Dumps the 256 characters on a string and displays the string to the console.
                    str = new String(read, 0, count);
                    //Console.Write(str);
                    count = readStream.Read(read, 0, 256);
                }
                //Console.WriteLine(str);
                // Releases the resources of the response.
                response.Close();
                // Releases the resources of the Stream.
                readStream.Close();

                Console.WriteLine("end {0}", Thread.CurrentThread.ManagedThreadId);
            }
        }
        catch (WebException ex)
        {
            //Console.WriteLine(ex.GetBaseException().ToString() );
            //Console.WriteLine(url);
            Console.WriteLine("time out !!");
        }
        finally
        {
            request.Abort();
            request = null;
            GC.Collect();
        }
    }
}
Foi útil?

Solução

Look at this:

for (int i = 0; i < numThreads - 1; i++)

You're only starting numThreads - 1 threads. Your counter starts at numThreads and counts down, so it'll only ever reach 1, not 0.

As an aside, this is also broken::

for (int i = 0; i < numThreads - 1; i++)
{
    new Thread( delegate()
    {
        testWebWorking(_url[i]);
        ...
    }
    ...
}

Here, you're capturing the variable i within the delegate, so it will have whatever value i has when you execute it. You may very well test the same URL more than once, and skip other URLs. Instead, you should copy the value of i into a variable within the loop, so that you capture a different variable each time:

// Fixed the loop boundary as well
for (int i = 0; i < numThreads; i++)
{
    int copy = i;
    new Thread(() => // Use a lambda expression for brevity
    {
        // Fix naming convention for method name, too...
        TestWebWorking(_url[copy]);
        if (Interlocked.Decrement(ref toProcess) == 0))
        {
            resetEvent.Set();
        }
    }).Start()
}

Both of your methods have the same set of problems.

Personally I wouldn't use a for loop here anyway - I'd use a foreach loop:

private static void TestWithResetEvent(string[] urls)
{
    ManualResetEvent resetEvent = new ManualResetEvent(false);
    int counter = urls.Length;

    foreach (string url in urls)
    {
        string copy = url;
        Thread t = new Thread(() =>
        {
            TestWebWorking(copy);
            if (Interlocked.Decrement(ref toProcess) == 0))
            {
                resetEvent.Set();
            }
        });
        t.Start();
    }
    resetEvent.WaitOne();
    Console.WriteLine("Done inside!!");
}

Alternatively, it would be simpler to use Parallel.ForEach, which is designed for exactly this sort of thing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top