Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, May 22, 2017 5:59 PM
Hello,
I had and I have a Problem since years.
Now that I use c# 7.0 I hoped for a Solutions - but I do not see it.
Anywhere in a library (where I have no source Code) there is a Methode in a class clsA
Task<int> ClsA.Func();
Now I want to call this method synchronously.
...in my Code I have:
void MyFunc()
{
int x = clsA.Func(); //not possible must be await clsA.Func().
//the Methode must not return an I must be here - after the resutl x is available
DoSomeThing(x);
}
If I would use int x = await clsA.Func() then the Methode would return before DoSomething(x) is called.
That is not possible - because Overall the program before anything, DoSomething(x) must be called.
In the past I have used constructions with WaitHandles, which works - but that is not fine.
I simple Need:
x = ..... clsA.Func() and this will only return after the result is available.
So the question:
Is it possible in c# 7.0 to make such a call really synchronously?
Thank you for any help.
Rowe
All replies (5)
Wednesday, May 24, 2017 7:26 AM ✅Answered | 1 vote
Hi rowe10,
Thank you for posting here.
For your question, you want to async method synchronously, please check the following link.
It shows three ways. I hope this would be helpful.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Monday, May 22, 2017 6:55 PM
Check the members of Task<int>, such as Result: ‘int x = clsA.Func().Result’. See also other members.
Monday, May 22, 2017 9:18 PM
Hello,
thank you.
I have tried what you said.
It hangs.
I have tried:
//this is the async method I want to call synchronously - means could be in
//a library where I have no Access to source.
asyncTask<int> Fa()
{
awaitTask.Delay(5000);
return10;
}
publicintFb()
{
//here I try to call it synchronously - it hangs.
intx = Fa().Result;
returnx;
}
Thank you.
Rowe
Monday, May 22, 2017 9:45 PM
Are there any events defined in the class you are using that would specify success or error?
Monday, May 22, 2017 10:27 PM
Hello,
no the class is only the two Methodes which I test.
Thank you and best regards
Rowi