0

我正在尝试在我的应用程序中使用多线程。该方法test5尝试从 Internet 获取一些内容,而main线程在继续其他工作之前等待所有线程完成。

但是我的main线程在调用后没有回来test5,我的控制台线Done Inside!!thread all got back!!永远不会到达。

我该如何解决这个问题?

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();
        }
    }
}
4

1 回答 1

2

看这个:

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

你只是开始numThreads - 1线程。你的计数器从开始numThreads倒计时,所以它只会达到 1,而不是 0。

顺便说一句,这也被打破了::

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

在这里,您在委托中捕获变量 ,因此当您执行它时它将具有任何值。您可以多次测试同一个 URL,并跳过其他 URL。相反,您应该将 的值复制到循环的变量中,以便每次捕获不同的变量:iii

// 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()
}

您的两种方法都有相同的问题。

就个人而言,无论如何我都不会for在这里使用循环 - 我会使用foreach循环:

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

或者,使用它会更简单Parallel.ForEach,它专为这类事情而设计。

于 2012-09-06T19:44:01.857 回答