Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
523
ERROR? On printing report
posted

First of all, sorry for my english. I'm using 12.1 and winforms for viewing my reports, I have a MDI application and from a child the system launch a dialog windows with a report viewer, the report renders on the form load and when the ReportProcessCompleted event fires I try to print the report but theres is always the error "An error occurred while trying to retrieve the printer HP Deskjet 2050 J510 series." My printer is on and working fine but I cannot use it for printing the reports. After the error I tried to print the report from the toolbar but there were another error (attachment Error.png):

System.InvalidOperationException: El subproceso que realiza la llamada no puede obtener acceso a este objeto porque el propietario es otro subproceso.
   en System.Windows.Threading.Dispatcher.VerifyAccess()
   en System.Printing.PrintQueue.VerifyAccess()
   en System.Printing.PrintQueue.get_FullName()
   en MS.Internal.Printing.Win32PrintDialog.PrintDlgExMarshaler.AllocateUnmanagedPrintDlgExStruct()
   en MS.Internal.Printing.Win32PrintDialog.PrintDlgExMarshaler.SyncToStruct()
   en MS.Internal.Printing.Win32PrintDialog.ShowDialog()
   en System.Windows.Controls.PrintDialog.ShowDialog()
   en Infragistics.Controls.Reports.ReportPrintService.PrintWithDialog(PageSettingsData pageSettingsData)

And this is my code

private void repViewer_ProcessingCompleted(object sender, Infragistics.Reports.ProcessingCompletedEventArgs e)
        {
            string printerName = "";
            try
            {
                LocalPrintServer localPrintServer = new LocalPrintServer();
                PrintQueue defaultPrintQueue = localPrintServer.DefaultPrintQueue;
                printerName = defaultPrintQueue.Name;
                PageSettings currentPageSettings = repViewer.GetCurrentPageSettings();
                PaperSettings paperSettings = new PaperSettings(currentPageSettings.PaperSize, currentPageSettings.PageOrientation);
                repViewer.Print(paperSettings, printerName, PageRange.All);
            }
            catch (Exception)
            {
                msgInfo.Caption = ManejadorRecursos.ObtenerCadena("comun.Error");
                msgInfo.Text = string.Format(ManejadorRecursos.ObtenerCadena("comun.ErrorImpresion"), printerName);
                msgInfo.Buttons = MessageBoxButtons.OK;
                msgInfo.Icon = MessageBoxIcon.Warning;

                UltraMessageBoxManager.ShowMessageBox(msgInfo);
            }
        }

How can I solve this?

Thanks in advance for the help.

  • 523
    Verified Answer
    posted

    Nevermind, it was a thread issue I solvede it using SynchrinzationContext. My solution:

    private void frmFactura_Load(object sender, EventArgs e)

    {
        sincronizacion = SynchronizationContext.Current;
    }

    private void repViewer_ProcessingCompleted(object sender, Infragistics.Reports.ProcessingCompletedEventArgs e)
    {
        sincronizacion.Post(Imprimir, sender);
    }

    private void Imprimir(object sender)
    {
        string printerName = "";
        try
       {
          LocalPrintServer localPrintServer = new LocalPrintServer();
          PrintQueue defaultPrintQueue = localPrintServer.DefaultPrintQueue;
          printerName = defaultPrintQueue.Name;
          PageSettings currentPageSettings = repViewer.GetCurrentPageSettings();
          PaperSettings paperSettings = new PaperSettings(currentPageSettings.PaperSize, currentPageSettings.PageOrientation);
          repViewer.Print(paperSettings, printerName, PageRange.All);
       }
       catch (Exception)
       {
          msgInfo.Caption = ManejadorRecursos.ObtenerCadena("comun.Error");
          msgInfo.Text = string.Format(ManejadorRecursos.ObtenerCadena("comun.ErrorImpresion"), printerName);
          msgInfo.Buttons = MessageBoxButtons.OK;
          msgInfo.Icon = MessageBoxIcon.Warning;

          UltraMessageBoxManager.ShowMessageBox(msgInfo);
       }
    }