run multiple task and await for all tasks completesRunning multiple async tasks and waiting for them all to completeCatch multiple exceptions at once?How to loop through all enum values in C#?How would I run an async Task<T> method synchronously?HttpClient.GetAsync(…) never returns when using await/asyncUsing async/await for multiple tasksHow and when to use ‘async’ and ‘await’How to safely call an async method in C# without awaitRunning multiple async tasks and waiting for them all to completeUsing ConfigureAwait(false) on tasks passed in to Task.WhenAll() failsMultiple async/await calls in a foreach loop iteration
Can Rydberg constant be in joules?
f-type expansion
What ways have you found to get edits from non-LaTeX users?
Is using haveibeenpwned to validate password strength rational?
Why can't I use =default for default ctors with a member initializer list
Did Milano or Benatar approve or comment on their namesake MCU ships?
A IP can traceroute to it, but can not ping
Meaning of 'lose their grip on the groins of their followers'
How to communicate to my GM that not being allowed to use stealth isn't fun for me?
Are there any important biographies of nobodies?
Inward extrusion is not working
English word for "product of tinkering"
Using "subway" as name for London Underground?
Is it possible to have the age of the universe be unknown?
Is a lack of character descriptions a problem?
What is the purpose of the goat for Azazel, as opposed to conventional offerings?
What is wrong with this proof that symmetric matrices commute?
Rebus with 20 song titles
Soft question: Examples where lack of mathematical rigour cause security breaches?
How does an ordinary object become radioactive?
Is it expected that a reader will skip parts of what you write?
Fixing obscure 8080 emulator bug?
How is water heavier than petrol, even though its molecular weight is less than petrol?
How to draw a Technology Radar?
run multiple task and await for all tasks completes
Running multiple async tasks and waiting for them all to completeCatch multiple exceptions at once?How to loop through all enum values in C#?How would I run an async Task<T> method synchronously?HttpClient.GetAsync(…) never returns when using await/asyncUsing async/await for multiple tasksHow and when to use ‘async’ and ‘await’How to safely call an async method in C# without awaitRunning multiple async tasks and waiting for them all to completeUsing ConfigureAwait(false) on tasks passed in to Task.WhenAll() failsMultiple async/await calls in a foreach loop iteration
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to run multiple task to access to database and to wait to all task finished to obtain the result and assign it to my viewmodel, i try many sample but i have never have the result, any help please, this my code
var model = new AggregationViewModel();
var loadDataTasks = new Task[]
Task.Run(async () =>model.Alive = await _repository.GetCountAsync<filter>(Predicate)),
Task.Run(async () => model.Delay = await _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")))
;
try
await Task.WhenAll(loadDataTasks);
foreach (var item in loadDataTasks)
catch (Exception ex)
c# .net asynchronous async-await
|
show 1 more comment
I need to run multiple task to access to database and to wait to all task finished to obtain the result and assign it to my viewmodel, i try many sample but i have never have the result, any help please, this my code
var model = new AggregationViewModel();
var loadDataTasks = new Task[]
Task.Run(async () =>model.Alive = await _repository.GetCountAsync<filter>(Predicate)),
Task.Run(async () => model.Delay = await _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")))
;
try
await Task.WhenAll(loadDataTasks);
foreach (var item in loadDataTasks)
catch (Exception ex)
c# .net asynchronous async-await
Possible duplicate of Running multiple async tasks and waiting for them all to complete
– Volkmar Rigo
Mar 24 at 18:03
I try same sample but my model.Delay and model.Alive always 0, and i don't know why
– user1428798
Mar 24 at 18:07
Your codce is missing the most essential part: how do you call this? Post the complete call chain.
– Henk Holterman
Mar 24 at 18:10
This is my main call, do i need something else?
– user1428798
Mar 24 at 18:10
ok sorry for that
– user1428798
Mar 24 at 18:11
|
show 1 more comment
I need to run multiple task to access to database and to wait to all task finished to obtain the result and assign it to my viewmodel, i try many sample but i have never have the result, any help please, this my code
var model = new AggregationViewModel();
var loadDataTasks = new Task[]
Task.Run(async () =>model.Alive = await _repository.GetCountAsync<filter>(Predicate)),
Task.Run(async () => model.Delay = await _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")))
;
try
await Task.WhenAll(loadDataTasks);
foreach (var item in loadDataTasks)
catch (Exception ex)
c# .net asynchronous async-await
I need to run multiple task to access to database and to wait to all task finished to obtain the result and assign it to my viewmodel, i try many sample but i have never have the result, any help please, this my code
var model = new AggregationViewModel();
var loadDataTasks = new Task[]
Task.Run(async () =>model.Alive = await _repository.GetCountAsync<filter>(Predicate)),
Task.Run(async () => model.Delay = await _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")))
;
try
await Task.WhenAll(loadDataTasks);
foreach (var item in loadDataTasks)
catch (Exception ex)
c# .net asynchronous async-await
c# .net asynchronous async-await
edited Mar 24 at 18:25
RAM
8,25754061
8,25754061
asked Mar 24 at 18:01
user1428798user1428798
71131841
71131841
Possible duplicate of Running multiple async tasks and waiting for them all to complete
– Volkmar Rigo
Mar 24 at 18:03
I try same sample but my model.Delay and model.Alive always 0, and i don't know why
– user1428798
Mar 24 at 18:07
Your codce is missing the most essential part: how do you call this? Post the complete call chain.
– Henk Holterman
Mar 24 at 18:10
This is my main call, do i need something else?
– user1428798
Mar 24 at 18:10
ok sorry for that
– user1428798
Mar 24 at 18:11
|
show 1 more comment
Possible duplicate of Running multiple async tasks and waiting for them all to complete
– Volkmar Rigo
Mar 24 at 18:03
I try same sample but my model.Delay and model.Alive always 0, and i don't know why
– user1428798
Mar 24 at 18:07
Your codce is missing the most essential part: how do you call this? Post the complete call chain.
– Henk Holterman
Mar 24 at 18:10
This is my main call, do i need something else?
– user1428798
Mar 24 at 18:10
ok sorry for that
– user1428798
Mar 24 at 18:11
Possible duplicate of Running multiple async tasks and waiting for them all to complete
– Volkmar Rigo
Mar 24 at 18:03
Possible duplicate of Running multiple async tasks and waiting for them all to complete
– Volkmar Rigo
Mar 24 at 18:03
I try same sample but my model.Delay and model.Alive always 0, and i don't know why
– user1428798
Mar 24 at 18:07
I try same sample but my model.Delay and model.Alive always 0, and i don't know why
– user1428798
Mar 24 at 18:07
Your codce is missing the most essential part: how do you call this? Post the complete call chain.
– Henk Holterman
Mar 24 at 18:10
Your codce is missing the most essential part: how do you call this? Post the complete call chain.
– Henk Holterman
Mar 24 at 18:10
This is my main call, do i need something else?
– user1428798
Mar 24 at 18:10
This is my main call, do i need something else?
– user1428798
Mar 24 at 18:10
ok sorry for that
– user1428798
Mar 24 at 18:11
ok sorry for that
– user1428798
Mar 24 at 18:11
|
show 1 more comment
2 Answers
2
active
oldest
votes
Check this out:
var task1 = _repository.GetCountAsync<filter>(Predicate);
var task2 = _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused"));
await Task.WhenAll(task1, task2); //wait both tasks to finish
model.Alive = await task1;
model.Delay = await task2;
PS: the function, which the above code is placed, is needed to be async for the above scenario.
There's a typo:WaitAll->WhenAll
– tukaef
Mar 24 at 18:28
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
add a comment |
Try this code:
var model = new AggregationViewModel();
//----------------------------------------
var tasks = new List<Task<int>>();
//Add CountA
tasks.Add(_repository.GetCountAsync<filter>(Predicate));
//Add CountB
tasks.Add(_repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")));
//----------------------------------------
// Create a task with "allTasks" name to wait to complete all tasks
Task allTasks = Task.WhenAll(tasks);
try
// Wait to compelete "allTask"
allTasks.Wait();
catch(AggregateException)
//----------------------------------------
//Working on the results
if (allTasks.Status == TaskStatus.RanToCompletion)
model.CountA=allTasks.Result[0]
model.CountB=allTasks.Result[1]
// Display information on faulted tasks.
else
foreach (var t in tasks)
Console.WriteLine("Task 0: 1", t.Id, t.Status);
what's t in the if please?
– user1428798
Mar 24 at 19:08
@user1428798, I have usedallTasksin the condition section of theifblock. Alsotvariable inforblock points to each one of your defined tasks. Do you have any problem?
– RAM
Mar 24 at 19:42
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
Sorry I corrected it toallTasks. Check it now please.
– RAM
Mar 24 at 19:57
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326828%2frun-multiple-task-and-await-for-all-tasks-completes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check this out:
var task1 = _repository.GetCountAsync<filter>(Predicate);
var task2 = _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused"));
await Task.WhenAll(task1, task2); //wait both tasks to finish
model.Alive = await task1;
model.Delay = await task2;
PS: the function, which the above code is placed, is needed to be async for the above scenario.
There's a typo:WaitAll->WhenAll
– tukaef
Mar 24 at 18:28
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
add a comment |
Check this out:
var task1 = _repository.GetCountAsync<filter>(Predicate);
var task2 = _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused"));
await Task.WhenAll(task1, task2); //wait both tasks to finish
model.Alive = await task1;
model.Delay = await task2;
PS: the function, which the above code is placed, is needed to be async for the above scenario.
There's a typo:WaitAll->WhenAll
– tukaef
Mar 24 at 18:28
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
add a comment |
Check this out:
var task1 = _repository.GetCountAsync<filter>(Predicate);
var task2 = _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused"));
await Task.WhenAll(task1, task2); //wait both tasks to finish
model.Alive = await task1;
model.Delay = await task2;
PS: the function, which the above code is placed, is needed to be async for the above scenario.
Check this out:
var task1 = _repository.GetCountAsync<filter>(Predicate);
var task2 = _repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused"));
await Task.WhenAll(task1, task2); //wait both tasks to finish
model.Alive = await task1;
model.Delay = await task2;
PS: the function, which the above code is placed, is needed to be async for the above scenario.
edited Mar 24 at 20:53
answered Mar 24 at 18:11
Derviş KayımbaşıoğluDerviş Kayımbaşıoğlu
16.1k22042
16.1k22042
There's a typo:WaitAll->WhenAll
– tukaef
Mar 24 at 18:28
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
add a comment |
There's a typo:WaitAll->WhenAll
– tukaef
Mar 24 at 18:28
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
There's a typo:
WaitAll -> WhenAll– tukaef
Mar 24 at 18:28
There's a typo:
WaitAll -> WhenAll– tukaef
Mar 24 at 18:28
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
i can't add await before WaitAll and i had never the result
– user1428798
Mar 24 at 19:03
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
yes, It is needed to be WhenAll sorry for that
– Derviş Kayımbaşıoğlu
Mar 24 at 20:53
add a comment |
Try this code:
var model = new AggregationViewModel();
//----------------------------------------
var tasks = new List<Task<int>>();
//Add CountA
tasks.Add(_repository.GetCountAsync<filter>(Predicate));
//Add CountB
tasks.Add(_repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")));
//----------------------------------------
// Create a task with "allTasks" name to wait to complete all tasks
Task allTasks = Task.WhenAll(tasks);
try
// Wait to compelete "allTask"
allTasks.Wait();
catch(AggregateException)
//----------------------------------------
//Working on the results
if (allTasks.Status == TaskStatus.RanToCompletion)
model.CountA=allTasks.Result[0]
model.CountB=allTasks.Result[1]
// Display information on faulted tasks.
else
foreach (var t in tasks)
Console.WriteLine("Task 0: 1", t.Id, t.Status);
what's t in the if please?
– user1428798
Mar 24 at 19:08
@user1428798, I have usedallTasksin the condition section of theifblock. Alsotvariable inforblock points to each one of your defined tasks. Do you have any problem?
– RAM
Mar 24 at 19:42
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
Sorry I corrected it toallTasks. Check it now please.
– RAM
Mar 24 at 19:57
add a comment |
Try this code:
var model = new AggregationViewModel();
//----------------------------------------
var tasks = new List<Task<int>>();
//Add CountA
tasks.Add(_repository.GetCountAsync<filter>(Predicate));
//Add CountB
tasks.Add(_repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")));
//----------------------------------------
// Create a task with "allTasks" name to wait to complete all tasks
Task allTasks = Task.WhenAll(tasks);
try
// Wait to compelete "allTask"
allTasks.Wait();
catch(AggregateException)
//----------------------------------------
//Working on the results
if (allTasks.Status == TaskStatus.RanToCompletion)
model.CountA=allTasks.Result[0]
model.CountB=allTasks.Result[1]
// Display information on faulted tasks.
else
foreach (var t in tasks)
Console.WriteLine("Task 0: 1", t.Id, t.Status);
what's t in the if please?
– user1428798
Mar 24 at 19:08
@user1428798, I have usedallTasksin the condition section of theifblock. Alsotvariable inforblock points to each one of your defined tasks. Do you have any problem?
– RAM
Mar 24 at 19:42
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
Sorry I corrected it toallTasks. Check it now please.
– RAM
Mar 24 at 19:57
add a comment |
Try this code:
var model = new AggregationViewModel();
//----------------------------------------
var tasks = new List<Task<int>>();
//Add CountA
tasks.Add(_repository.GetCountAsync<filter>(Predicate));
//Add CountB
tasks.Add(_repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")));
//----------------------------------------
// Create a task with "allTasks" name to wait to complete all tasks
Task allTasks = Task.WhenAll(tasks);
try
// Wait to compelete "allTask"
allTasks.Wait();
catch(AggregateException)
//----------------------------------------
//Working on the results
if (allTasks.Status == TaskStatus.RanToCompletion)
model.CountA=allTasks.Result[0]
model.CountB=allTasks.Result[1]
// Display information on faulted tasks.
else
foreach (var t in tasks)
Console.WriteLine("Task 0: 1", t.Id, t.Status);
Try this code:
var model = new AggregationViewModel();
//----------------------------------------
var tasks = new List<Task<int>>();
//Add CountA
tasks.Add(_repository.GetCountAsync<filter>(Predicate));
//Add CountB
tasks.Add(_repository.GetCountAsync<Flight>(predicate.And(x => x.Status == "refused")));
//----------------------------------------
// Create a task with "allTasks" name to wait to complete all tasks
Task allTasks = Task.WhenAll(tasks);
try
// Wait to compelete "allTask"
allTasks.Wait();
catch(AggregateException)
//----------------------------------------
//Working on the results
if (allTasks.Status == TaskStatus.RanToCompletion)
model.CountA=allTasks.Result[0]
model.CountB=allTasks.Result[1]
// Display information on faulted tasks.
else
foreach (var t in tasks)
Console.WriteLine("Task 0: 1", t.Id, t.Status);
edited Mar 24 at 19:57
answered Mar 24 at 18:56
RAMRAM
8,25754061
8,25754061
what's t in the if please?
– user1428798
Mar 24 at 19:08
@user1428798, I have usedallTasksin the condition section of theifblock. Alsotvariable inforblock points to each one of your defined tasks. Do you have any problem?
– RAM
Mar 24 at 19:42
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
Sorry I corrected it toallTasks. Check it now please.
– RAM
Mar 24 at 19:57
add a comment |
what's t in the if please?
– user1428798
Mar 24 at 19:08
@user1428798, I have usedallTasksin the condition section of theifblock. Alsotvariable inforblock points to each one of your defined tasks. Do you have any problem?
– RAM
Mar 24 at 19:42
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
Sorry I corrected it toallTasks. Check it now please.
– RAM
Mar 24 at 19:57
what's t in the if please?
– user1428798
Mar 24 at 19:08
what's t in the if please?
– user1428798
Mar 24 at 19:08
@user1428798, I have used
allTasks in the condition section of the if block. Also t variable in for block points to each one of your defined tasks. Do you have any problem?– RAM
Mar 24 at 19:42
@user1428798, I have used
allTasks in the condition section of the if block. Also t variable in for block points to each one of your defined tasks. Do you have any problem?– RAM
Mar 24 at 19:42
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
in the first part is not defined in the if
– user1428798
Mar 24 at 19:50
Sorry I corrected it to
allTasks. Check it now please.– RAM
Mar 24 at 19:57
Sorry I corrected it to
allTasks. Check it now please.– RAM
Mar 24 at 19:57
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326828%2frun-multiple-task-and-await-for-all-tasks-completes%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Possible duplicate of Running multiple async tasks and waiting for them all to complete
– Volkmar Rigo
Mar 24 at 18:03
I try same sample but my model.Delay and model.Alive always 0, and i don't know why
– user1428798
Mar 24 at 18:07
Your codce is missing the most essential part: how do you call this? Post the complete call chain.
– Henk Holterman
Mar 24 at 18:10
This is my main call, do i need something else?
– user1428798
Mar 24 at 18:10
ok sorry for that
– user1428798
Mar 24 at 18:11