Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:27:23+00:00 2026-05-13T20:27:23+00:00

I have a need for users to be able to scan a series of

  • 0

I have a need for users to be able to scan a series of items and for each item print off x number of labels. I am currently trying to use a background worker to accomplish this but I have run into a problem where they are scanning items so fast and there are so many labels to print for each item the background worker chokes. This is how I am generating the background worker thread for each scan because contention was occurring when there was a large number of labels be printed.

 private void RunPrintWorker()
    {
        if (printWorker.IsBusy)
        {
            printWorker = new BackgroundWorker();
            printWorker.DoWork += new DoWorkEventHandler(printWorker_DoWork);
            printWorker.RunWorkerAsync();
        }
        else
            printWorker.RunWorkerAsync();
    }

I don’t get any exceptions from the background worker it just seems to not be creating threads fast enough. I am newer to using multiple threads so can anyone point me in the direction of what I am doing wrong?

Thanks.

EDIT: Thanks everyone for the suggestions and reading material this should really help. The order the labels are being printed doesn’t really matter since they are scanning pretty fast and the labels are only being printed to one printer too. I will mark an answer after I get the implementation up and running.

EDIT: Austin, below is how I have my printing method setup. Before I was just calling LabelPrinter.PrintLabels in my RunPrintWorker method. Now that I am redoing this I can’t figure out what to pass into the SizeQueue method. Should I be passing the newly created print document into it?

 public class LabelPrinter
{
    private int CurrentCount = 0;

    private List<int> _selectedRows = new List<int>();
    public List<int> SelectedRows
    {
        get { return _selectedRows; }
        set { _selectedRows = value; }
    }

    private string _selectedTemplate;
    public string SelectedTemplate
    {
        get { return _selectedTemplate; }
        set { _selectedTemplate = value; }
    }

    private string _templateDirectory = string.Empty;
    public string TemplateDirectory
    {
        get { return _templateDirectory; }
        set { _templateDirectory = value; }
    }

    public void PrintLabels(PrintDocument printDoc, PageSettings pgSettings, PrinterSettings printerSettings, List<int> selectedRows, string selectedTemplate, string templateDir)
    {
        this._selectedRows = selectedRows;
        this._selectedTemplate = selectedTemplate;
        this._templateDirectory = templateDir;

        printDoc.DefaultPageSettings = pgSettings;
        printDoc.PrinterSettings = printerSettings;

        printDoc.PrinterSettings.MaximumPage = selectedRows.Count();
        printDoc.DefaultPageSettings.PrinterSettings.ToPage = selectedRows.Count();
        printDoc.PrinterSettings.FromPage = 1;

        printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

        printDoc.Print();
    }

    private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        CurrentCount = DrawLabel.DrawLabelsForPrinting(e, SelectedTemplate, SelectedRows, CurrentCount, TemplateDirectory);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:27:23+00:00Added an answer on May 13, 2026 at 8:27 pm

    Try adding the items to a queue (for example, Queue<Item>) and have the BackgroundWorker process the queue.

    EDIT: Adding some simple, untested code that may work for you. I would encapsulate the print queue with its processor and just send it jobs.

    class SimpleLabelPrinter
    {
        public bool KeepProcessing { get; set; }
        public IPrinter Printer { get; set; }
    
        public SimpleLabelPrinter(IPrinter printer)
        {
            Printer = printer;
        }
    
    
        /* For thread-safety use the SizeQueue from Marc Gravell (SO #5030228) */        
        SizeQueue<string> printQueue = new SizeQueue<string>();
    
        public void AddPrintItem(string item)
        {
            printQueue.Enqueue(item);
        }
    
        public void ProcessQueue()
        {
            KeepProcessing = true;
    
            while (KeepProcessing)
            {
                while (printQueue.Count > 0)
                {
                    Printer.Print(printQueue.Dequeue());
                }
    
                Thread.CurrentThread.Join(2 * 1000); //2 secs
            }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            SimpleLabelPrinter printer1 = new SimpleLabelPrinter(...);
            SimpleLabelPrinter printer2 = new SimpleLabelPrinter(...);
    
            Thread printer1Thread = new Thread(printer1.ProcessQueue);
            Thread printer2Thread = new Thread(printer2.ProcessQueue);
    
            //...
    
            printer1.KeepProcessing = false;  //let the thread run its course...
            printer2.KeepProcessing = false;  //let the thread run its course...
        }
    }
    

    SizeQueue implementation

    EDIT 2: Addressing the updated code in the question

    First, I would define a PrintJob class that contains the number of copies to print and either the complete label text or enough data to derive it (like IDs for a DB query). This would lead you to replace SizeQueue<string> in my code above to SizeQueue<PrintJob> as well as AddPrintItem(string item) to AddPrintJob(PrintJob job).

    Second, I would keep your LabelPrinter code separated (perhaps create that IPrinter interface) and pass that into the constructor of my SimpleLabelPrinter (which may not be the best name at this point but I’ll let you handle that).

    Next, create your LabelPrinter and SimpleLabelPrinter (say printer1 for this example) wherever it’s appropriate for your app (in your apps Closing or “cleanup” method, be sure to set KeepProcessing to false so its thread ends).

    Now when you scan an item you send it to the SimpleLabelPrinter as:

    printer1.AddPrintJob(new PrintJob(labelText, numberOfCopies));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that normal users need to be able to run, but
I have a score i need to calculate for multiple items for multiple users.
I have a need to be able to allow users to export their .doc
I have a Menu that contains a TreePanel. The users need to be able
I have a page with an editable table. I need users to be able
I have a canvas that i need the users to be able to paste
I have a need to be able to identify all of the users on
i have an access front end. users need to be able to open a
I have the need to create the functionality to allow my registered users to
I have a feature matrix implemented with Silverlight's Grid where users need to select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.