.net中的任务并行库顺序访问

时间:2013-09-22 00:11:31

标签: c# .net multithreading task-parallel-library

我正在编写一个显示文件下载状态的组件。有一个应用程序可以访问我的组件的方法并传递参数,该参数还包含我需要在屏幕上显示的信息。

因此服务非常频繁地访问我的方法,因为它需要为我提供大量文件的信息(目前正在下载队列中)

 private void ProcessMessage(MessageOptions options)
  {
        Task t= Task.Factory.StartNew(()=>
        {
           //update data row using options object
        });    

        Task t1 = t.ContinueWith((continution) =>
               {
                 //here goes the code to update the MainUI   
               }
            ,TaskScheduler.FromCurrentSynchronizationContext());
        }

所以这就做了我需要的一切,到目前为止我还没有遇到任何实际问题。 但是这种方法存在潜在的问题。随着这种方法继续被称为这种事情可能会发生

  

调用1个选项。文件名=“文件1”   options.DataTransferred =“3 mb”

     

调用2个选项。文件名=“文件2”   options.DataTransferred =“6 mb”

     

调用3个选项。文件名=“文件1”   options.DataTransferred =“6 mb”

等等。每次调用此方法时,都会初始化一个新任务,当任务完成时,它会使用该信息更新MainUI。

问题

无法保证首先完成哪项任务。 Call3可能首先完成并显示文件1下载6 mb的信息,然后Call1完成并更新文件1下载3 mb的信息,这绝对是不可接受的。

我想确保任务1必须在任务3之前完成,因为这两个任务都获得了文件1的信息。

由于

2 个答案:

答案 0 :(得分:0)

如果您可以修改服务器并为正在发送的消息添加版本号,则可以随后丢弃其版本小于为该文件接收的上一个版本的任何消息。

只要消息以正确的顺序到达ProcessMessage,您也可以在更新数据行之前在客户端上添加版本号。

答案 1 :(得分:0)

如果我正确理解您的问题,您希望您的任务独立运行,但无论任务何时完成,您都希望继续按顺序运行。

我会使用这样的并发队列:

private ConcurrentQueue<Task<MessageOptions>> _tasks = new ConcurrentQueue<Task<MessageOptions>>();

private void ProcessMessage(MessageOptions options)
{
    var t= Task<MessageOptions>.Factory.StartNew(()=>
    {
       //update data row using options object
       return options;
    });    

    _tasks.Enqueue(t);

    Task t1 = t.ContinueWith(_ =>
    {
        // everytime a task finishes, update the UI for all completed tasks from the start of the queue
        Task<MessageOptions> firstInQueue;

        while (_tasks.TryPeek(out firstInQueue) &&
               firstInQueue.IsCompleted)
        {
            // this alwys runs in the same thread we can be sure the item is still in the queue
            _tasks.TryDequeue(out firstInQueue);

            var taskOptions = firstInQueue.Result;
            //here goes the code to update the MainUI
        }
    }
    ,TaskScheduler.FromCurrentSynchronizationContext());
 }