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;namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void showPro(int value)
{
this.progressBar1.Value = value;
this.label1.Content = value + "%";
System.Threading.Thread.Sleep(1000);//停止一秒后继续+ }
private void button1_Click(object sender, RoutedEventArgs e)
{ int xueTiao = 0;
while (xueTiao < 100)
{ xueTiao++;
showPro(xueTiao);
}
MessageBox.Show("满了"); } }
}
进度条
while (true)
{
//System.Threading.Thread.Sleep(500); 此处可以加这句
progressBar1.Value += 1;
if (progressBar1.Value>=progressBar1.Maximum)
{
return;
}
}
{
this.progressBar1.Value = value;
this.label1.Content = value + "%";
Application.DoEvents();//加这条,将消息给处理显示到界面上System.Threading.Thread.Sleep(1000);//停止一秒后继续+ }
可能少了引用吧,百度下Application.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;namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void showPro(int value)
{ this.Invoke((EventHandler)delegate
{
this.progressBar1.Value = value;
this.label1.Content = value + "%";
}); System.Threading.Thread.Sleep(1000);//停止一秒后继续+ }
private void button1_Click(object sender, RoutedEventArgs e)
{
Thread th = new Thread(new ThreadStart(ProgressBarAdd));
th.Start(); } private void ProgressBarAdd()
{
int xueTiao = 0;
while (xueTiao < 100)
{ xueTiao++;
showPro(xueTiao); }
MessageBox.Show("满了");
} }
}