/// <summary> /// 清理打印机队列中的JOb /// </summary> public void ClearJobs() { //清空打印机里残留的队列 PrintServer ps = new PrintServer(); PrintQueue queue = ps.GetPrintQueue(ConfigurationManager.AppSettings["printer"]); queue.Refresh(); PrintJobInfoCollection allPrintJobs = queue.GetPrintJobInfoCollection(); foreach (PrintSystemJobInfo printJob in allPrintJobs) { printJob.Cancel(); } //释放资源 ps.Dispose(); queue.Dispose(); allPrintJobs.Dispose(); }
public void Dispose() { _printDocument?.Dispose(); _printServer?.Dispose(); _printQueue?.Dispose(); }
/// <summary> /// 状态检测方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void stadtimer_Tick(object sender, EventArgs e) { System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions(); options.DontFragment = true; string data = "Test Data!"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 2000; // Timeout 时间,单位:毫秒 //检查网络连接 try { System.Net.NetworkInformation.PingReply reply = p.Send("baidu.com", timeout, buffer, options); if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) { stat.line = true; netLine.Text = "网络已连接"; netLine.Foreground = Brushes.Green; } else { stat.line = false; netLine.Text = "网络连接已断开"; netLine.Foreground = Brushes.Red; } } catch { stat.line = false; netLine.Text = "网络异常,请联系管理员"; netLine.Foreground = Brushes.Red; } if (stat.line == true) { //从网络获取机器状态 HttpBLL httpbll = new HttpBLL(); JSONBLL jsonbll = new JSONBLL(); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("id", ConfigurationManager.AppSettings["id"]); string str = httpbll.GetResponseString(httpbll.CreatePostHttpResponse(ConfigurationManager.AppSettings["machine"], dic)); if (str != null) { JObject jo = JsonConvert.DeserializeObject <JObject>(str); if (jo["code"].ToString() == "200") { JObject jo1; string str1 = jo["machine"].ToString(); if (str1 != null) { jsonbll.jsonToJobject(str1, out jo1); stat.stat = jo1["nowStatus"].ToString(); } else { stat.stat = "查无此机,若要正常使用,请检查配置文件与服务器"; } } else { stat.stat = "意外错误,未能与服务器正常通信"; } } else { stat.stat = "意外错误,通信失败"; } } else { stat.stat = "连接失败"; } pageRemain.Text = App.set.remainPageNum.ToString(); try { PrintServer ps = new PrintServer(); PrintQueue queue = ps.GetPrintQueue(ConfigurationManager.AppSettings["printer"]); printerStaTxt.Text = queue.QueueStatus.ToString(); ps.Dispose(); queue.Dispose(); } catch { printerStaTxt.Text = "打印服务未启动"; } }
static private bool PrintDoc2(List <FiscalCheckVisualString> Args, int TryCount, string printerName) { int W = 268; ctrlCheckVisual vis = new ctrlCheckVisual(); logger.Error($"PrintDoc2 printerName: {printerName}"); if (Args != null) { // BitmapImage QrImg = FiscalCheckCreator.CreateQRBitmap(Args.QRAsStr, 130, 130); vis.CreateCheck(Args); vis.Visibility = Visibility.Visible; // string PrName = @"Predchek4"; //string PrName = iniFile.FRSPrinterName; try { // Utils.ToCardLog("PrintDoc PrName " + PrName); PrintDialog Pd = new PrintDialog(); Pd.PageRangeSelection = PageRangeSelection.AllPages; PrintServer Ps = new PrintServer(); PrintQueue PQ = new PrintQueue(Ps, printerName); Pd.PrintQueue = PQ; // Pd.ShowDialog(); PrintTicket Pt = Pd.PrintTicket; Pt.PageMediaSize = new PageMediaSize(W, 11349); Pt.PageBorderless = PageBorderless.Borderless; Pt.PageResolution = new PageResolution(203, 203); Pt.PageScalingFactor = 1; Pt.TrueTypeFontMode = TrueTypeFontMode.DownloadAsRasterFont; Size pageSize = new Size(W - 10, Pd.PrintableAreaHeight); //pageSize = new Size(W, H); ((UserControl)vis).Measure(pageSize); ((UserControl)vis).Arrange(new Rect(0, 0, W - 10, ((UserControl)vis).Height)); //((UserControl)vis).Arrange(new Rect(0, 0, W, H)); Pd.PrintVisual(vis, "Hello"); Ps.Dispose(); Pd.PrintQueue.Dispose(); Pd = null; } catch (Exception e) { // Utils.ToCardLog("PrintDoc Error " + e.Message); logger.Error("PrintDoc Error " + e.Message); if (TryCount < 5) { logger.Error("Try again " + TryCount); System.Threading.Thread.Sleep(300); GC.Collect(); return(PrintDoc2(Args, TryCount + 1, printerName)); } return(false); } } else { logger.Debug("PrintDoc2 Args==null"); } vis = null; GC.Collect(); logger.Debug("PrintDoc2 Ok"); return(true); }
public override void Start() { _wait.Reset(); _inQueue = false; PrintServer PrintS = new PrintServer(); PrintQueue queue = new PrintQueue(PrintS, _window.PrintInformation.PrinterSettings.PrinterName); int trial = 0; do { trial++; queue.Refresh(); if (trial >= 3000 && trial < 6000) { RefreshList(_document); _window.Dispatcher.Invoke(new Action(() => { _stopWindow.skipVisibility(System.Windows.Visibility.Visible); })); SetStatus(_document, State.Searching); } else if (trial >= 6000) { SetStatus(_document, State.StillSearching); } try { if (queue.NumberOfJobs > 0) { using (PrintSystemJobInfo job = queue.GetPrintJobInfoCollection().Last()) { if (job.Name.Contains(_document.Name)) { _inQueue = true; SetStatus(_document, State.InQueue); log.Info(_document.Name + " has been queued"); } } } } catch (NullReferenceException) { } catch (RuntimeWrappedException) { } catch (InvalidOperationException) { } } while (!_inQueue && trial < 10000 && !_token.IsCancellationRequested && !_stopWindow.Skip); PrintS.Dispose(); queue.Dispose(); if (trial >= 10000) { SetStatus(_document, State.Error); log.Error(_document.Name + " has made an error"); if (!_window.Configuration.AutoRetry) { System.Media.SystemSounds.Beep.Play(); _window.Dispatcher.Invoke(new Action(() => { _stopWindow.retryVisibility(System.Windows.Visibility.Visible); _window.Activate(); _stopWindow.Activate(); })); _stopWindow.Retry.Reset(); _stopWindow.Retry.WaitOne(); } } if (_stopWindow.Skip) { SetStatus(_document, State.Skipped); log.Info(_document.Name + " has been skipped"); } }