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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23452864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:05:44  来源:igfitidea点击:

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

wpfgridviewtelerikmessageboxradgridview

提问by Amin Eshtiaghi

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

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

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.

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

Note That the GridView Control is Telerik RadGridView

注意 GridView 控件是 Telerik RadGridView

回答by Dave Tillman

This answerdescribes 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.

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

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().

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

So, replace:

所以,替换:

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

with:

和:

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

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

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