這期內容當中小編將會給大家帶來有關WPF中怎么通過線程使用ProcessBar,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
方法示例:
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);
}
});
}
}
}上述就是小編為大家分享的WPF中怎么通過線程使用ProcessBar了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。