• WPF刷新界面


    Winform 里有 Application.DoEvents();可刷新!

    WPF 里没这个,尽管可用委托实现多线程,但是刷新还是不行!

    后来找到了 类似App.DoEvents()的方法();

    代码:

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Windows;

    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Threading;
    using System.Windows.Threading;

     

    namespace wgscd
    {

     

        public partial class App : Application
        {
            private static DispatcherOperationCallback exitFrameCallback = new DispatcherOperationCallback(ExitFrame);
            public static void DoEvents()
            {
                DispatcherFrame nestedFrame = new DispatcherFrame();
                DispatcherOperation exitOperation =  Dispatcher.CurrentDispatcher.BeginInvoke (DispatcherPriority.Background, exitFrameCallback, nestedFrame);
                Dispatcher.PushFrame(nestedFrame);
                if (exitOperation.Status !=
                DispatcherOperationStatus.Completed)
                {
                    exitOperation.Abort();
                }
            }

            private static Object ExitFrame (Object state)
            {
                DispatcherFrame frame = state as
                DispatcherFrame;
                frame.Continue = false;
                return null;
            }


        }

     

    }

     

    ------------------------------------------------------------------

    这样在需要调用刷新的地方就可:App.DoEvents();

    -----------------例子--------------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Threading;
    using System.Windows.Threading;

     

    namespace wgscd
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                myDothread = new DoThread(doJob);
                t.Tick+=new EventHandler(t_Tick);
                t.Interval = TimeSpan.FromMilliseconds( 100d);
                t.Start();

            }


            int i = 0;
            public delegate void DoThread();
            DoThread myDothread;
            DispatcherTimer t = new DispatcherTimer();//定义一个 定时器
            void t_Tick(object sender ,EventArgs e){

                this.Title = DateTime.Now .ToString();
               // textBox1.Text = i.ToString();
            }

            private void button1_Click(object sender, RoutedEventArgs e)
            {
                textBox1.Text = "11";
                myDothread.BeginInvoke(null,null );

            }


            void doJob() {


                i = 0;

                //System.Windows.Threading.Dispatcher.Run();
             
                    DoThread dotd = delegate()
                    {

                    // Thread.Sleep(20000);  //这样就会让 按钮(button1)假死,WPF还是 无能为力吗?

                      while (i < 33773)
                        {
                            textBox1.Text =i.ToString();
                            i++;
                           // textBox1.UpdateLayout();
                            App.DoEvents();//这里可刷新文本框!
                       
                        }

                    };
            
               this.Dispatcher.BeginInvoke(dotd,null);


            }

            private void button2_Click(object sender, RoutedEventArgs e)
            {
                textBox1.Text = "hello";
            }

     

     

        }
    }

     

     

    //--------------------------------------------------------------------------------------------------------

    WPF多线程改变界面元素

     

    第1种用 Task类. 推荐用这个办法

    publicvoid工作_Task()
    {
    Dispatcher x=Dispatcher.CurrentDispatcher;//取得当前工作线程
    //另开线程工作
    Task<int>计数=newTask<int>(()=>{return计数方法(); });
    计数.ContinueWith(工作完毕后方法);//工作完毕后执行的方法
    计数.Start();//开始工作

    }
    publicvoid工作完毕后方法(Task<int>参数)
    {
    if(参数.IsCompleted)//正常工作完毕
    {
    var 结果=参数.Result;//取得结果
    //处理结果.
    //本方法非界面线程.如果需要在界面线程操作,需要转移到界面线程
    }
    }

    intc;
    publicint计数方法()
    {
    returnc++;
    }

    第2种方法用线程

    publicvoid工作_Thread()
    {
    Dispatcher x=Dispatcher.CurrentDispatcher;//取得当前工作线程
    //另开线程工作
    System.Threading.ThreadStart start=delegate()
    {
    //工作函数
    Func<string>fu=newFunc<string>(()=>{return""; });//工作函数
    var 工作结果=fu();//开始工作

    //异步更新界面
    x.BeginInvoke(newAction(()=>
    {
    //在界面线程操作 可以使用 工作结果
    }), DispatcherPriority.Normal); 
    };
    newSystem.Threading.Thread(start).Start();//启动线程
    }

    第3种方法用 BackgroundWorker.

    BackgroundWorker 后台线程;
    publicvoid线程初始化()
    {
    后台线程=newBackgroundWorker();
    后台线程.WorkerSupportsCancellation=true;//可以取消
    后台线程.DoWork+=newDoWorkEventHandler(后台线程_DoWork);
    后台线程.RunWorkerCompleted+=newRunWorkerCompletedEventHandler(后台线程_RunWorkerCompleted); 
    }
    publicvoid启动后台线程()
    {
    后台线程.RunWorkerAsync();
    }

    void后台线程_RunWorkerCompleted(objectsender, RunWorkerCompletedEventArgs e)
    {
    //工作完毕的方法
    }

    void后台线程_DoWork(objectsender, DoWorkEventArgs e)
    {
    //工作方法
    }

  • 相关阅读:
    N46期第一周作业
    马哥博客作业第五周
    马哥博客作业第四周
    马哥博客作业第三周
    04go条件和循环
    03go运算符
    01go变量和常量
    02vue指令-1
    01vue初识
    05django模板层
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434659.html
Copyright © 2020-2023  润新知