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

WPF刷新界面的更多相关文章

  1. WPF刷新界面之坎坷路

    WPF刷新界面之坎坷路 项目需要一个硬件检测功能,需要用到界面刷新,刚开始想用个定时器,对检测过的硬设定时添加后刷新界面. 但是很遗憾,定时器并不能进行刷新.后台检测List数据里面已经添加了很多了很 ...

  2. 【源码分享】WPF漂亮界面框架实现原理分析及源码分享

    1 源码下载 2 OSGi.NET插件应用架构概述 3 漂亮界面框架原理概述 4 漂亮界面框架实现  4.1 主程序  4.2 主程序与插件的通讯   4.2.1 主程序获取插件注册的服务   4.2 ...

  3. 重复点击主界面(TabBar)按钮刷新界面--点击状态栏回到顶部

    1.监听按钮点击   2.判断是否是点击的同一个按钮(记录上次点击的按钮)   3.当重复点击相同按钮时,需要获取当前按钮对应控制器刷新界面      3.1 判断是否重复点击按钮,代码写在哪里?   ...

  4. C#子线程刷新界面并关闭窗体

    目的:要循环刷新界面上的控件,同时不影响用户操作.循环结束后关闭窗体. 步骤:先创建一个窗体,窗体中拖入一个lable控件(label1),一个button控件(button1) 代码窗口输入: // ...

  5. 使用Jquery解决Asp.Net中下拉列表值改变后访问服务器刷新界面。

    使用DropDownList控件时,改变选项时,获取服务端数据库数据并刷新界面数据. 1. 绑定DropDownList控件SelectedIndexChanged事件. 2. AutoPortBac ...

  6. 使用OC和swift创建系统自带的刷新界面

    使用OC和swift创建系统自带的刷新界面 一:swift刷新界面代码: import UIKit class ViewController: UITableViewController { // 用 ...

  7. ListView中响应item的点击事件并且刷新界面

    ---恢复内容开始--- 最近在在实现listview功能中遇到了这个问题: 点击事件写在了adapter的item中,不知道如何在listview的点击事件中更新数据的显示: 总结:1.要使用not ...

  8. WPF防止界面卡死并显示加载中效果

    原文:WPF防止界面卡死并显示加载中效果 网上貌似没有完整的WPF正在加载的例子,所以自己写了一个,希望能帮到有需要的同学 前台: <Window x:Class="WpfApplic ...

  9. Angular 2/4/5+ 重复点击菜单刷新界面

    记一下,网上没找到方法 自己搞了好久  通过跳转到别的界面在跳回来的方式进行实现             //再次点击刷新界面       if (this.router.url == item.ur ...

随机推荐

  1. 应用 Valgrind 发现 Linux 程序的内存问题及交叉编译for arm

    Valgrind 概述 体系结构 Valgrind是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个框 ...

  2. Android 设置alpha值来制作透明与渐变效果的实例

    Android系统支持的颜色是由4个值组成的,前3个为RGB,也就是我们常说的三原色(红.绿.蓝),最后一个值是A,也就是Alpha.这4个值都在0~255之间.颜色值越小,表示该颜色越淡,颜色值越大 ...

  3. 20+ 个很有用的 jQuery 的 Google 地图插件 (英语)

    20+ 个很有用的 jQuery 的 Google 地图插件 (英语) 一.总结 一句话总结:英语提上来我才能快速去google上面找资源啊.google上面的资源要比百度丰富很多,然后有了英语就可以 ...

  4. iOS 9和xcode7设置

    升级了Xcode7各种问题来了,折腾两天 一.Xcode7  http适配设置 1.大部分社交平台接口不支持https协议. 2.大部分社交平台SDK不支持bitcode. 3.添加Scheme白名单 ...

  5. 使用RpcLite构建SOA/Web服务

    使用RpcLite构建SOA/Web服务 SOA框架系列 1. 使用RpcLite构建SOA/Web服务 提到Web服务最先想到的就是WebService此外常用的还有WCF.ServiceStack ...

  6. Google Android Studio Kotlin

    Google Android Studio Kotlin 开发环境配置 Google 近日开发者大会宣布Kotlin成为Android开发的第一级语言,即Android官方开发语言,可见Google对 ...

  7. centos安装启动ssh服务

    centos安装启动ssh服务 #rpm -qa |grep ssh 检查是否装了SSH包 没有的话yum install openssh-server #chkconfig --list sshd ...

  8. 【35.39%】【hdu 3333】Turing Tree

    Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  9. codeblocks如何设置,启动后自动打开上次未关闭的workspace

    设置--环境--普通设置--on application start-up -------default 关闭code::blocks时不要关那个workspace就可以.至于其它方法就不知道了.

  10. Clustered filesystem with membership version support

    A computer system with read/write access to storage devices creates a snapshot of a data volume at a ...