我有带进度条的wpf应用程序。我想在1分钟后开始。我的进度条正常工作只是想在特定时间段后开始。
进度条代码
public login()
{
InitilizeComponent()
DispatcherTimer timer= new Timer(1000);
timer.Elapsed+= timer_Elapsed;
timer.start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
thsi.Dispatcher.Invoke(DispatcherProperty.Normal,(Action)() =>
{
if (progressBar<20)
{
progressBar.value+=1;
}
else
{
timer.stop();
}
}
)
}
答案 0 :(得分:0)
您可以使用async / await方法和计时器的组合来实现此目的:
DispatcherTimer timerProgressBar;
public login()
{
InitializeComponent();
//Create the timer
timerProgressBar = new DispatcherTimer();
timerProgressBar.Interval = new TimeSpan(0,0,1);
timerProgressBar.Tick += timerProgressBar_Tick;
//Start waiting
WaitForProgressBar();
}
void timerProgressBar_Tick(object sender, EventArgs e)
{
this.Dispatcher.Invoke(() =>
{
if (progressBar.Value < 20)
progressBar.Value++;
else
timerProgressBar.Stop();
});
}
/// <summary>
/// Begins waiting for 1 minute before starting the timer.
/// </summary>
public async void WaitForProgressBar()
{
await Task.Run(() => System.Threading.Thread.Sleep(new TimeSpan(0, 1, 0)));
timerProgressBar.Start();
}
使用异步方法,这将使您的UI在等待期间保持响应。如果这不是预期的,那么只需将异步方法更改为同步。