UWP C# three warrnings connected to printing

VoyTec 671 Reputation points
2021-05-17T16:42:42.33+00:00

Hey, hey, hey! And greetings Arya :-)...

So, starting from beginning:
I downloaded sample of printing service from official Microsoft Learn (Git Hub to be specific) and the sample were missing four files so I copied the whole project and I removed conflicting codes and it's working somehow. But I have three warnings I have to deal with:

  1. Variable "e" is declared, but never used.
    I can't remove it 'couse program won't work. public async Task ShowPrintUIAsync()
    {
    // Catch and print out any errors reported
    try
    {
    await PrintManager.ShowPrintUIAsync();
    }
    catch (Exception e) {}
    }
  2. In this asynchronic method is missing operator "await" so it will work synchronic. Confider using "await" operator in case of defining waiting for non-blocking invoking API interface or sentence "await Task.Run(...)" in VitroSoft in case of moving processing intensive taska using processor in background. /// <summary>
    /// This is the event handler for PrintManager.PrintTaskRequested.
    /// </summary>
    /// <param name="sender">PrintManager</param>
    /// <param name="e">PrintTaskRequestedEventArgs </param>
    protected virtual void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
    {
    PrintTask printTask = null;
    printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
    {
    // Print Task event handler is invoked when the print job is completed.
    printTask.Completed += async (s, args) =>
    {
    };
                sourceRequested.SetSource(printDocumentSource);  
            });  
    
  3. Is the same as 2nd. /// <summary>
    /// This is the event handler for PrintManager.PrintTaskRequested.
    /// In order to ensure a good user experience, the system requires that the app handle the PrintTaskRequested event within the time specified by PrintTaskRequestedEventArgs.Request.Deadline.
    /// Therefore, we use this handler to only create the print task.
    /// The print settings customization can be done when the print document source is requested.
    /// </summary>
    /// <param name="sender">PrintManager</param>
    /// <param name="e">PrintTaskRequestedEventArgs</param>
    protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
    {
    PrintTask printTask = null;
    printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequestedArgs =>
    {
    // Print Task event handler is invoked when the print job is completed.
    printTask.Completed += async (s, args) =>
    {
                };  
    
                sourceRequestedArgs.SetSource(printDocumentSource);  
            });  
    
            // Choose not to show the preview by setting the property on PrintTask  
            printTask.IsPreviewEnabled = false;  
        }  
    

I will be glad to receive some help with this.

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2021-05-18T03:05:34.957+00:00

    Hello, Welcome to Micorosoft Q&A,

    Variable "e" is declared, but never used.

    You could use Debug.WriteLine(e.Message) to print current exception info to clean this warning.

    In this asynchronic method is missing operator "await" so it will work synchronic

    You need to add await keyword before async method to make it invoked async, for more detail please refer Asynchronous programming document.
    And here is sample code that you could refer.

    protected virtual void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)  
    {  
       PrintTask printTask = null;  
       printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>  
       {  
             // Print Task event handler is invoked when the print job is completed. async key word.  
             printTask.Completed += async (s, args) =>  
             {  
                // Notify the user when the print operation fails.  
                if (args.Completion == PrintTaskCompletion.Failed)  
                {    //await key word  
                   await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>  
                   {  
                         MainPage.Current.NotifyUser("Failed to print.", NotifyType.ErrorMessage);  
                   });  
                }  
             };  
      
             sourceRequested.SetSource(printDocumentSource);  
       });  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.