WPF 刷新主界面

本文介绍了一个在WPF应用程序中实现DoEvents功能的方法,通过创建自定义的DispatcherOperationCallback来允许UI线程处理挂起的事件,从而解决长时间运行的操作导致UI冻结的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gifCode
 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2    /// App.xaml 的交互逻辑
 3    /// </summary>

 4    public partial class App : Application
 5ExpandedBlockStart.gifContractedBlock.gif    {
 6        private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
 7
 8        public static void DoEvents()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif        {
10            DispatcherFrame nestedFrame = new DispatcherFrame();
11
12            DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, exitFrameCallback, nestedFrame);
13
14            Dispatcher.PushFrame(nestedFrame);
15
16            if (exitOperation.Status != DispatcherOperationStatus.Completed)
17ExpandedSubBlockStart.gifContractedSubBlock.gif            {
18                exitOperation.Abort();
19            }

20        }

21
22        private static Object ExitFrame(Object state)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        {
24            DispatcherFrame frame = state as DispatcherFrame;
25            frame.Continue = false;
26            return null;
27        }

28    }

转载于:https://www.cnblogs.com/xsi640/articles/1593219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值