当前位置: 首页 > article >正文

WPF-基础-02 DispatcherObject类

   public abstract class DispatcherObject
{
    protected DispatcherObject();
    public Dispatcher Dispatcher { get; }
    public bool CheckAccess();
    public void VerifyAccess();
}

WPF中使用Dispatcher更新界面

xaml
    <Grid>
        <TextBlock x:Name="tbkShow" HorizontalAlignment="Center" Margin="0,120,0,0" VerticalAlignment="Top"/>
        <Button x:Name="btnLogin" Click="btnLogin_Click" Content="登录" Margin="0,88,0,0" VerticalAlignment="Top" Width="75" Height="23"/>
    </Grid>
C#
形式一 (Thread )
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(Login);
            thread.Start();
        }
        private void Login() 
        {
            for (int i = 0; i < 30; i++)
            {
                Thread.Sleep(100);
                //利用Dispatcher更新主界面控件属性
                this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate {
                    btnLogin.Content = i + "s";
                    if (i == 29)
                        btnLogin.IsEnabled = true;
                });
            }
        }
形式二 (Task.Run)       
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(() =>
            {
                // 需要更新UI的操作
                Dispatcher.Invoke(() =>
                {
                    // 在UI线程上更新UI元素
                    btnLogin.Content = "点击登录后";
                });

            });
        }  
形式三 (Task.Factory)          
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                Task.Delay(100).Wait();

                Dispatcher.Invoke(() =>
                {
                    btnLogin.Content = "登录成功";
                });
            });
        }
形式四 (参数行为new Action()private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(Login);
            thread.Start();
        }
        private void Login() 
        {

            //利用Dispatcher更新主界面控件属性
            //Application.Current.Dispatcher.BeginInvoke()
            Dispatcher.BeginInvoke(DispatcherPriority.Normal,

                new Action(() =>
                {
                    Thread.Sleep(100);
                    this.btnLogin.Content = DateTime.Now.ToString();

                }));
        } 
形式五  (BeginInvoke 返回DispatcherOperation执行Completed)        
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
          Thread thread = new Thread(Login);
          thread.Start();
        }
        private void Login() 
        {

            //利用Dispatcher更新主界面控件属性
            //Application.Current.Dispatcher.BeginInvoke()
           var task=  Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,

                new Action(() =>
                {
                    Thread.Sleep(100);
                    this.btnLogin.Content = DateTime.Now.ToString();

            }));
            task.Completed += new EventHandler(task_Completed);
        }
        private void task_Completed(object sender, EventArgs e)
        {
            MessageBox.Show("任务已经完成");
        }
        private void Login() 
        {
         double i = 0d;
         for (i = 0d; i < 800d; i++)
          {
          Task.Delay(5).Wait();
          Action act = () => rect.Width++;
          Dispatcher.BeginInvoke(act, DispatcherPriority.Background);
         }
        }
}             
DispatcherPriority枚举值
Invalid = -1,
Inactive = 0,
SystemIdle = 1,
ApplicationIdle = 2,
ContextIdle = 3,
Background = 4,
Input = 5,
Loaded = 6,
Render = 7,
DataBind = 8,
Normal = 9, 以正常优先级将工作项目调度到 UI 线程。这是调度大多数应用程序工作项目时的优先级
Send = 10 以最高优先级将工作项目调度到 UI 线程
public sealed class Dispatcher
{
//同步
public void Invoke(Action callback);
public void Invoke(Action callback, DispatcherPriority priority);
public void Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken);
public void Invoke(Action callback, DispatcherPriority priority, CancellationToken cancellationToken, TimeSpan timeout);
public object Invoke(Delegate method, params object[] args);
//异步
public DispatcherOperation BeginInvoke(Delegate method, params object[] args);
public DispatcherOperation BeginInvoke(Delegate method, DispatcherPriority priority, params object[] args);
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method);
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method, object arg);
public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method, object arg, params object[] args);
}

http://www.kler.cn/news/318607.html

相关文章:

  • R语言 基础 笔记 3
  • 生成式AI赋能:对话式BI引领数据分析新潮流
  • 【devops】rsync介绍和使用
  • 数据库学习1
  • Leetcode 螺旋矩阵
  • 关于idea编辑xml文件卡死
  • 选择租用徐州服务器机柜的作用有哪些?
  • 统信服务器操作系统【开机自启动】配置方法
  • 关于前端vue3+element-plus项目正常安装运行时未报错,但是前端界面老是空白问题及解决方案(其他使用nodejs的框架同理)
  • Python记录
  • Unity Debug时出现请选择unity实例
  • 探索C语言与Linux编程:获取当前用户ID与进程ID
  • QT中的消息机制(事件机制)总结
  • 接口调用方式2
  • C语言 | Leetcode C语言题解之第434题字符串中的单词数
  • uniapp组件封装和父子组件间通讯的介绍和详细案例
  • UE学习篇ContentExample解读------Blueprint_Communication-上
  • 海康HIK IN客户端使用帮助说明
  • 【工具】语音朗读PDF的免费工具
  • 探索未来科技的深邃海洋:IT领域的波澜壮阔
  • 目标检测DOTA数据集
  • 助力智能化农田作物除草,基于YOLOv10全系列【n/s/m/b/l/x】参数模型开发构建农田作物场景下玉米苗、杂草检测识别分析系统
  • 算法:69.x的平方根
  • 力扣每日一题 字符串中最多数目的子序列 贪心 字符串 前缀和
  • JavaWeb--纯小白笔记06:使用Idea创建Web项目,Servlet生命周期,注解,中文乱码解决
  • 基于姿态估计算法的健身辅助应用
  • 关系型数据库 - MySQL II
  • Redis 数据同步原理
  • Go weak包前瞻:弱指针为内存管理带来新选择
  • Spring源码学习:SpringMVC(3)mvcannotation-driven标签解析【RequestMappingHandlerMapping生成】