1010namespace EventHook . Hooks
1111{
1212 /// <summary>
13- /// // http://www.codeproject.com/Articles/51085/Monitor-jobs-in-a-printer-queue-NET
13+ /// http://www.codeproject.com/Articles/51085/Monitor-jobs-in-a-printer-queue-NET
1414 /// </summary>
1515 internal class PrintJobChangeEventArgs : EventArgs
1616 {
@@ -42,56 +42,39 @@ internal PrintSystemJobInfo JobInfo
4242 get { return _jobInfo ; }
4343 }
4444
45- #region private variables
4645
4746 private readonly int _jobId ;
4847 private readonly string _jobName ;
4948 private readonly JOBSTATUS _jobStatus ;
5049 private readonly PrintSystemJobInfo _jobInfo ;
5150
52- #endregion
5351 }
5452
5553 internal delegate void PrintJobStatusChanged ( object sender , PrintJobChangeEventArgs e ) ;
5654
5755 internal class PrintQueueHook
5856 {
59- #region Constants
6057
6158 private const int PRINTER_NOTIFY_OPTIONS_REFRESH = 1 ;
6259
63- #endregion
64-
65- #region constructor
66-
6760 internal PrintQueueHook ( string strSpoolName )
6861 {
6962 // Let us open the printer and get the printer handle.
7063 SpoolerName = strSpoolName ;
7164 }
7265
73- #endregion
74-
75- #region Events
76-
7766 internal event PrintJobStatusChanged OnJobStatusChange ;
7867
79- #endregion
80-
81- #region destructor
82-
8368 ~ PrintQueueHook ( )
8469 {
8570 Stop ( ) ;
8671 }
8772
88- #endregion
89-
90- #region StartMonitoring
9173
9274 internal void Start ( )
9375 {
9476 OpenPrinter ( SpoolerName , out _printerHandle , 0 ) ;
77+
9578 if ( _printerHandle != IntPtr . Zero )
9679 {
9780 //We got a valid Printer handle. Let us register for change notification....
@@ -112,10 +95,6 @@ internal void Start()
11295 }
11396 }
11497
115- #endregion
116-
117- #region StopMonitoring
118-
11998 internal void Stop ( )
12099 {
121100 try
@@ -132,15 +111,10 @@ internal void Stop()
132111 }
133112 }
134113
135- #endregion
136-
137- #region Callback Function
138-
139114 internal void PrinterNotifyWaitCallback ( object state , bool timedOut )
140115 {
141116 if ( _printerHandle == IntPtr . Zero ) return ;
142117
143- #region read notification details
144118
145119 _notifyOptions . Count = 1 ;
146120 var pdwChange = 0 ;
@@ -159,11 +133,8 @@ internal void PrinterNotifyWaitCallback(object state, bool timedOut)
159133 PRINTER_CHANGES . PRINTER_CHANGE_DELETE_JOB ) ||
160134 ( ( pdwChange & PRINTER_CHANGES . PRINTER_CHANGE_WRITE_JOB ) ==
161135 PRINTER_CHANGES . PRINTER_CHANGE_WRITE_JOB ) ;
162- if ( ! bJobRelatedChange ) return ;
163136
164- #endregion
165-
166- #region populate Notification Information
137+ if ( ! bJobRelatedChange ) return ;
167138
168139 //Now, let us initialize and populate the Notify Info data
169140 var info = ( PRINTER_NOTIFY_INFO ) Marshal . PtrToStructure ( pNotifyInfo , typeof ( PRINTER_NOTIFY_INFO ) ) ;
@@ -176,9 +147,6 @@ internal void PrinterNotifyWaitCallback(object state, bool timedOut)
176147 pData += Marshal . SizeOf ( typeof ( PRINTER_NOTIFY_INFO_DATA ) ) ;
177148 }
178149
179- #endregion
180-
181- #region iterate through all elements in the data array
182150
183151 for ( var i = 0 ; i < data . Count ( ) ; i ++ )
184152 {
@@ -214,19 +182,12 @@ internal void PrinterNotifyWaitCallback(object state, bool timedOut)
214182 }
215183 }
216184
217- #endregion
218-
219- #region reset the Event and wait for the next event
220185
221186 _mrEvent . Reset ( ) ;
222187 _waitHandle = ThreadPool . RegisterWaitForSingleObject ( _mrEvent , PrinterNotifyWaitCallback , _mrEvent , - 1 , true ) ;
223-
224- #endregion
225188 }
226189
227- #endregion
228190
229- #region DLL Import Functions
230191
231192 [ DllImport ( "winspool.drv" , EntryPoint = "OpenPrinterA" , SetLastError = true , CharSet = CharSet . Ansi ,
232193 ExactSpelling = true , CallingConvention = CallingConvention . StdCall ) ]
@@ -253,16 +214,6 @@ internal static extern IntPtr FindFirstPrinterChangeNotification
253214 [ In ] int fwOptions ,
254215 [ In , MarshalAs ( UnmanagedType . LPStruct ) ] PRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions ) ;
255216
256- //[DllImport("winspool.drv", EntryPoint = "FindNextPrinterChangeNotification",
257- // SetLastError = true, CharSet = CharSet.Ansi,
258- // ExactSpelling = false,
259- // CallingConvention = CallingConvention.StdCall)]
260- //internal static extern bool FindNextPrinterChangeNotification
261- // ([In] IntPtr hChangeObject,
262- // [Out] out int pdwChange,
263- // [In, MarshalAs(UnmanagedType.LPStruct)] PRINTER_NOTIFY_OPTIONS pPrinterNotifyOptions,
264- // [Out] out IntPtr lppPrinterNotifyInfo
265- // );
266217
267218 [ DllImport ( "winspool.drv" , EntryPoint = "FindNextPrinterChangeNotification" ,
268219 CallingConvention = CallingConvention . StdCall , SetLastError = true ) ]
@@ -274,9 +225,6 @@ [In] PRINTER_NOTIFY_OPTIONS
274225 ref IntPtr ppPrinterNotifyInfo ) ;
275226
276227
277- #endregion
278-
279- #region private variables
280228
281229 private IntPtr _printerHandle = IntPtr . Zero ;
282230 internal string SpoolerName ;
@@ -287,6 +235,5 @@ [In] PRINTER_NOTIFY_OPTIONS
287235 private readonly Dictionary < int , string > _objJobDict = new Dictionary < int , string > ( ) ;
288236 private PrintQueue _spooler ;
289237
290- #endregion
291238 }
292239}
0 commit comments