Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to have a printdialog auto cancel itself if the DialogResult isn't returned for say 60 seconds.

What I have tried:

I use this bit of code to kick it off. I guess I need a way to introduce a timer to cancel the pdi but I'm lost.
C#
using (PrintDialog pdi = new PrintDialog())
{
   if (pdi.ShowDialog(this) == DialogResult.Cancel)
   {
      return;
   }

   printPages();
}
Posted
Updated 22-May-19 16:15pm

1 solution

private void button1_Click( object sender, EventArgs e ) {

   Timer timer = new Timer() {
      Interval = 10000
   };

   timer.Tick += Timer_Tick;
   timer.Start();

   try {
      using ( PrintDialog pdi = new PrintDialog() ) {
         if ( pdi.ShowDialog( this ) == DialogResult.Cancel ) {
            return;
         }
         MessageBox.Show( "Printing" );
      }
   } finally {
      timer.Stop();
      timer.Dispose();
   }
}

private void Timer_Tick( object sender, EventArgs e ) {
   SendKeys.Send( "{ESC}" );
}
 
Share this answer
 
Comments
Member 11709930 23-May-19 17:50pm    
Thanks Gerry, that's a great start and has got me moving.
Unfortunately SendKeys won't work for me as there no guarantee what the active window will be when the timer times out. Also there my be a few child windows open via the PrintDialog.
I think I need a way to find the HWND's for the Dialog and its children, then hit them with the user32.dll DestroyWindow

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900