ASP.NET Web API Client ProgressMessageHandler Post Task stuck in WinForm App
I’ve happily tinkered with this in a console application without issue, but now in a WinForm app, a “Post” task just gets plain old stuck on either
Below is a complete listing of my very simple test application. Button 1 works fine, Button 2 freezes every time on the call to
Targetting 4.0 or 4.5 makes no difference. The same code in a console application has no issues.
OK, so it looks like this is my issue. For .NET 4.5, the obvious solution would be, as @StephenCleary suggests, to let the async / await pattern permeate from the
Now my problem is getting an equivalent solution in .NET 4.0 (for legacy reasons, of course). A close approximation would be to use a continuation in the button-click handler, trouble is (again for legacy reasons), I actually want the button-click handler to block until the async returns. I’ve found some creative solutions using the 4.0 compatible
I can’t imagine this is the most performant implementation, and it still feels frankly clumsy. Can I do better?
Any time you try to mix synchronous and asynchronous code, you’re going to end up with a bit of a mess. (I have a blog post explaining why this deadlock happens on Windows Forms but not Console apps).
The best solution is to make it all
Consider some alternatives. Would it be possible to disable the button at the beginning of the
Perhaps if you describe why you want the button-click handler to block (in another question), we could suggest alternative solutions.
It’s as good a solution as any other. It is possible to directly call