WPF-基础-02 DispatcherObject类

csharp 复制代码
   public abstract class DispatcherObject
{
    protected DispatcherObject();
    public Dispatcher Dispatcher { get; }
    public bool CheckAccess();
    public void VerifyAccess();
}

WPF中使用Dispatcher更新界面

csharp 复制代码
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);
         }
        }
}             
csharp 复制代码
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);
}
相关推荐
麻花20137 小时前
C#之WPF的C1FlexGrid空间的行加载事件和列事件变更处理动态加载的枚举值
开发语言·c#·wpf
lcintj7 小时前
【WPF】Prism学习(九)
学习·wpf·prism
界面开发小八哥7 小时前
界面控件DevExpress WPF中文教程:网格视图数据布局的列和卡片字段
wpf·界面控件·devexpress·ui开发·用户界面
△曉風殘月〆7 小时前
如何在WPF中嵌入其它程序
wpf
Crazy Struggle7 小时前
功能齐全的 WPF 自定义控件资源库(收藏版)
.net·wpf·ui控件库
shepherd枸杞泡茶21 小时前
WPF动画
c#·.net·wpf
lcintj21 小时前
【WPF】Prism学习(十)
学习·wpf·prism
wyh要好好学习21 小时前
WPF数据加载时添加进度条
ui·wpf
code_shenbing21 小时前
跨平台WPF框架Avalonia教程 三
前端·microsoft·ui·c#·wpf·跨平台·界面设计
lcintj2 天前
【WPF】Prism学习(八)
学习·wpf·prism