Exemplo n.º 1
0
        private PrintManager(String name, int remainingPrints)
        {
            _thisPrintBatch        = NullPrintBatch.Instance;
            _remainingPrintsInTray = remainingPrints;
            _doPrintSignal         = new ManualResetEvent(false);
            _killSignal            = new ManualResetEvent(false);


            Thread printWorker = new Thread(() =>
            {
                SetPrinterSearchString(name);
                FindPrinter();
                _thisPrinter.Refresh();

                while (!_killSignal.WaitOne(0))
                {
                    if (_doPrintSignal.WaitOne(0))
                    {
                        _doPrintSignal.Reset();
                        Print();
                    }
                }
            });

            printWorker.SetApartmentState(ApartmentState.STA);
            printWorker.Start();
        }
Exemplo n.º 2
0
        private bool EnQueuePrintBatch(IPrintBatch batch)
        {
            bool doPrint    = false;
            bool addedBatch = false;

            if (batch.Template.CanAddMoreImages())
            {
                throw new Exception("Cannot print a print batch before it has all of the its images");
            }

            lock (_queueLock)
            {
                if (!_printQueue.Contains(batch))
                {
                    _printQueue.Enqueue(batch);
                    addedBatch = true;

                    if (_printQueue.Count > 0)
                    {
                        doPrint = true;
                    }
                }
            }
            if (doPrint)
            {
                _doPrintSignal.Set();
            }

            return(addedBatch);
        }
Exemplo n.º 3
0
        // TODO: Make this thread safe
        private void Print()
        {
            try
            {
                _thisPrinter.Refresh();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }

            // Get our current print Batch(if we don't already have it)
            if (_thisPrintBatch.BatchFinishedPrinting())
            {
                _thisPrintBatch = DeQueuePrintBatch();
            }

            if (!_thisPrintBatch.BatchFinishedPrinting())
            {
                try
                {
                    // See if our printer is in an error state
                    if (CheckPrinterForErrorState())
                    {
                        _inError = true;
                    }
                    else
                    {
                        FixedPage page = (_thisPrintBatch.Template.Clone() as PrintTemplate).Render();
                        _thisPrinter.UserPrintTicket.PageBorderless      = PageBorderless.Borderless;
                        _thisPrinter.UserPrintTicket.PhotoPrintingIntent = PhotoPrintingIntent.PhotoBest;
                        _thisPrinter.UserPrintTicket.PageMediaSize       = new PageMediaSize(4, 6);
                        _thisPrinter.Commit();
                        XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(_thisPrinter);

                        writer.Write(page);

                        // Pretend we know that this print worked and tell our current print batch that it did.
                        _thisPrintBatch.RegisterSucessfullPrint();

                        _remainingPrintsInTray--;

                        // Print the next page (eventually this should run out of batches or a print error will happen...)
                        Print();
                    }
                }
                catch (Exception ex)
                {
                    _printErrors = ex.Message;
                    _inError     = true;
                }
            }

            if (_inError)
            {
                InformClientOfPrintProblems(_printErrors);
            }
        }