So I'm trying to get a driver working in a WinForms application (I wish I could go WPF but that is not an option here) and I'm running into an interesting problem. The UI thread blocks forever when an await method is called in the driver. Here is a very simplified version of my problem, again this only seems to happen on Windows Forms applications.
private void button_Click(object sender, EventArgs e)
{
MessageBox.Show(this.TestSynchronous().ToString());
}
// I'm trying to avoid having to change the code below
private int TestSynchronous()
{
return this.TestAsync().Result;
}
private async Task<int> TestAsync()
{
await Task.Delay(100);
var rand = new Random();
return rand.Next();
}
Is there a way I can wrap the call to the 'TestSynchronous' method to prevent it from permanently blocking the UI thread? Remember I'm trying to avoid having to change the 'Test' methods in any way.
I've seen similar posts like duplicate like await works but calling task.Result hangs/deadlocks which are similar to my problem but my problem cannot be solved the same way. The reason being he has direct access to the async method being awaited whilst I do not. So to reiterate, I'm calling into a library that only presents me with the 'TestSynchronous' method and I want to know if there's any way I can wrap the method so it doesn't end up blocking somewhere down into the library where I don't have access to change the code.