I'm developing some WPF program and make a thread called 'thCheckIdle' in 'SyringeTricontinent.cs' class which is generated in 'MainWindow.xaml.cs'. I wanna kill this thread when program exits so I added exit event in Application class and make behind code below:
private void Application_Exit(object sender, ExitEventArgs e)
{
if(((MainWindow)System.Windows.Application.Current.MainWindow).syringe.thCheckIdle!= null)
{
((MainWindow)System.Windows.Application.Current.MainWindow).syringe.thCheckIdle.Abort();
}
}
But it doesn't work popping up "An unhandled exception of type 'System.NullReferenceException' occurred in WHALE.exe" message and I figure out that status of MainWindow is null even program is executed showing MainWindow.
How can I kill that thread when I exit the main program?