可以抢占Windows消息循环吗?

发布于 2024-09-29 07:47:40 字数 348 浏览 0 评论 0原文

我有一个 wpf 应用程序,它通过将可观察集合绑定到网格的常用方法来填充 Infragistics XamDataGrid。当每个数据项一次一个地填充到集合中时,网格就会更新。我还有一个取消按钮,如果用户单击它,我想立即停止填充。但是,需要几秒钟或更长时间才能响应取消。

问题(我认为)是消息循环充满了网格填充事件,而我的取消位于后面,必须等待轮到它。我想知道是否有一种方法可以在队列前面插入一条消息,从而使取消更具响应性(是否黑客行为 - 如果黑客行为,请解释我可以预期的不良影响)。

我并没有遇到糟糕的表现;事实上,用户界面的响应速度非常快。问题纯粹在于取消事件必须在消息队列中等待轮到,我宁愿它的优先级高于填充消息。

编辑:澄清

I have a wpf application that populates an Infragistics XamDataGrid by the usual method of binding an observable collection to the grid. As each data item is populated into the collection, one at a time, the grid updates. I also have a cancel button that I would like to immediately stop the population if the user clicks it. However, it takes a few seconds or more to respond to the cancel.

The problem (I think) is that the message loop is full of the grid population events and my cancel is way in the back and must wait its turn. I was wondering if there is a way to insert a message in the front of the queue and thus make the cancel more responsive (hacky or not - if hacky, please explain what ill effects I can expect).

I am not experiencing bad performance; in fact the UI is quite responsive. The problem is purely that the cancel event has to wait its turn in the message queue and I would rather it have priority over the population messages.

edit: clarifications

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2024-10-06 07:47:40

当你说网格时,你指的是哪种网格?我认为您的问题可能是,对于大型集合,此设置可能不使用任何类型的项目虚拟化。在这种情况下,您最好使用 ListBox 或 ListView,它们可以使用 VirtualizingStackPanel 仅为屏幕上的项目生成 UIElement。

如果是这种情况,您的 ui 线程就会陷入困境,因为它为每个新项目生成元素,无论它是否显示在屏幕上。如果您使用的是第 3 方网格,您可以检查它是否具有可以打开的内置虚拟化功能。

When you say Grid, what kind of Grid do you mean? I think your issue might be that for a large collection, this setup might not use any type of item virtualization. You might be better off using a ListBox or ListView in this case, which can use a VirtualizingStackPanel to generate UIElements for only the on-screen items.

If this is the case, your ui thread is getting bogged down because its generating elements for every new item, whether or not it's being displayed on screen. If you're using a 3rd party Grid, you might check to see if it has any virtualization functionality built in that you can turn on.

怪我太投入 2024-10-06 07:47:40

答案最终是 DispatcherPriority:

 private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Dispatcher.Invoke(new Action(() => btnCancel.Command.Execute(null)), DispatcherPriority.Send);
        }

“Send”优先级是枚举类型中最高的,“SystemIdle”优先级最低。当我以该优先级调用按钮的命令时,取消会立即执行。

The answer ended up being the DispatcherPriority:

 private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Dispatcher.Invoke(new Action(() => btnCancel.Command.Execute(null)), DispatcherPriority.Send);
        }

"Send" priority is the highest of an enumerated type, with "SystemIdle" being the lowest. When I invoke the button's command with that priority, the Cancel goes through immediately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文