1

尝试在线程安全队列中播放 .wav 文件。任何线程都可以随机触发调用,但必须按顺序播放。问题是当我Play(string[] files)用 3 个文件名(目前从 UI 线程测试)调用时,最后一个文件播放了 3 次,而第一个文件从未播放过。看不出这是什么原因造成的..?

希望提供更简单的方法来执行此操作,但主要想知道为什么这不起作用。

public class Audio
{
    static ActionQueue actionQueue;
    public static string MediaFolder { get; set; }
    private static object syncroot = new object();

    static Audio()
    {
        actionQueue = new ActionQueue();
    }

    /// <summary>
    /// Plays .wav in async queue. 
    /// </summary>
    /// <param name="fileName"></param>
    public static void Play(string fileName)
    { 
        actionQueue.Add(() => PlaySound(fileName));
    }

    public static void Play(string[] files)
    {
        Console.WriteLine("ID0: " + Thread.CurrentThread.ManagedThreadId);
        foreach (string f in files.ToList())
        {
            Console.WriteLine("Queue file: " + f);
            actionQueue.Add(() => PlaySound(f));
        }
    }

    private static void PlaySound(string f)
    {
        Console.WriteLine("ID1: " + Thread.CurrentThread.ManagedThreadId);

        var fileName = f;

        Console.WriteLine("Play queue: " + fileName);

        fileName = Path.Combine(MediaFolder, fileName);

        if (!Path.HasExtension(fileName))
            fileName = fileName + ".wav";

        if (!File.Exists(fileName)) return;
        string ext = Path.GetExtension(fileName);
        if (ext != ".wav") return;

        Console.WriteLine("Playing: " + fileName);

        SoundPlayer player = new SoundPlayer(fileName);
        player.PlaySync();
    }
}

public class ActionQueue
{
    private  BlockingCollection<Action> persisterQueue = new BlockingCollection<Action>();     

    public  ActionQueue( )
    { 
        var thread = new Thread(ProcessWorkQueue);
        thread.IsBackground = true;
        thread.Start();
    }

    private   void ProcessWorkQueue()
    {
        while (true)
        {
            var nextWork = persisterQueue.Take(); 
            nextWork();  
        }
    }

    public  void Add(Action action)
    { 
        persisterQueue.Add(action ); 
    }
}
4

1 回答 1

3

这是经典的闭包捕获循环变量问题。

您需要复制循环变量,即

    foreach (string f in files.ToList())
    {
        var copy = f;
        Console.WriteLine("Queue file: " + f);
        actionQueue.Add(() => PlaySound(copy));
    }

传递给您的代表的原因是actionQueue在循环完成之前不会执行。当然,到那个时候,变量的f值已经改变了。

于 2013-02-21T16:06:19.707 回答