WPF:调度程序处理已暂停,但仍在处理消息 [英] WPF : Dispatcher processing has been suspended, but messages are still being processed

查看:35
本文介绍了WPF:调度程序处理已暂停,但仍在处理消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 项目,当我尝试在 RowLoad 事件上运行此代码时,出现以下错误:

I Have a WPF Project, When i try to Run This Code On RowLoad Event I got below Error :

private void ParentGridView_OnRowLoaded(object sender, EventArgs e)
{
    try
    {
        if(((RadGridView)sender).Columns != null)
        {
            MessageBox.Show(((RadGridView)sender).Columns.Count.ToString(CultureInfo.InvariantCulture));
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

错误:调度程序处理已暂停,但仍在处理消息.

Error : Dispatcher processing has been suspended, but messages are still being processed.

注意 GridView 控件是 Telerik RadGridView

推荐答案

此答案 描述了与您相同的情况.(它引用了这个 答案不同的网站).

This answer describes the same situation as yours. (It references this answer on a different website).

调度器处理被暂停以避免更新可视化树时的重入问题.

The dispatcher processing is suspended to avoid reentrancy problems when updating the visual tree.

如果你真的需要显示一个消息框来响应你的Row Loaded"事件,你需要使用`Dispatcher.BeginInvoke()来推迟调用.

If you really need to display a message box in response to your "Row Loaded" event, you need to defer the call using `Dispatcher.BeginInvoke().

所以,替换:

MessageBox.Show(((RadGridView)sender).Columns.Count.ToString(CultureInfo.InvariantCulture));

与:

var msg = ((RadGridView)sender).Columns.Count.ToString(CultureInfo.InvariantCulture);
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(msg)));

如果此代码在 WPF 对象中,则 Dispatcher 属性可用.否则,您需要从其他地方获取它.

If this code is in a WPF object, then the Dispatcher property is available. Otherwise, you need to get it from somewhere else.

这篇关于WPF:调度程序处理已暂停,但仍在处理消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆