【问题标题】:What is the local varibale for (in MSDN code example on CountdownEvent Class)?什么是局部变量(在 CountdownEvent 类的 MSDN 代码示例中)?
【发布时间】:2013-03-01 09:17:53
【问题描述】:

在 MSDN 文章中CountdownEvent Class 代码示例(下),
local 变量有什么用?

using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
class CDESample
{
    // Demonstrates:
    //      CountdownEvent construction
    //      CountdownEvent.AddCount()
    //      CountdownEvent.Signal()
    //      CountdownEvent.Wait()
    //      CountdownEvent.Wait() w/ cancellation
    //      CountdownEvent.Reset()
    //      CountdownEvent.IsSet
    //      CountdownEvent.InitialCount
    //      CountdownEvent.CurrentCount
    static void Main()
    {
        // Initialize a queue and a CountdownEvent
        ConcurrentQueue<int> queue = new ConcurrentQueue<int>(Enumerable.Range(0, 10000));
        CountdownEvent cde = new CountdownEvent(10000); // initial count = 10000

        // This is the logic for all queue consumers
        Action consumer = () =>
        {
            int local;
            // decrement CDE count once for each element consumed from queue
            while (queue.TryDequeue(out local)) cde.Signal();
        };

        // Now empty the queue with a couple of asynchronous tasks
        Task t1 = Task.Factory.StartNew(consumer);
        Task t2 = Task.Factory.StartNew(consumer);

        // And wait for queue to empty by waiting on cde
        cde.Wait(); // will return when cde count reaches 0

        Console.WriteLine("Done emptying queue.  InitialCount={0}, CurrentCount={1}, IsSet={2}",
            cde.InitialCount, cde.CurrentCount, cde.IsSet);

        // Proper form is to wait for the tasks to complete, even if you that their work
        // is done already.
        Task.WaitAll(t1, t2);

        // Resetting will cause the CountdownEvent to un-set, and resets InitialCount/CurrentCount
        // to the specified value
        cde.Reset(10);

        // AddCount will affect the CurrentCount, but not the InitialCount
        cde.AddCount(2);

        Console.WriteLine("After Reset(10), AddCount(2): InitialCount={0}, CurrentCount={1}, IsSet={2}",
            cde.InitialCount, cde.CurrentCount, cde.IsSet);

        // Now try waiting with cancellation
        CancellationTokenSource cts = new CancellationTokenSource();
        cts.Cancel(); // cancels the CancellationTokenSource
        try
        {
            cde.Wait(cts.Token);
        }
        catch (OperationCanceledException)
        {
            Console.WriteLine("cde.Wait(preCanceledToken) threw OCE, as expected");
        }

        // It's good for to release a CountdownEvent when you're done with it.
        cde.Dispose();

    }
}

【问题讨论】:

    标签: c# multithreading delegates lambda anonymous


    【解决方案1】:

    TryDequeue 需要 T 类型的 out 参数。T 是队列的类型。本地参数将填充您刚刚为队列删除的对象。可用于进一步加工。见http://msdn.microsoft.com/en-us/library/dd287208.aspx

    【讨论】:

      【解决方案2】:

      我猜是因为TryDequeue()这个方法有一个int类型的参数,不使用也得给它一个。

      【讨论】:

        【解决方案3】:

        来自ConcurrentQueue&lt;T&gt;.TryDequeue

        尝试删除并返回开头的对象 并发队列。

        public bool TryDequeue(
            out T result
        )
        
        Parameters
        result
        Type: T
        When this method returns, if the operation was successful, result contains the object removed. If no object was available to be removed, the value is unspecified.
        

        T 是您的队列类型。 如果您想知道out 做什么,请查看MSDN

        queue.TryDequeue(out local) 方法返回true 时,您的cde.Signal() 方法将继续处理。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-21
          • 1970-01-01
          • 1970-01-01
          • 2018-03-02
          • 2020-04-07
          相关资源
          最近更新 更多