Does Task.Run guarantee each method will be run to completion
up vote
0
down vote
favorite
There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.
List<Widget> widgetList = new List<Widget>()
{
file list with 100 widget objects
}
List<Task> tasks = new List<Task>();
foreach (Widget widget in widgetList)
{
Task t = Task.Run(async () =>
{
var result = await widget.Analyze(newData);
ReportResults(result);
});
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
No it is not a complete code example but should be enough for the question which is as follows:
Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?
TIA,
Doug
c#-4.0 async-await task-parallel-library
add a comment |
up vote
0
down vote
favorite
There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.
List<Widget> widgetList = new List<Widget>()
{
file list with 100 widget objects
}
List<Task> tasks = new List<Task>();
foreach (Widget widget in widgetList)
{
Task t = Task.Run(async () =>
{
var result = await widget.Analyze(newData);
ReportResults(result);
});
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
No it is not a complete code example but should be enough for the question which is as follows:
Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?
TIA,
Doug
c#-4.0 async-await task-parallel-library
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.
List<Widget> widgetList = new List<Widget>()
{
file list with 100 widget objects
}
List<Task> tasks = new List<Task>();
foreach (Widget widget in widgetList)
{
Task t = Task.Run(async () =>
{
var result = await widget.Analyze(newData);
ReportResults(result);
});
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
No it is not a complete code example but should be enough for the question which is as follows:
Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?
TIA,
Doug
c#-4.0 async-await task-parallel-library
There are two methods that together must be run in parallel, this is due to a time constraint. The next piece of data to work on is coming every 100ms for example.
List<Widget> widgetList = new List<Widget>()
{
file list with 100 widget objects
}
List<Task> tasks = new List<Task>();
foreach (Widget widget in widgetList)
{
Task t = Task.Run(async () =>
{
var result = await widget.Analyze(newData);
ReportResults(result);
});
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
No it is not a complete code example but should be enough for the question which is as follows:
Can I be sure that ReportResults(result) will run to completion per Task or is it possible that if a Task finishes and calls ReportResults(result) and then another task finishes that it can call ReportResults(result) resulting in unpredictable behavior?
TIA,
Doug
c#-4.0 async-await task-parallel-library
c#-4.0 async-await task-parallel-library
asked Nov 14 at 22:38
AeroClassics
569312
569312
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Each method will run to completion, unless something tears down the process or application domain.
As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults
works.
I would do something like this instead:
var results = await Task.WhenAll(
for widget in widgetList
select widget.Analyze(newData));
foreach (var result in results)
{
ReportResults(result);
}
Note: async-await
is a C# 5.0 feature, not available in C# 4.0.
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Each method will run to completion, unless something tears down the process or application domain.
As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults
works.
I would do something like this instead:
var results = await Task.WhenAll(
for widget in widgetList
select widget.Analyze(newData));
foreach (var result in results)
{
ReportResults(result);
}
Note: async-await
is a C# 5.0 feature, not available in C# 4.0.
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
add a comment |
up vote
1
down vote
accepted
Each method will run to completion, unless something tears down the process or application domain.
As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults
works.
I would do something like this instead:
var results = await Task.WhenAll(
for widget in widgetList
select widget.Analyze(newData));
foreach (var result in results)
{
ReportResults(result);
}
Note: async-await
is a C# 5.0 feature, not available in C# 4.0.
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Each method will run to completion, unless something tears down the process or application domain.
As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults
works.
I would do something like this instead:
var results = await Task.WhenAll(
for widget in widgetList
select widget.Analyze(newData));
foreach (var result in results)
{
ReportResults(result);
}
Note: async-await
is a C# 5.0 feature, not available in C# 4.0.
Each method will run to completion, unless something tears down the process or application domain.
As the "call ReportResults(result) resulting in unpredictable behavior", it depends on how ReportResults
works.
I would do something like this instead:
var results = await Task.WhenAll(
for widget in widgetList
select widget.Analyze(newData));
foreach (var result in results)
{
ReportResults(result);
}
Note: async-await
is a C# 5.0 feature, not available in C# 4.0.
answered Nov 15 at 0:33
Paulo Morgado
5,41611531
5,41611531
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
add a comment |
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
Good suggestion! As it turns out you are correct, as long as nothing tears things down the do run to completion. I think you solution is a bit more elegant.
– AeroClassics
Nov 16 at 19:29
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.
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.
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%2f53309787%2fdoes-task-run-guarantee-each-method-will-be-run-to-completion%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