C#C
C#4y ago
6 replies
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)
{
    // ...
}

I'd need to integrate it here somehow
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();
}

(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?
Was this page helpful?