Task.Run.Wait
-
While I was reading code produced by coworker of mine, I got confused. What does
public void DoSomething(double someParameter)
{
_SomeComponent.Set(someParameter);
Task.Run(() => Check(someParameter)).Wait();
}actually do, i.e. how does it differ from
public void DoSomething(double someParameter)
{
_SomeComponent.Set(someParameter);
Check(someParameter);
}The
Check
function is synchronous. So, the two versions could be equivalent? ThatTask.Run
starts theCheck
function in a newTask
. But because of theWait
,DoSomething
won't be left immediately after starting thatTask
, but only when thatTask
ran to completion (or failure). But I am too confused by that snippet now to be sure of my assumptions.Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
While I was reading code produced by coworker of mine, I got confused. What does
public void DoSomething(double someParameter)
{
_SomeComponent.Set(someParameter);
Task.Run(() => Check(someParameter)).Wait();
}actually do, i.e. how does it differ from
public void DoSomething(double someParameter)
{
_SomeComponent.Set(someParameter);
Check(someParameter);
}The
Check
function is synchronous. So, the two versions could be equivalent? ThatTask.Run
starts theCheck
function in a newTask
. But because of theWait
,DoSomething
won't be left immediately after starting thatTask
, but only when thatTask
ran to completion (or failure). But I am too confused by that snippet now to be sure of my assumptions.Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
Bernhard Hiller wrote:
how does it differ from
Well, it uses more memory, and adds a thread to the system ... But no, you are right. In essence starting a Task to call
Check
and then calling Wait is the same as calling the method directly: the calling method will not continue untilCheck
is complete. It's possible that the cow-orker doesn't know what he is doing, it's possible that theCheck
method was used elsewhere and called via a Task without the Wait, but that code caused problems when re-used. We don't know, and will probably never find out. But it's pretty poor code in it's current form!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
Bernhard Hiller wrote:
how does it differ from
Well, it uses more memory, and adds a thread to the system ... But no, you are right. In essence starting a Task to call
Check
and then calling Wait is the same as calling the method directly: the calling method will not continue untilCheck
is complete. It's possible that the cow-orker doesn't know what he is doing, it's possible that theCheck
method was used elsewhere and called via a Task without the Wait, but that code caused problems when re-used. We don't know, and will probably never find out. But it's pretty poor code in it's current form!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
Thanks. When you say that the cow-orker did not know what he did, you may be right. Next to that code sits a
TimeSpan.FromSeconds(SET_XY_MAX_WAIT_TIME_MS)
(seconds vs. implied milliseconds ms)... Or he just tries to obfuscate his code so that nobody else can deal with it - job security by obscurity!Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
Thanks. When you say that the cow-orker did not know what he did, you may be right. Next to that code sits a
TimeSpan.FromSeconds(SET_XY_MAX_WAIT_TIME_MS)
(seconds vs. implied milliseconds ms)... Or he just tries to obfuscate his code so that nobody else can deal with it - job security by obscurity!Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
GO to QA - you'll meet a lot of that! :laugh:
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!
-
While I was reading code produced by coworker of mine, I got confused. What does
public void DoSomething(double someParameter)
{
_SomeComponent.Set(someParameter);
Task.Run(() => Check(someParameter)).Wait();
}actually do, i.e. how does it differ from
public void DoSomething(double someParameter)
{
_SomeComponent.Set(someParameter);
Check(someParameter);
}The
Check
function is synchronous. So, the two versions could be equivalent? ThatTask.Run
starts theCheck
function in a newTask
. But because of theWait
,DoSomething
won't be left immediately after starting thatTask
, but only when thatTask
ran to completion (or failure). But I am too confused by that snippet now to be sure of my assumptions.Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!