开发者

Command CanExecute handlers conflicting with my threaded tasks

开发者 https://www.devze.com 2023-03-07 03:40 出处:网络
I\'ve started introducing some parallel programming into my WPF app to speed up the more intensive, repetitive tasks. It\'s going well, in that I\'m seeing noticeable differences in execution time and

I've started introducing some parallel programming into my WPF app to speed up the more intensive, repetitive tasks. It's going well, in that I'm seeing noticeable differences in execution time and perceived UI response.

I've obviously been careful to ensure there is no concurrent access to the relevant data from my UI thread and these task threads.

BUT - sometimes (not all the time), my program crashes, and it turns out that a command's CanExecute handler is trying to access the same data that is being processed in a parallel task. Or, conversely, my parallel task fails because the data is already being used by a CanExecute handler.

Is there a recommended way to sort out this issue? Can I turn CanExecute checking off? Must I add a test to each and every CanExecute method?

(On a side note, in looking at this issue, I'm extremely surprised at how often the CanExecute handlers get called! Part of me thinks, for performance reasons, it would be worth manually enabling 开发者_Python百科and disabling the commands, but surely it's that sort of menial task that WPF is trying to make easier for us!)


Yes, that bothered me also, how many times CanExecute is called. The only option i can think of, because you have little control of the checking of CanExecute, is minimizing the code in the handler and using a CanExecute local field.

Something like this:

CanExecuteSaveData ()
  {
  return _canTheDataBeSaved;
  }

And build in some concurrency checking in this code to avoid the concurrency problems.

0

精彩评论

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

关注公众号