我有一个主要的WPF窗口,其中一个控件是我创建的用户控件。这个用户控制是一个模拟时钟,并包含一个更新小时,分钟和秒针的线程。最初它不是一个线程,它是一个计时器事件,更新了小时,分钟和秒钟,但我已将它更改为一个线程,因为当用户按下开始按钮然后时钟不会更新,所以我改变它为一个线程。 WPF窗口当WPF应用程序关闭时自动退出线程
代码片段:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GParts"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes
assembly=PresentationFramework.Aero"
xmlns:UC="clr-namespace:GParts.UserControls"
x:Class="GParts.WinMain"
Title="GParts"
WindowState="Maximized"
Closing="Window_Closing"
Icon="/Resources/Calendar-clock.png"
x:Name="WMain"
>
<...>
<!-- this is my user control -->
<UC:AnalogClock Grid.Row="1" x:Name="AnalogClock" Background="Transparent"
Margin="0" Height="Auto" Width="Auto"/>
<...>
</Window>
我的问题是,当用户退出应用程序,然后线程似乎继续执行。我希望线程在主窗口关闭时自动完成。用户控制构造的
代码片段:
namespace GParts.UserControls
{
/// <summary>
/// Lógica de interacción para AnalogClock.xaml
/// </summary>
public partial class AnalogClock : UserControl
{
System.Timers.Timer timer = new System.Timers.Timer(1000);
public AnalogClock()
{
InitializeComponent();
MDCalendar mdCalendar = new MDCalendar();
DateTime date = DateTime.Now;
TimeZone time = TimeZone.CurrentTimeZone;
TimeSpan difference = time.GetUtcOffset(date);
uint currentTime = mdCalendar.Time() + (uint)difference.TotalSeconds;
christianityCalendar.Content = mdCalendar.Date("d/e/Z", currentTime, false);
// this was before implementing thread
//timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
//timer.Enabled = true;
// The Work to perform
ThreadStart start = delegate()
{
// With this condition the thread exits when main window closes but
// despite of this it seems like the thread continues executing after
// exiting application because in task manager cpu is very busy
//
while ((this.IsInitialized) &&
(this.Dispatcher.HasShutdownFinished== false))
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
DateTime hora = DateTime.Now;
secondHand.Angle = hora.Second * 6;
minuteHand.Angle = hora.Minute * 6;
hourHand.Angle = (hora.Hour * 30) + (hora.Minute * 0.5);
DigitalClock.CurrentTime = hora;
}));
}
Console.Write("Quit ok");
};
// Create the thread and kick it started!
new Thread(start).Start();
}
// this was before implementing thread
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
DateTime hora = DateTime.Now;
secondHand.Angle = hora.Second * 6;
minuteHand.Angle = hora.Minute * 6;
hourHand.Angle = (hora.Hour * 30) + (hora.Minute * 0.5);
DigitalClock.CurrentTime = hora;
}));
}
} // end class
} // end namespace
怎么能当主窗口关闭,然后应用程序退出我自动从线程退出?
调度程序只在事件处理程序完成时运行。如果他在事件处理程序中有很长的计算时间,调度程序将不会运行以分派计时器事件。 – Gabe
好的,谢谢,但是当主应用程序(窗口)关闭或用户退出应用程序时,如何退出while循环?我的while循环是正确的?我已经测试,当退出应用程序的线程完成并达到线:console.write(“退出确定”)但由于某种原因退出应用程序后,我注意到CPU使用率是非常高的任务管理器,它似乎线程真的没有完成执行。当用户退出应用程序(关闭包含用户控制模拟时钟的主窗口)时,while循环中的正确条件以便退出并从线程中退出? 谢谢。 – toni
我认为发生了什么事情是所有调度员的工作都在你的应用程序关闭之前就已经被磨掉了。在你的循环中放置睡眠语句1秒,然后将线程更改为后台线程应该工作。 – RandomEngy