c# - 什么是局部变量(在 CountdownEvent 类的 MSDN 代码示例中)?

标签 c# multithreading delegates lambda anonymous

在 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();

    }
}

最佳答案

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

关于c# - 什么是局部变量(在 CountdownEvent 类的 MSDN 代码示例中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15154243/

相关文章:

ios - 委托(delegate)方法在使用 If 语句测试时调用失败,但在没有 If 语句的情况下正确调用

java - Java 中的 Volatile 是什么?我们何时/如何使用它们?

iphone - UITextView 委托(delegate)类在单击 TextView 时崩溃?!这是怎么回事?

c# - 当您在 C# 任务等待程序上调用 `OnCompleted()` 时,您如何等待 OnCompleted 调用中给出的新作业?

c# - 如何打印用户选择的文档?

c - OpenMP 中的插入排序

java - 在没有 GUI 的情况下启动 JavaFX 应用程序线程

ios - 从非 UIViewController 类在 UIViewController 上设置标签文本

C# HttpWebRequest 使用特殊字符串发布表单数据

c# - 对多对象列表进行排序的正确方法?