How can I programatically close a WPF PrintDialog ? I tried to call Finalize on it trough reflection, and that does not close it either. Here is what I tried with:
using System;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication15
{
partial class Window1 : Window
{
PrintDialog _printDialog;
public Window1()
{
InitializeComponent();
new Thread(OpenDialog).Start();
new Thread(CloseDialog).Start();
}
void OpenDialog()
{
Thread.Sleep(1000);
Dispatcher.BeginInvoke((Action)OpenDialogImpl);
}
void OpenDialogImpl()
{
_printDialog = new PrintDialog();
_printDialog.ShowDialog();
}
void CloseDialog()
{
Thread.Sleep(2000);
Dispatcher.BeginInvoke((Action)CloseDialogImpl);
}
void CloseDialogImpl()
{
var type = typeof(PrintDialog);
var flags = BindingFlags.Instance | BindingFlags.NonPublic;
var finalize = type.GetMethod("Finalize", flags);
finalize.Invoke(_printDialog, null);
MessageBox.Show("Finalized");
}
}
}