如何在wpf实现进度条
发布日期:2021-08-31 16:58:50 浏览次数:6 分类:技术文章

本文共 1156 字,大约阅读时间需要 3 分钟。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Linq;using System.Windows.Forms;using System.Threading;namespace doWorker{    public partial class Form1 : Form    {        delegate void MyDelegate(int value);        Thread t;//new一个新线程,以便用来更新ui的显示 int i = 0;        public Form1()        {            InitializeComponent();        }        // 在新的线程中做“需要长时间做的”工作        private void button1_Click(object sender, EventArgs e)        {            t = new Thread(doWork);            t.Start();        }        // 要长时间做的工作        void doWork()        {            MyDelegate d = new MyDelegate(setValue);            while (true)            {                ++i;//----WPF---added by wonsoft.cn---                this.Dispatcher.Invoke(d, i);                Thread.Sleep(100);            }        }        // 更新用户界面        void setValue(int value)            {            label1.Text = value.ToString();        }        // 终止线程的执行        private void button2_Click(object sender, EventArgs e)        {            t.Abort();        }    }}

 要想让主线程更新UI的显示,可以通过新建一个线程,来控制算法,在适当的时间去更新UI的线程。

具体的Dispatcher可以参考

转载地址:https://blog.csdn.net/weixin_34082789/article/details/85595198 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:推荐一款VS2008代码增强插件——MetalScroll
下一篇:每日英语:China Bond Trading Dives

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月03日 04时34分32秒