C
C#2y ago
HimmDawg

Using async,await in old codebase

Currently, I'm implementing a custom control that utilizes an API, so there are some async methods. The problem is that I cannot wrap my head around, how I would use them in this old codebase I'm working on (WinForms, .NET Framework 4.8). Simplified example: The user control has this method
public async Task LoadDocuments(int id)
{
// ...
}
public async Task LoadDocuments(int id)
{
// ...
}
I'd need to integrate it here somehow
void Init()
{
/*await*/ this.customControl.LoadDocuments(this.id);
}
void Init()
{
/*await*/ this.customControl.LoadDocuments(this.id);
}
Here I could change void to Task and put an async in front of it. That'd work, but this method is used here...
protected override void Method1()
{
/*await*/ Init();
}

protected override bool Method2()
{
/*await*/ Init();
}
protected override void Method1()
{
/*await*/ Init();
}

protected override bool Method2()
{
/*await*/ Init();
}
(don't ask about method names and how much they do not make sense) In the first method, i could theoretically use async and leave void as is, but in the second case, i cannot easily add async to the method, because now it requires a Task<bool> of course. Anybody got an idea?
4 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
HimmDawg
HimmDawg2y ago
Thank you. But this article states exactly what I was afraid of fluffyFoxMood either rewriting the entire code or use GetAwaiter, GetResult etc. and pray for no deadlocks
Trinitek
Trinitek2y ago
yeah those two are pretty much your options
Angius
Angius2y ago
It's async all the way down Just use the quick fixes to turn all the methods that aren't async into async ones lol Lastly, async Task Main(string[] args)