C#任务作为参数传递给其他任务

时间:2018-11-22 22:04:42

标签: c# asynchronous task

所以我目前有这样的代码:

//THIS IS A PART OF A METHOD
// ...
// Add a logic after the request has finished
(this.cache[RequestType.DeleteReceiver] as Task<Tuple<ServiceResponseCode, bool>>).ContinueWith((receiversDeletionTask) =>
{
     receiversDeletionTask.Wait();

     // Check that response was ok
     if (receiversDeletionTask.Result.Item1 == ServiceResponseCode.Ok && receiversDeletionTask.Result.Item2 == true)
     {
          ReceiverModel deletedReceiver = this.fetchedReceivers.Find(receiver => receiver.Id == id);
          this.fetchedReceivers.Remove(deletedReceiver);
          this.fetchedReceiversIndexes.RemoveAt(this.fetchedReceivers.IndexOf(deletedReceiver));
          foreach (int index in this.fetchedReceiversIndexes)
          {
              this.fetchedReceiversIndexes[index] = index;
          }
      }
});
return await (this.cache[RequestType.DeleteReceiver] as Task<Tuple<ServiceResponseCode, bool>>);

在此实现中,我返回第一个任务,并且在第一个任务完成后,调用方开始工作,但是我不希望调用方等待ContinueWith任务。

所以我试图重构为:

Task receiversDeletionTask = this.cache[RequestType.DeleteReceiver] as Task<Tuple<ServiceResponseCode, bool>>;
Task receiverDeletionFromCacheTask = new Task((receiversDeletionTask) => { });

但是,它提示我:代表“动作”不带1个参数

我知道动作不会引起争论。我需要一种方法来实现他的梦想。

我有一个删除服务器上接收者的任务-receiversDeletionTask; 我需要在完成receiversDeletionTask之后启动一个方法,该方法将从本地缓存中删除数据。 而且,只有在缓存清除方法完成后,我才需要返回信息。

0 个答案:

没有答案