private void WaitForJobToComplete(string printerName, int jobID, out string status)
        {
            status = string.Empty;
            SampleCodeGraphics g       = null;
            ZBRPrinter         printer = null;

            try
            {
                int errValue = 0;
                g       = new SampleCodeGraphics();
                printer = new ZBRPrinter();

                while (true)
                {
                    bool ready = g.IsPrinterReady(printerName, 60, out status);
                    if (!ready && string.IsNullOrEmpty(status))
                    {
                        if (printer.IsPrinterInErrorMode(out errValue))
                        {
                            status = "Printer in error mode: " + Convert.ToString(errValue);
                            break;
                        }
                    }
                    else if (ready) //print job completed
                    {
                        if (printer.IsPrinterInErrorMode(out errValue))
                        {
                            status = "Printer in error mode: " + Convert.ToString(errValue);
                            break;
                        }
                        status = "Success";
                        break;
                    }
                    else if (!string.IsNullOrEmpty(status))
                    {
                        string temp = string.Empty;
                        g.CancelJob(printerName, jobID, out temp);
                        status += " " + temp;
                        break;
                    }

                    if (printer.IsPrinterInErrorMode(out errValue))
                    {
                        status = "Printer in error mode: " + Convert.ToString(errValue);
                        break;
                    }

                    Thread.Sleep(1000);
                }
            }
            catch (Exception e)
            {
                status = "WaitForJobToComplete exception: " + e.Message;
            }
            finally
            {
                g       = null;
                printer = null;
            }
        }
示例#2
0
        //public int PrintCard(PrintParameter pParas, out int err)
        //{
        //    SampleCodeGraphics prn = null;
        //    string msg = string.Empty;
        //    int jobID = 0;
        //    err = 0;
        //    try
        //    {
        //        if (EndPostionCard(1, 0, out err) == 1)
        //        {
        //            if (MoveToPrintLocation(out err) == 0)
        //            {
        //                return 0;
        //            }

        //            prn = new SampleCodeGraphics();
        //            if (IsPrinterReady(out err) == 1)
        //            {
        //                prn.PrintFrontSideOnly(pParas, printName,out jobID, out msg);
        //                if (string.IsNullOrEmpty(msg))
        //                {
        //                    WaitForJobToComplete(printName, jobID, out msg);
        //                }
        //            }

        //            LogHelper.WriteLog(typeof(ZBRPrinter), "PrintCard: " + msg);
        //        }
        //        return 1;
        //    }
        //    catch (System.Exception ex)
        //    {
        //        msg += ex.Message;
        //        MessageBox.Show(ex.ToString(), "PrintCard");
        //        LogHelper.WriteLog(typeof(ZBRPrinter), "PrintCard: " + msg);
        //        return 0;
        //    }
        //    finally
        //    {
        //        if (prn != null)
        //        {
        //            prn = null;
        //        }
        //    }


        //}


        public int PrintCard(PrintParameter pParas, out int err)
        {
            LogHelper.WriteLog(typeof(FormMain), "PrintCard");

            SampleCodeGraphics prn = null;
            string             msg = string.Empty;
            int jobID = 0;

            err = 0;
            try
            {
                LogHelper.WriteLog(typeof(ZBRPrinter), "EndPostionCard Begin");
                if (EndPostionCard(1, 0, out err) == 1)
                {
                    LogHelper.WriteLog(typeof(ZBRPrinter), "EndPostionCard 成功");

                    LogHelper.WriteLog(typeof(ZBRPrinter), "MoveToPrintLocation Begin");

                    if (MoveToPrintLocation(out err) == 0)
                    {
                        LogHelper.WriteLog(typeof(ZBRPrinter), "MoveToPrintLocation 失败: err +" + err);
                        return(0);
                    }

                    LogHelper.WriteLog(typeof(ZBRPrinter), "MoveToPrintLocation 成功: err +" + err);

                    prn = new SampleCodeGraphics();
                    if (IsPrinterReady(out err) == 1)
                    {
                        LogHelper.WriteLog(typeof(ZBRPrinter), "IsPrinterReady 成功: err +" + err);
                        prn.PrintFrontSideOnly(pParas, printName, out jobID, out msg);

                        LogHelper.WriteLog(typeof(ZBRPrinter), "PrintFrontSideOnly: msg +" + msg);

                        if (string.IsNullOrEmpty(msg))
                        {
                            WaitForJobToComplete(printName, jobID, out msg);
                        }

                        return(1);
                    }
                    else
                    {
                        LogHelper.WriteLog(typeof(ZBRPrinter), "IsPrinterReady 失败: err +" + err);
                        return(0);
                    }
                }
                else
                {
                    LogHelper.WriteLog(typeof(ZBRPrinter), "EndPostionCard 失败: err +" + err);
                    return(0);
                }
            }
            catch (System.Exception ex)
            {
                msg += ex.Message;
                MessageBox.Show(ex.ToString(), "PrintCard");
                LogHelper.WriteLog(typeof(ZBRPrinter), "PrintCard: " + msg);
                return(0);
            }
            finally
            {
                if (prn != null)
                {
                    prn = null;
                }
            }
        }