国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

WPF通過線程使用ProcessBar的方法詳解

2020-01-24 00:06:01
字體:
來源:轉載
供稿:網友

前言

WPF下使用進度條也是非常方便的,如果直接采用循環然后給ProcessBar賦值,理論上是沒有問題的,不過這樣會卡主主UI線程,我們看到的效果等全部都結束循環后才出現最后的值。

所以需要采用線程或者后臺方式給進度條賦值的方式,以下通過線程來觸發事件觸發的方式來實現給進度條賦值。這樣就可以模擬我們在實際過程中處理數據的一種進度方式。

方法示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;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;namespace WpfTestProcessBar{ /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window {  public delegate void ProgressDelegate(int percent);  public MainWindow()  {   InitializeComponent();   ProgressEvent += MainWindow_ProgressEvent;   beginImport();  }  void MainWindow_ProgressEvent(int percent)  {   Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(Pro.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, Convert.ToDouble(percent+ 1) });   Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(label.SetValue), System.Windows.Threading.DispatcherPriority.Background, new object[] { Label.ContentProperty, Convert.ToString((percent + 1)+"%") });   }  private event ProgressDelegate ProgressEvent;  private void beginImport()  {   Pro.Maximum = 100;   Pro.Value = 0;   label.Content = "0%";   ThreadPool.QueueUserWorkItem(state =>   {    Thread.Sleep(2000);    for (int i = 0; i < 100; i++)    {     if (ProgressEvent != null)     {      ProgressEvent(i);     }     Thread.Sleep(10);    }   });  } }}

以上只是一種實現方式,希望給有需要的人提供幫助。

效果如下:

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 防城港市| 宣威市| 昔阳县| 通海县| 乐清市| 清流县| 武乡县| 乐亭县| 南城县| 南昌县| 苍山县| 利津县| 寻乌县| 宝清县| 宝山区| 邯郸县| 仙游县| 天祝| 商南县| 喀什市| 泰兴市| 广宁县| 乐清市| 陇川县| 扎赉特旗| 稻城县| 三河市| 望江县| 龙南县| 无为县| 东丰县| 武山县| 乌拉特中旗| 阜阳市| 石城县| 保亭| 图们市| 辽宁省| 仁寿县| 台中市| 湖北省|