• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 509
  • Last Modified:

How do I catch printing errors in a .Net Winforms control hosted in a WPF application?


I have a WPF application, which hosts a custom control which wraps a 3rd party (DevExpress) winforms control.

From within the DevExpress control, I can print using (what I believe is the standard winforms printing dialog). It prints successfully.

However, if there are issues with the printer (not found, connected, turned on or in error state), I do not get notification of that error.
How do I catch these errors?
Are exceptions getting thrown somewhere that I am unaware of?

The client/users/customers would like to be notified of such states/conditions/errors.

1 Solution
Meir RivkinFull stack Software EngineerCommented:
this is the basic code i'm using to detect print jobs which their status is not OK.
basically whenever a print job is arraived, the event handler loop through the job PropertyData which store all the print job properties.
i can use this information to detect which job failed, what document was it, what kind of error, printer name and so on.
CreatePrintJobsWatcher, creates the event watcher on Win32_PrintJob, and objWatcher_EventArrived is the event handler which processes the data.

      static void Main(string[] args)

        private static void CreatePrintJobsWatcher()
            var objWatcher = new ManagementEventWatcher();
            objWatcher.Query = new EventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PrintJob'");
            objWatcher.Scope = new ManagementScope("\\root\\cimv2");
            objWatcher.EventArrived += new EventArrivedEventHandler(objWatcher_EventArrived);

        static void objWatcher_EventArrived(object sender, EventArrivedEventArgs e)
            ManagementBaseObject job = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;
            foreach (PropertyData p in job.Properties)
                    Console.WriteLine(p.Name + " : " + p.Value.ToString());
                catch (Exception ex)
                    Console.WriteLine(p.Name + " : Unknown");

Open in new window

jxbmaSoftware ConsultantAuthor Commented:
Spot on. Thanks for the quick response.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now