This is a migrated thread and some comments may be shown as answers.

Print RichTextBox mail merge letters in background thread

5 Answers 166 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Nisarg
Top achievements
Rank 1
Nisarg asked on 11 Jan 2016, 04:35 AM

Hi,

I want to print all mail merge letters (around 500 to 800) in background thread so that my application remains active and I should continue
doing tasks when the printing is in progress.
 
I also want to perform database task on all letters with the printing process in my application
but when I print all generated mail merge letters, my application is freezing until the printing is completed.

I also created support ticket no 999572.

Is it also possible to cancel printing process in the middle after printing is started?

Thanks

5 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 11 Jan 2016, 11:46 AM
Hello Nisarg,

I am attaching the modified version of the sample project to show how a similar scenario could be implemented. It demonstrates how you could use the Thread class to perform background mail merge and print the result. Here are some notes regarding the implementation:

  • In order to print RadRichTextBox, it must be included in the visual tree. Therefore, an additional Window is created to hold the RadRichTextBox instance.
  • When working in another thread, you may experience issues when the document contains images. I included the necessary code, which will freeze all the images in the document and will prevent this.
  • In the project is demonstrated how you could stop the process as well. It is done in the handler of the button with text "Stop Printing".
  • In case you need to notify the UI about the progress of the operations, you could use the BeginInvoke() method of the Dispatcher:
  • this.Dispatcher.BeginInvoke(new Action(() => ... );

Hope this helps.
 
Regards,
Tanya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Nisarg
Top achievements
Rank 1
answered on 12 Jan 2016, 11:45 AM
Hi Tanya,

Thanks for your reply.

MailMergePrintingDemo is working fine. I do not want to use default printer but
I would like to select printer from all available printer (by PrintDialog) before starting to print in background thread.

When I use PrintDialog.ShowDialog() method in the background thread, it throws an error
"An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
Additional information: The calling thread cannot access this object because a different thread owns it."

So how can I select printers from available printer options when I want to print in background thread?

Thanks

0
Tanya
Telerik team
answered on 15 Jan 2016, 06:43 AM
Hello Nisarg,

It is not allowed to show dialogs or other elements from the background thread to the UI. To achieve the desired goal, you could use the Dispatcher.Invoke() method. This method will give you access to the UI thread and you will be able to show the dialog. When the dialog is shown you can get the selected printer name through the PrintQueue.FullName property and pass it as a parameter to the print action:
this.Dispatcher.Invoke(() =>
{
    bool? dialogResult = dialog.ShowDialog();
    if (dialogResult.Value)
    {
        printerName = dialog.PrintQueue.FullName;
    }
});
 
 
this.printAction(result, printerName);

Here is how I changed its declaration to pass the new parameter:
private Action<RadDocument, string> printAction = new Action<RadDocument, string>((doc, printerName) =>
{ ... }

Before start printing on the background thread, a new PrintDialog should be created. The following snippet demonstrates how you could set the name of the printer to be used:
PrintDialog printDialog = new PrintDialog();
PrintQueue queue = new LocalPrintServer().GetPrintQueue(printerName);
printDialog.PrintQueue = queue;

You could follow the same approach for any other option that should be set from the user through the dialog.

Regards,
Tanya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Nisarg
Top achievements
Rank 1
answered on 21 Jan 2016, 11:58 AM
Hi Tanya,

When I try to get PrintQueue by printerName at this point - "PrintQueue queue = new LocalPrintServer().GetPrintQueue(printerName);"
I got below error.
An exception occurred while populating the properties for the PrintQueue object. Win32 error: The printer name is invalid.

I am using network printer to print document from the background thread.

Can you please advise or suggest the solution?

Thanks
0
Tanya
Telerik team
answered on 26 Jan 2016, 08:00 AM
Hello Nisarg,

This is a low-level exception in NET Framework and could be just skipped. If you continue the execution of the application, everything should work as expected. In order to suppress this exception you could go to Debug -> Exceptions and uncheck the check box for CLR Exceptions.

You could find a similar discussion in this thread on StackOverflow.

Regards,
Tanya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Nisarg
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Nisarg
Top achievements
Rank 1
Share this question
or