예제 #1
0
        private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            PrinterSettings  printerSettings = new PrinterSettings();
            LocalPrintServer printerServer   = new LocalPrintServer();

            while (!backgroundWorker.CancellationPending)
            {
                //Windows check
                using (var searcher = new ManagementObjectSearcher
                                          ("SELECT * FROM WIN32_Printer"))
                {
                    var printers = searcher.Get().Cast <ManagementBaseObject>().Where(p => p["DriverName"].Equals("MS-D347")).ToList();

                    var defaultPrinterName = printerSettings.PrinterName;

                    var defaultPrinter = printers.Where(p => p["Name"].Equals(defaultPrinterName)).FirstOrDefault();

                    bool Offline = (bool)defaultPrinter["WorkOffline"];

                    if (!Offline)
                    {
                        //Printer online
                        //Print Job != 0 error
                        var queue = printerServer.DefaultPrintQueue;

                        if (queue.NumberOfJobs != 0)
                        {
                            StatusChanged(MSD347SDK.Status.PRINTER_JOBS_QUEUE_NOT_EMPTY, "Jobs: " + queue.NumberOfJobs, Source.System);
                        }

                        //SDK Init
                        if (CheckBySdk)
                        {
                            if (SDKLastStatus.Equals(MSD347SDK.Status.PRINTER_IS_OFFLINE))
                            {
                                try
                                {
                                    //if (MSD347SDK.SetClean() != 0)
                                    //{
                                    //    Debug.WriteLine("SetClean error");
                                    //}

                                    if (MSD347SDK.SetClose() != 0)
                                    {
                                        Debug.WriteLine("SetClose error");
                                    }

                                    if (MSD347SDK.SetUsbportauto() != 0)
                                    {
                                        Debug.WriteLine("SetUsbportauto error");
                                    }

                                    if (MSD347SDK.SetInit() != 0)
                                    {
                                        Debug.WriteLine("SetInit error");
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine("Exception: {0}", ex.ToString());
                                }
                            }

                            //SDK status
                            var statusCode = MSD347SDK.GetStatus();
                            SDKLastStatus = MSD347SDK.GetStatusByCode(statusCode);

                            if (SDKLastStatus.Equals(MSD347SDK.Status.PRINTER_IS_READY))
                            {
                                StatusChanged(MSD347SDK.Status.PRINTER_IS_READY, "", Source.System);
                            }
                            else
                            {
                                StatusChanged(SDKLastStatus, "", Source.SDK);
                            }
                        }


                        Debug.WriteLine("{0} online, status by sdk: {1}", defaultPrinter["Name"], SDKLastStatus);
                    }
                    else
                    {
                        StatusChanged(MSD347SDK.Status.PRINTER_IS_OFFLINE, "", Source.System);

                        Debug.WriteLine("{0} offline", defaultPrinter["Name"]);

                        if (AutoDetectDriverOnline)
                        {
                            foreach (var p in printers)
                            {
                                if (!(bool)p["WorkOffline"])
                                {
                                    var Name = p["Name"].ToString();
                                    SetDefaultPrinter(Name);
                                    StatusChanged(MSD347SDK.Status.PRINTER_DRIVER_CHANGED, "", Source.System);
                                    Debug.WriteLine("{0} is set as the default printer", Name);
                                }
                            }
                        }
                    }
                }

                Thread.Sleep(500);
            }
        }
예제 #2
0
            private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                StatusChanged(Status.WORKER_STARTED, "");

                while (!backgroundWorker.CancellationPending)
                {
                    //SDK Check
                    if (SDKLastStatus.Equals(MSD347SDK.Status.PRINTER_IS_OFFLINE))
                    {
                        try
                        {
                            if (MSD347SDK.SetClean() != 0)
                            {
                                StatusChanged(Status.SDK_CLEAN_ERROR, "");
                            }

                            if (MSD347SDK.SetClose() != 0)
                            {
                                StatusChanged(Status.SDK_CLOSE_ERROR, "");
                            }

                            if (AutoUsbport)
                            {
                                if (MSD347SDK.SetUsbportauto() != 0)
                                {
                                    StatusChanged(Status.SDK_SET_USB_AUTO_PORT_ERROR, "");
                                }
                            }
                            else
                            {
                                if (MSD347SDK.SetPrintport(UsbPort, Baudrate) != 0)
                                {
                                    StatusChanged(Status.SDK_SET_PRINT_PORT_ERROR, "");
                                }
                            }

                            if (MSD347SDK.SetInit() != 0)
                            {
                                StatusChanged(Status.INIT_PRINTER_ERROR, "");
                            }
                        }
                        catch (Exception ex)
                        {
                            StatusChanged(Status.INIT_PRINTER_ERROR, ex.ToString());
                        }
                    }

                    var statusCode = MSD347SDK.GetStatus();

                    //if (!LastStatus.Equals(MSD347SDK.GetStatusByCode(statusCode)))
                    //{
                    //    PrinterStatusChanged(MSD347SDK.GetStatusByCode(statusCode), "");
                    //    LastStatus = MSD347SDK.GetStatusByCode(statusCode);
                    //}

                    PrinterStatusChanged(MSD347SDK.GetStatusByCode(statusCode), "");
                    SDKLastStatus = MSD347SDK.GetStatusByCode(statusCode);

                    Thread.Sleep(1000);
                }
            }