We have a library that is used by WPF and / or Winforms clients.
We have provided an asynchronous method similar to:
Task<int> GetIntAsync()
We also (unfortunately) provided a synchronous wrapper method:
int GetInt();
which essentially just calls the asynchronous method and calls .Result in its task.
We recently realized that under certain circumstances, some code in GetIntAsync should be executed in the main thread of the user interface (it needs to use an outdated COM component that is marked as a “Single” Threading model (that is, the component should work in the main STA thread not only any STA thread)
So the problem is that when GetInt() is called in the main thread, it will be a dead end since
.Result blocks the main thread,- the code inside
GetIntAsync() uses Dispatcher.Invoke to try to run in the main thread.
The synchronous method is already consumed, so removing it will require a change. So instead, we decided to use WaitWithPumping in our synchronous GetInt() method to allow access to the main thread.
This works fine, except for clients who use GetInt() from their user interface code. Previously, they expected that using GetInt() would prevent their user interface from responding - that is, if they called GetInt() from a button event handler, they expected window messages to not be processed until the handler will return. Now that the messages are pumped up, their user interface is responsive, and the same button can be clicked again (and they probably did not encode their handler to re-enter).
If there is a reasonable solution, we don’t want our customers to need to encode a user interface that responds during a GetInt call
Question:
- Is there a way to do
WaitWithPumping that will pump "Call to main" messages, but not pump other messages related to the user interface? - This would be sufficient for our purposes if the client user interface behaved as if a modal dialog box is currently being displayed, albeit hidden (i.e. the user cannot access other windows). But from what I read, you cannot hide the modal dialogue.
- Other workarounds you can come up with will be appreciated.
source share