public void BrowserPrint() { var browser = ((GeckoWebBrowser)_pdfViewerControl); using (AutoJSContext context = new AutoJSContext(browser.Window)) { nsIDOMWindow domWindow = browser.Window.DomWindow; nsIWebBrowserPrint print = Xpcom.QueryInterface <nsIWebBrowserPrint> (domWindow); try { if (PrintProgress != null) { // Send event to disable print, simple, outside cover and inside buttons // while printing PrintProgress.Invoke(this, new PdfPrintProgressEventArgs(true)); } _printing = true; print.Print(null, this); } catch (COMException e) { if (PrintProgress != null) { PrintProgress.Invoke(this, new PdfPrintProgressEventArgs(false)); } //NS_ERROR_ABORT means user cancelled the printing, not really an error. if (e.ErrorCode != GeckoError.NS_ERROR_ABORT) { throw; } } finally { Marshal.ReleaseComObject(print); } } }
private void HardWorkerMethod(int num, PrintProgress printProgress) { for (int i = 0; i < num; i++) { printProgress(i); Thread.Sleep(100); // thread will stop for 100 ms and go do other OS work } }
public void DelegatesAreAGoodWayToDecouple() { // lets say you have code to log the progress of something PrintProgress p = PrintProgressMethod; // essentially it is easy to pass in the code for the "PrintProgressMethod" here // therefore "HardWorkerMethod" only has to know the signature for the method it is calling // almost like programming to an interface for only one method HardWorkerMethod(10, p); }
public void OnStateChange(nsIWebProgress aWebProgress, nsIRequest aRequest, uint aStateFlags, int aStatus) { if (_printing && ((aStateFlags & STATE_STOP) != 0)) { _printing = false; if (PrintProgress != null) { PrintProgress.Invoke(this, new PdfPrintProgressEventArgs(false)); } } }
/// <summary> /// Prints Ticket collection /// </summary> private void ProcessPrint(List <Spot> spots) { if (spots.Count == 0) { return; } Spot spotBlank = spots.FirstOrDefault(x => x.Blank == null); if (spotBlank != null) { StatusText = OperationResult = "Недостаточно бланков для печати. Печать отменена"; return; } spots.Sort(); _printProgress = new PrintProgress { Message = "Идет печать билетов..." }; _printProgress.Cancel += CancelProcess; _worker = new BackgroundWorker { WorkerSupportsCancellation = true }; Dispatcher dispatcher = _progressDialog.Dispatcher; int ticketsCount = spots.Count; int printedCount = 0; _worker.DoWork += delegate(object s, DoWorkEventArgs args) { decimal percent = 100 / (ticketsCount); int progress = 0; for (int i = 1; i <= ticketsCount; i++) { if (_worker.CancellationPending) { args.Cancel = true; return; } Spot spot = spots[i - 1]; if (spot.Status == ItemStatus.Sold) { string msg = string.Format("{0} из {1} - печать билета № {2} на бланке {3}", i, ticketsCount, spot.Id, spot.Blank.Key); _updatePrintDelegate = UpdatePrintText; dispatcher.BeginInvoke(_updatePrintDelegate, progress, msg); byte[] bytes = GetTicketImage(Customer.Order.Id, spot.Id, 2).ToArray(); //Thread.Sleep(500); //bool result = _printRequest.Print(bytes); bool result = true; if (!result) { PrinterStatus = string.Format("Ошибка печати билета № {0} ", spot.Id); _worker.CancelAsync(); } else { Ticket ticket = new Ticket { Id = spot.Id, BlankId = spot.Blank.Id, Status = ItemStatus.Printed }; ticket = Access.SetTicketPrinted(ticket); if (ticket.BlankId == spot.Blank.Id) { spot.Status = ItemStatus.Printed; } else { //t.Status = ItemStatus.Printed; } } } progress = (int)(progress + percent); printedCount++; } }; _worker.RunWorkerCompleted += delegate { _printProgress.Close(); _worker = null; }; _worker.RunWorkerAsync(); _printProgress.ShowDialog(); StatusText = OperationResult = String.Format("Печать завершена. Напечатано {0} из {1} билетов.", printedCount, ticketsCount); BaseData.ClearCache(); }