开发者

WPF, MVVM, and Asynchronous work

I know this question has been asked here before, but I've read through several answers that haven't helped me.

I have a ComboBox that needs to get some information related to a selection from a database (probably on lost focus, to stop a thousand calls during scrolling). This information is for display only, and not critical, so getting on a background thread/task sounds like the perfect solution. It does take several seconds though, as it is getting counts from some very large tables. The user should be free to move on to other tasks, as this information really is just for display/reference only.

This question advocates using a Background worker, but this solution has two issues. 1) Changing the selection while the worker is already running introduces problems. You can either not start it the second time, meaning when it returns it is no longer showing valid info for the new selection, or try to cancel it (which doesn't always work). 2) For some reason I cannot explain, the method that actually accesses the database for the background worker returns slower if the method is in Model than if in the ViewModel, where I do not think it belongs. I really don't know why.

This question has several vote开发者_JAVA百科s, but the OP's question is worded very poorly, and the selected answer just says "yeah that should work."

This question's method looks promising, but the linked video is an hour long (I watched the whole thing), and touches on the dispatcher only for 10-15 seconds without explaining it. If someone has a link to an article that covers this method more in-depth, that would be good.

Thread pooling, as suggested here looks like it is probably the best way to go, as multiple requests for lookup just get queued, instead of causing already-running errors. However, it has no explanation of how to use a thread pool, instead linking to the MSDN article. If someone has a link to an article that covers this method more in-depth, that would be ideal, as it seems like the better solution (of course, I could be wrong).

I really tried to do my research on this one, but most of these answers just tell you what method to use, not how to use it. I am really looking for a "how-to."


OK. Your question:

  1. You have a selection control with list of items
  2. You have an expensive operation that returns some result from the currently selected item (Note that this operation should be expensive, not just takes time to return, in order to have you worry about not having too many of them at the same time) -- so you need to do it in parallel
  3. The returned result is not acted upon, only displayed -- so do it asynchronously
  4. If the currently selected item changes, you no longer want the previous result -- and the previous requests should be cancelled as soon as possible because they are expensive

What you should do, with the latest .NET technologies:

  1. Use Reactive Extensions (Rx), set up a throttle so that it only fires when the user keeps the current selection for, say, at least 500ms (you don't want to spawn many many taks when the user keeps pressing the down arrow key)
  2. When the throttle fires, call an async method (Async CTP) which await the operation in a Task (long-running to avoid starving the thread pool), and also put in a Cancellation token; save the current selection for comparison later
  3. When the operation returns, set the result into your data context (which your display control should be bound to) -- the async method always continues on the UI thread, so you don't have to worry about thread access
  4. If the throttle fires and there is an outstanding task / cancellation token, first use the cancellation token to cancel the task, before spawning a new task as per #2. The await will throw because the Task is cancelled, but it doesn't matter since you don't need it any more.
  5. There is no concurrency issues here because the Async CTP always continues on the UI thread. As far as all your operations are concerned, they are all single-threaded and won't tread on each other.

I think if you use the Async CTP with Rx, it is about 10 lines of code.

Note: If your operation is NOT EXPENSIVE, you don't have to use a cancellation token. Just allow the task to run to completion, but ignore the result. However, it is still recommended that you cancel a database query early, although it is not expensive on the client machine, it is expensive on the server.


You could try to use an async binding:

<ComboBox Name="theCombo" ... />
<TextBlock Text="{Binding Path=SomeSlowProperty, ElementName=theCombo, IsAsync=True}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜