none
Multithreading in IIS RRS feed

  • Question

  • Hi,

    I am trying to design and implement a label printing solution whereby users will call a WCF service to print labels to different label printers. Now some of these printers are quite slow and I have to wait a certain amount of time before sending the request to them, whilst others I can just send without checking.

    The problem I have is that I have created a ThreadPool.QueueWorkItem and passed in my label data to be processed by the thread. This works fine.

    How can I return back a success/fail flag from the thread created by the threadpool back through the WCF and to the user?

            public ConcurrentDictionary<string, NeoPrinterQueue> PrinterDictionary = new ConcurrentDictionary<string, NeoPrinterQueue>();
            public static object LockObj = new object();

            /// <summary>
            /// Print Thermal label using Neodynamics thermal label sdk
            /// </summary>
            /// <param name="labelData"></param>
            public void PrintLabel(ThermalLabelData labelData)
            {
                lock (LockObj)
                {
                    if (PrinterDictionary.ContainsKey(labelData.Printer.Address))
                    {
                        PrinterDictionary[labelData.Printer.Address].ThreadPoolQueueUserWorkItem(labelData);
                    }
                    else
                    {
                        var printerQueue = new NeoPrinterQueue();
                        printerQueue.ThreadPoolQueueUserWorkItem(labelData);
                        PrinterDictionary.TryAdd(labelData.Printer.Address, printerQueue);
                    }
                }
            }

    My idea for the above is that when a request comes in, it gets added to a ConcurrentDictionary and processed by the NeoPrinterQueue.

        public class NeoPrinterQueue
        {
            public void ThreadPoolQueueUserWorkItem(ThermalLabelData labelData)
            {            
                ThreadPool.QueueUserWorkItem(Print, labelData);
            }

            private static void Print(object a)
            {    
                var tLabel = (ThermalLabelData)a;
                try
                {                
                    if (tLabel != null) 
                        throw new Exception("Error converting to ThermalLabel");

                    tLabel.Label.GapLength = 0.6;
                    using (var pj = new PrintJob())
                    {
                        // Lets wait for a while for EPL printers so they dont clug up the printer memory
                        if (tLabel.Printer.Type == PrinterTypes.EPL) Thread.Sleep(4500);

                        pj.PrinterSettings = new PrinterSettings();
                        pj.PrinterSettings.Communication.CommunicationType = CommunicationType.Network;
                        pj.PrinterSettings.Dpi = 203;
                        pj.PrinterSettings.ProgrammingLanguage = (tLabel.Printer.Type == PrinterTypes.ZPL
                            ? ProgrammingLanguage.ZPL
                            : ProgrammingLanguage.EPL);
                        pj.PrinterSettings.Communication.NetworkIPAddress = System.Net.IPAddress.Parse(tLabel.Printer.Address);
                        pj.PrinterSettings.Communication.NetworkPort = 9100;

                        pj.Print(tLabel.Label);
                    }
                }
                catch (Exception exc)
                {
                    // return back to user
                }
            }
        }

    Any help greatfully appreciated.

    Tuesday, January 6, 2015 5:11 PM

Answers

  • How can I return back a success/fail flag from the thread created by the threadpool back through the WCF and to the user?

    I have seen the request/response pattern being used in WCF where a request object is sent by the client to the WCF service,  and a  response object is sent back to the client. The response objct would hold error messages and and flags.

    Tuesday, January 6, 2015 6:51 PM