Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

c# - Sync over Async avoiding deadlock and prevent UI from being responsive

We have a library that is being used by WPF and/or Winforms clients.

We've provided an asynchronous method similar to:

Task<int> GetIntAsync()

We've also (unfortunately) provided a synchronous wrapper method:

int GetInt();

which essentially just calls the asynchronous method and calls .Result on its task.

We recently realized under certain circumstances some code in the GetIntAsync needs to run on the main UI thread (it needs to use a legacy COM component that is marked as "Single" Threading model (i.e. the component must run in the main STA thread not just any STA thread)

So the problem is that when GetInt() is called on the main thread, it will deadlock since

  • the .Result blocks the main thread,
  • the code within GetIntAsync() uses Dispatcher.Invoke to attempt to run on the main thread.

The synchronous method is already being consumed so it would be a breaking change to remove it. So instead, we've opted for using the WaitWithPumping in our synchronous GetInt() method to allow the invoking to the main thread to work.

This works fine except for clients that use GetInt() from their UI code. Previously, they expected that using GetInt() would leave their UI unresponsive--that is, if they called GetInt() from within a button's click event handler, they would expect that no windows messages were processed until the handler returned. Now that messages are pumped, their UI is responsive and that same button can be clicked again (and they probably didn't code their handler to be re-entrant).

If there is a reasonable solution, we'd like to not have our clients need to code against the UI being responsive during a call to GetInt

Question:

  • Is there a way to do a WaitWithPumping that will pump "Invoke to main" messages, but not pump other UI related messages?
  • It would suffice for our purposes if the client UI behaved as if a modal dialog were currently shown, albeit hidden (i.e. user couldn't access the other windows). But, from what I read, you cannot hide a modal dialog.
  • Other workarounds you can think of would be appreciated.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rather than utilizing the existing message pump you can create your own message pump within the context of GetInt. Here is a blog entry discussing how to write one. This is the full solution the blog creates.

Using that you can write it as:

public int GetInt()
{
    return AsyncPump.Run(() => GetIntAsync());
}

That will result in completely blocking the UI thread as expected, while still ensuring that all continuations called from GetIntAsync don't deadlock as they'll be marshaled to a different SynchronizationContext. Also note that this message pump is still running on the main STA/UI thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...