C# Task pass as a parameter to other task












2














So i have currently such code:



//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>>);


In this implementation i return the first task, and the caller begins to work after the first task finishes, but i wan't the caller, to wait for the ContinueWith task.



So i have tried to refactor to this:



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


But, it prompts me: Delegate 'Action' does not take 1 arguments



I know that action doesn't take an argument. I need a way how to make his happen.



I have a task that deletes the receiver on the server - receiversDeletionTask;
I need after finishing the receiversDeletionTask launch a method, that will remove the data from local cache.
And i need to return info only when the cache clearing method will finish.










share|improve this question


















  • 1




    If you want the caller to wait for the continuation task, then you should await and return the continuation task, no?
    – elgonzo
    Nov 22 '18 at 22:15










  • @elgonzo Yes, i need to wait for it, but how to refactor the code to do so? I need 2 task to run after 1 task, and the caller should await for the 2 task
    – Cheese
    Nov 22 '18 at 22:16










  • The ContinueWith method returns the Task object for the continuation task. I have no idea what and why you want to refactor. Instead of awaiting and returning this.cache[RequestType.DeleteReceiver] just take the task object provided by ContinueWith and await and return this task object. Do i perhaps misunderstand you?
    – elgonzo
    Nov 22 '18 at 22:19












  • (You might perhaps need to modfiy your continuation task method so that it returns data the caller awaiting this task might want/need.)
    – elgonzo
    Nov 22 '18 at 22:23












  • @elgonzo "The ContinueWith method returns the Task object for the continuation task" - i think i missed this. Let me try it
    – Cheese
    Nov 22 '18 at 22:26
















2














So i have currently such code:



//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>>);


In this implementation i return the first task, and the caller begins to work after the first task finishes, but i wan't the caller, to wait for the ContinueWith task.



So i have tried to refactor to this:



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


But, it prompts me: Delegate 'Action' does not take 1 arguments



I know that action doesn't take an argument. I need a way how to make his happen.



I have a task that deletes the receiver on the server - receiversDeletionTask;
I need after finishing the receiversDeletionTask launch a method, that will remove the data from local cache.
And i need to return info only when the cache clearing method will finish.










share|improve this question


















  • 1




    If you want the caller to wait for the continuation task, then you should await and return the continuation task, no?
    – elgonzo
    Nov 22 '18 at 22:15










  • @elgonzo Yes, i need to wait for it, but how to refactor the code to do so? I need 2 task to run after 1 task, and the caller should await for the 2 task
    – Cheese
    Nov 22 '18 at 22:16










  • The ContinueWith method returns the Task object for the continuation task. I have no idea what and why you want to refactor. Instead of awaiting and returning this.cache[RequestType.DeleteReceiver] just take the task object provided by ContinueWith and await and return this task object. Do i perhaps misunderstand you?
    – elgonzo
    Nov 22 '18 at 22:19












  • (You might perhaps need to modfiy your continuation task method so that it returns data the caller awaiting this task might want/need.)
    – elgonzo
    Nov 22 '18 at 22:23












  • @elgonzo "The ContinueWith method returns the Task object for the continuation task" - i think i missed this. Let me try it
    – Cheese
    Nov 22 '18 at 22:26














2












2








2







So i have currently such code:



//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>>);


In this implementation i return the first task, and the caller begins to work after the first task finishes, but i wan't the caller, to wait for the ContinueWith task.



So i have tried to refactor to this:



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


But, it prompts me: Delegate 'Action' does not take 1 arguments



I know that action doesn't take an argument. I need a way how to make his happen.



I have a task that deletes the receiver on the server - receiversDeletionTask;
I need after finishing the receiversDeletionTask launch a method, that will remove the data from local cache.
And i need to return info only when the cache clearing method will finish.










share|improve this question













So i have currently such code:



//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>>);


In this implementation i return the first task, and the caller begins to work after the first task finishes, but i wan't the caller, to wait for the ContinueWith task.



So i have tried to refactor to this:



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


But, it prompts me: Delegate 'Action' does not take 1 arguments



I know that action doesn't take an argument. I need a way how to make his happen.



I have a task that deletes the receiver on the server - receiversDeletionTask;
I need after finishing the receiversDeletionTask launch a method, that will remove the data from local cache.
And i need to return info only when the cache clearing method will finish.







c# asynchronous task






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 22:04









Cheese

1,96352355




1,96352355








  • 1




    If you want the caller to wait for the continuation task, then you should await and return the continuation task, no?
    – elgonzo
    Nov 22 '18 at 22:15










  • @elgonzo Yes, i need to wait for it, but how to refactor the code to do so? I need 2 task to run after 1 task, and the caller should await for the 2 task
    – Cheese
    Nov 22 '18 at 22:16










  • The ContinueWith method returns the Task object for the continuation task. I have no idea what and why you want to refactor. Instead of awaiting and returning this.cache[RequestType.DeleteReceiver] just take the task object provided by ContinueWith and await and return this task object. Do i perhaps misunderstand you?
    – elgonzo
    Nov 22 '18 at 22:19












  • (You might perhaps need to modfiy your continuation task method so that it returns data the caller awaiting this task might want/need.)
    – elgonzo
    Nov 22 '18 at 22:23












  • @elgonzo "The ContinueWith method returns the Task object for the continuation task" - i think i missed this. Let me try it
    – Cheese
    Nov 22 '18 at 22:26














  • 1




    If you want the caller to wait for the continuation task, then you should await and return the continuation task, no?
    – elgonzo
    Nov 22 '18 at 22:15










  • @elgonzo Yes, i need to wait for it, but how to refactor the code to do so? I need 2 task to run after 1 task, and the caller should await for the 2 task
    – Cheese
    Nov 22 '18 at 22:16










  • The ContinueWith method returns the Task object for the continuation task. I have no idea what and why you want to refactor. Instead of awaiting and returning this.cache[RequestType.DeleteReceiver] just take the task object provided by ContinueWith and await and return this task object. Do i perhaps misunderstand you?
    – elgonzo
    Nov 22 '18 at 22:19












  • (You might perhaps need to modfiy your continuation task method so that it returns data the caller awaiting this task might want/need.)
    – elgonzo
    Nov 22 '18 at 22:23












  • @elgonzo "The ContinueWith method returns the Task object for the continuation task" - i think i missed this. Let me try it
    – Cheese
    Nov 22 '18 at 22:26








1




1




If you want the caller to wait for the continuation task, then you should await and return the continuation task, no?
– elgonzo
Nov 22 '18 at 22:15




If you want the caller to wait for the continuation task, then you should await and return the continuation task, no?
– elgonzo
Nov 22 '18 at 22:15












@elgonzo Yes, i need to wait for it, but how to refactor the code to do so? I need 2 task to run after 1 task, and the caller should await for the 2 task
– Cheese
Nov 22 '18 at 22:16




@elgonzo Yes, i need to wait for it, but how to refactor the code to do so? I need 2 task to run after 1 task, and the caller should await for the 2 task
– Cheese
Nov 22 '18 at 22:16












The ContinueWith method returns the Task object for the continuation task. I have no idea what and why you want to refactor. Instead of awaiting and returning this.cache[RequestType.DeleteReceiver] just take the task object provided by ContinueWith and await and return this task object. Do i perhaps misunderstand you?
– elgonzo
Nov 22 '18 at 22:19






The ContinueWith method returns the Task object for the continuation task. I have no idea what and why you want to refactor. Instead of awaiting and returning this.cache[RequestType.DeleteReceiver] just take the task object provided by ContinueWith and await and return this task object. Do i perhaps misunderstand you?
– elgonzo
Nov 22 '18 at 22:19














(You might perhaps need to modfiy your continuation task method so that it returns data the caller awaiting this task might want/need.)
– elgonzo
Nov 22 '18 at 22:23






(You might perhaps need to modfiy your continuation task method so that it returns data the caller awaiting this task might want/need.)
– elgonzo
Nov 22 '18 at 22:23














@elgonzo "The ContinueWith method returns the Task object for the continuation task" - i think i missed this. Let me try it
– Cheese
Nov 22 '18 at 22:26




@elgonzo "The ContinueWith method returns the Task object for the continuation task" - i think i missed this. Let me try it
– Cheese
Nov 22 '18 at 22:26

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438456%2fc-sharp-task-pass-as-a-parameter-to-other-task%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438456%2fc-sharp-task-pass-as-a-parameter-to-other-task%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Fiat S.p.A.

Type 'String' is not a subtype of type 'int' of 'index'