public NotifyWorkerPage() { InitializeComponent(); this.Loaded += delegate(object sender, RoutedEventArgs e) { // In general, you could handle this directly in the constructor, // but in the case where a component is re-loaded, it's good to do this Debug.Assert(m_notifyWorker == null); m_notifyWorker = new NotifyWorker(preWork, work, postWork); m_notifyWorker.ClientException += m_notifyWorker_ClientException; m_textBlockLastException.DataContext = m_notifyWorker; }; this.Unloaded += delegate(object sender, RoutedEventArgs e) { // As a usage pattern, dispose NotifyWorker when done with it // this ensures that the 'work' being done finishes and // the worker thread exits cleanly m_notifyWorker.Dispose(); m_notifyWorker.ClientException -= m_notifyWorker_ClientException; m_notifyWorker = null; m_textBlockLastException.DataContext = null; }; }