0

我需要创建多个异步调用,例如

IList<Task> Ts = new List<Task>();
Ts.Add(GetInformationFromServer(ID));

但我没有在线程中等待,我从

所以应该可以这样做(旧方法之一,但有新方法吗?),来自另一个电话

GetInformation(string ID) {
   while (!Finish) {
      Thread.Sleep(100);
   }
   return _Information.First(a=>a.ID==ID);
 }

我当然可以将任务保存在一个变量中,但是我该如何启动它们呢?我如何获得状态?
我想我可以在另一个线程中等待它们,但是如何检查它们是否完成?我应该自己实施吗?
以及如何启动它们(我应该只使用 Task.WhenAll 而不等待)?

更新

我想通了,我必须实现自己的方式,所以答案是这样的,但我需要使用 Task 而不是 Func

/// The executed elements
private IList<T> _ExecutedElements;

/// The Stack over the elements to be executed
private Stack<T> _ExecutingElements;

/// The method to be runned
private Func<object, Task<T>> _Method;

/// Should the while-loop start?
private bool _Running;

/// The Task
private Task<T> _Task;

/// Construct the class
/// <param name="Method">The function to be executed</param>
public MultiAsync(Func<object, T> Method) {
   _Method = Method;
}

/// Add an element
/// <param name="Item">The item to be added</param>
public void AddItem(T Element) {
   _ExecutingElements.Push(Element);
}

/// Execute the method
public async void ExecuteAsync() {

   // Set it to start running
   _Running = true;

   // While there are elements left
   while (_ExecutingElements.Count > 0) {

      // Check if it is not running, and if it isn't break out
      if (!_Running) { break; }

      // The current element
      T Element = default(T);

      // Pop out the element, that has not been runned through
      do { Element = _ExecutingElements.Pop(); }
      while (!_ExecutedElements.Contains(Element));

      // Check if there is an element, and if there is execute the method and await it
      if (Element != default(T)) {
         await ExecuteMethodAsync(Element);
      }
   }
}

/// Execute the item
/// <param name="Item">The item to be executed</param>
/// <returns>The executed item (due to reflection in FillInformation, the Item is filled)</returns>
public async Task<T> ExecuteItemAsync(T Item) {

   // Check if the item has not been executed, and if it is not executed
   if (!_ExecutedElements.Contains(Item)) {

      // Stop the while-loop
      _Running = false;

      // Check if the Task is running, and if it is await it
      if (_Task != default(Task) && !_Task.IsCompleted && !_Task.IsFaulted) {
         await _Task;
      }

      // Execute the method using the specific item
      await ExecuteMethodAsync(Item);
   }

   // Start the while-loop
   ExecuteAsync();

   // Return the element
   return Item;
}

/// Execute the method
/// <param name="Item">The item to run</param>
/// <returns>The Task to be executed</returns>
private async Task ExecuteMethodAsync(T Item) {

   // Set the Task
   _Task = _Method.Invoke(Item)

   // Start the task
   T Element = await _Task;

   // Add the item to the List
   _ExecutedElements.Add(Element);

   // Fill the information
   FillInformation(Element);
}

通话是这样的

private async void FillTasksAsync(IEnumerable<Model.Task> Tasks) {
   _InfoLoader = new MultiAsync<Model.Task>(Tsk => { return GetTaskAsync(Tsk); });

   foreach (var Tsk in Tasks) {
      _InfoLoader.AddItem(Tsk);
   }
}
4

1 回答 1

1

我有一篇博客文章讨论了异步初始化,这听起来像是您需要的。它源自 Stephen Toub 的一个原创想法。

在这种情况下,您可以使用:

List<AsyncLazy<string>> Ts = ...
Ts.Add(new AsyncLazy<string>(() => GetServerStringAsync(ID));

要开始一次下载,您可以执行以下操作:

Ts[0].Start();

当你需要它时,你可以这样做:

var result = await Ts[0];

如果尚未完成下载,它将(异步)等待它完成下载。如果它已经存在,那么您将立即获得结果。

于 2012-12-21T23:09:49.610 回答