Solved

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

Posted on 2013-06-25
2
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39277120
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
ID: 39280257
Spot on. Thanks for the quick response.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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