Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return value from function and start a task

Is it possible to have a function like this

public string Foo()
{
    Task.Factory.StartNew(Foo2());
    return "computedUniqueId";
}

The thing is that I want to start that Foo2 task AFTER Foo returns a value. The reason I ask this is that caller of Foo() function will be listening for information with that "computedUniqueId". Foo2() will trigger that information so there could be a situation that listener would not not what to wait for and will miss that message. So to summarize: Is there something(like continueWith) that will assert that Foo() returns its value before Foo2() kicks in ?

like image 371
Gwynnbleid1 Avatar asked Jun 22 '26 23:06

Gwynnbleid1


1 Answers

That's not possible because return is the last thing that happens in that method. After a return, the method in question has relinquished control and scope back to the caller. Period. The best I can think of is if you call Foo2 from where ever the string "computerUniqueId" is getting returned to.

Maybe if you supply more details we can help out more. Thanks.

like image 75
kmarks2 Avatar answered Jun 25 '26 12:06

kmarks2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!