question

JeremyNelson-4460 avatar image
0 Votes"
JeremyNelson-4460 asked ·

Cancel UWP Print Task

I'm implementing a print feature in a UWP app, and I cannot find any nice way to cancel the print task after clicking Print on the preview page. Our users may be printing dozens or even hundreds of pages at a time, which takes awhile to send to the printer, so having a cancel option is very important.

The user's workflow is:

  1. Select some pages to print

  2. Configure some custom print options

  3. Show the Print UI with the preview (PrintManager.ShowPrintUIAsync())

  4. Click print

  5. Show a progress bar/status while the job is being sent to the printer. This can take awhile, so a cancel option needs to be available here.

I'm following the print sample: https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/Printing I cannot find any way to cancel in PrintManager, PrintTask, PrintTaskRequest, AddPagesEventArgs, PrintTaskProgressingEventArgs, etc.

My only idea is to use a CancellationTokenSource and throw if cancellation is requested, but this seems like a hacky workaround. Am I missing something?






windows-uwp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried to set the printTask as null to see if it would stop the task, but it still continued to print. I'm not sure how to cancel the printTask when printing, so I will ask the team about it.

0 Votes 0 ·

1 Answer

JeremyNelson-4460 avatar image
0 Votes"
JeremyNelson-4460 answered ·

I can sort of cancel if I use a CancellationTokenSource and check for cancellation in the AddPages event handler.

 protected async void AddPrintPages(object sender, AddPagesEventArgs args)
         {
             PrintDocument printDoc = (PrintDocument)sender;
             for ( uint i = 0; i < _pageCount; i++)
             {
                 if (_cancellationToken.IsCancellationRequested) 
                     break;
    
                  var page = await GetPrintPageAsync(i)
                  printDoc.AddPage(page);
             }           
    
             printDoc.AddPagesComplete();
         }

This skips the remainder of the pages, but any pages that have already been added to the print doc will still be sent.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.