Solved

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

Posted on 2013-06-25
2
492 Views
Last Modified: 2013-06-26
Hi:

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.

Thanks,
JohnB
0
Comment
Question by:jxbma
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
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)
        {
            CreatePrintJobsWatcher();
            Console.ReadLine();
        }

        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.Start();
            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)
            {
                try
                {
                    Console.WriteLine(p.Name + " : " + p.Value.ToString());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(p.Name + " : Unknown");
                }
            }
        }

Open in new window

0
 
LVL 1

Author Closing Comment

by:jxbma
Comment Utility
Spot on. Thanks for the quick response.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
optimize  c# code 7 48
Round a string to two digits 12 23
Problem to open Excel file 15 35
Achieve json result 2 20
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now