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)
{
//工作方法
}

最新文章

  1. iOS 自定义选项卡-CYLTabBarController
  2. H5实现拍照并上传
  3. 101个LINQ示例,包含几乎全部操作
  4. 数据库迁移 Migrator.Net
  5. 如何在github上展示作品——为你的项目生成一个快速访问的网址如(DaisyWang88.github.io)
  6. hdu1051 Wooden Sticks
  7. bzoj2763: [JLOI2011]飞行路线 分层图+dij+heap
  8. LeanCloud使用入门(android)
  9. 内存管理pbuf.h头文件源码解析——LwIP学习
  10. 初入angular4——实际项目搭建总结
  11. ubuntu14.04 升级mysql到5.7版本
  12. 常见JS写法
  13. LeetCode编程训练 - 合并查找(Union Find)
  14. 使用Pandas将多个数据表合一
  15. python第十八天 多态 和 私有
  16. 一个struts2程序
  17. 使用axios发送post请求,将JSON数据改为为form类型
  18. php 上传大文件主要涉及配置upload_max_filesize和post_max_size两个选项。
  19. hive-2.3.3安装
  20. 6_python之路之atm购物

热门文章

  1. 【u211】编码
  2. web网站如何获取用户的地理位置
  3. windows下 管理员身份启动java进程
  4. 利用performSelectorInBackground和performSelectorOnMainThread实现多线程
  5. Oracle中的sid与servicename
  6. NSString与int和float的相互转换
  7. gen_server笔记
  8. 回顾Oracle几个用到的基本语句
  9. UIPasteboard粘贴板:UIlabel开启复制粘贴功能(一)
  10. 一起学Python: 多线程-共享全局变量问题