次のような複数の非同期呼び出しを作成する必要があります
IList<Task> Ts = new List<Task>();
Ts.Add(GetInformationFromServer(ID));
しかし、私はスレッドで何を待っているのではなく、私はから呼び出しています
したがって、別の呼び出しから、このようにすることができるはずです (古い方法の 1 つですが、新しい方法はありますか?)。
GetInformation(string ID) {
while (!Finish) {
Thread.Sleep(100);
}
return _Information.First(a=>a.ID==ID);
}
もちろん、タスクを変数に保存することもできますが、どうすればタスクを開始できますか? また、ステータスを取得するにはどうすればよいですか?
別のスレッドでそれらを待つことができると思いますが、それらが終了したかどうかを確認するにはどうすればよいですか? 自分で実装する必要がありますか?
どうすればそれらを開始できますか (待機せずに Task.WhenAll を使用する必要があります)。
アップデート
私は自分のやり方で実装しなければならないことがわかったので、答えはこのようなものですが、 Func の代わりに Task を使用する必要があります
/// 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);
}
}