C# .NET - generating 200000 datamatrix 2d barcode take too much time how to less time

Asked By ahmed elbarbary on 08-May-17 07:43 PM

I make windows form application c# vs 2015 to generate 2d matrix bar codes for pharmaceutical company
manufacture medicine .
my problem when i make order to generate 2d matrix barcode for 200000 barcode and save barcodes in hard disk
take 20 minutes
How to minimize 20 minutes to 8 minutes when generate and saving 200000 barcode ?
my code under button as following

I attached serial and library for data matrix encoding with message

 Class1 CLS = new Class1();
    DataTable dt = CLS.ShowalldataSerial(textBox4.Text);
     string baseText = "UserID" + dt.Rows[0][0] + "FirmName" + dt.Rows[0][1] + "OrderNo" + dt.Rows[0][2] + "BtachNo" + dt.Rows[0][3] + "Quantity" + dt.Rows[0][4] + "ProductName" + dt.Rows[0][5] + "SerialNo";
        Color foregroundColor = Color.FromName(comboBox1.SelectedItem.ToString());
        int serialsToGenerate = Convert.ToInt32(textBox1.Text);
        Parallel.For(0, serialsToGenerate, index=>
        {
            string Serial = SRL.Rnd().ToString();
            string txt = baseText + Serial;
            // WARNING HERE
            DM_Encoder dm = new DM_Encoder();
            dm.DM(txt, foregroundColor, Color.White).Save(root + "\\" + Serial + ".emf", System.Drawing.Imaging.ImageFormat.Emf);
        });

Library used for data matrix

http://www.mediafire.com/file/ciadoloynverba7/library+used+for+datamatrix.txt

Robbe Morris replied to ahmed elbarbary on 09-May-17 01:27 PM
That is a VB.NET library and I don't see anywhere in the code where it disposes of graphics resources when it is done with them  Assuming I'm not mistaken, I would think there would be memory issues all over the place.

If you are not able to fix that on your own, you could try using multiple threads to draw each file:

http://nullskull.com/faq/1488/threadpoolqueueuserworkitem-multithreading.aspx