none
System.Printing in multi threaded environment cause weird font issue RRS feed

  • Question

  • Hello,

    We are experiencing font issues when using the System.printing namespace in a multi threaded environment. We are using VB.NET 4.0. We were able to prove that this was not a printer issue nor a driver issue. We were also able to prove that the issue is not with the .xps file itself. We ran the same files through a single threaded environment without any issues.

    The challenge is that it's not consistent. We printed 500 documents one day without reproducing the issue. But once in a while, a document will come out of the printer with some characters being replaced with random characters/symbols.

    An example of the issue is having a "6" instead of an "A" in "Time 6ctivity". Or having a symbol replacing my "e".

    We can't reproduce this issue using the following MSDN example in a single threaded environment:http://msdn.microsoft.com/en-us/library/ms552914.aspx

    But as soon as we try in a multi threaded environment, the issue occurs.

    Has anyone experienced this issue before? Would you be kind enough to share a solution?

    Here's a sample app used to reproduce the issue. This app will load three different set of .xps files located in three unique folders. It will then print to three unique printers. This is all done in a multi threaded environment:

    Imports System.Threading
    Imports System.Printing
    Imports System.IO
    
    Module Module1
    
        Sub Main(ByVal args() As String)
            Dim pt As printTest = New printTest()
        End Sub
    
    End Module
    
    Public Class printTest
    
        Public Property serverName As String
        Public Property printerName1 As String
        Public Property printerName2 As String
        Public Property printerName3 As String
    
        Public Property xpsPath1 As String
        Public Property xpsPath2 As String
        Public Property xpsPath3 As String
    
        Public Shared server1 As PrintServer
        Public Shared server2 As PrintServer
        Public Shared server3 As PrintServer
        Public Shared pq1 As PrintQueue
        Public Shared pq2 As PrintQueue
        Public Shared pq3 As PrintQueue
    
        Public Sub New()
    
            'Gather info about Printer Server and Printer Names
            '**************************************************
            Console.Write("Enter the server name or ip (ex: \\myserver):")
            serverName = Console.ReadLine()
    
            Console.Write("Enter the printer #1 name:")
            printerName1 = Console.ReadLine()
    
            Console.Write("Enter the printer #2 name:")
            printerName2 = Console.ReadLine()
    
            Console.Write("Enter the printer #3 name:")
            printerName3 = Console.ReadLine()
    
            'Gather info about the xps files location (three different folders / one folder for each thread)
            '***********************************************************************************************
            Console.Write("Enter the path to the XPS files #1:")
            xpsPath1 = Console.ReadLine
    
            Console.Write("Enter the path to the XPS files #2:")
            xpsPath2 = Console.ReadLine
    
            Console.Write("Enter the path to the XPS files #3:")
            xpsPath3 = Console.ReadLine
    
            'Start three seperate threads which will each print all xps files from a different folder using a unique printer
            '***************************************************************************************************************
            Dim _ProcessThread1 As Thread
            _ProcessThread1 = New Threading.Thread(AddressOf Print1)
            _ProcessThread1.Priority = ThreadPriority.Highest
            _ProcessThread1.SetApartmentState(Threading.ApartmentState.STA)
            _ProcessThread1.Start()
    
            Dim _ProcessThread2 As Thread
            _ProcessThread2 = New Threading.Thread(AddressOf Print2)
            _ProcessThread2.Priority = ThreadPriority.Highest
            _ProcessThread2.SetApartmentState(Threading.ApartmentState.STA)
            _ProcessThread2.Start()
    
            Dim _ProcessThread3 As Thread
            _ProcessThread3 = New Threading.Thread(AddressOf Print3)
            _ProcessThread3.Priority = ThreadPriority.Highest
            _ProcessThread3.SetApartmentState(Threading.ApartmentState.STA)
            _ProcessThread3.Start()
    
        End Sub
    
        Public Sub Print1()
    
            'Set server and print queue objects
            server1 = New PrintServer(serverName)
            pq1 = server1.GetPrintQueue(printerName1)
    
            'Set directory object where xps files are 
            Dim dir As New DirectoryInfo(xpsPath1)
    
            'Loop through each .xps files and print them
            For Each f As FileInfo In dir.GetFiles("*.xps")
    
                Dim nextFile As String = xpsPath1 & "\" & f.Name
    
                Dim xpsPrintJob As PrintSystemJobInfo = pq1.AddJob(f.Name, f.FullName, False)
                Console.WriteLine("Printing file: " & f.FullName & " || threadid: " & Threading.Thread.CurrentThread.ManagedThreadId & " || printer server: " & server1.Name & " || print queue: " & pq1.Name)
    
            Next f ' end for each XPS file
    
        End Sub
    
        Public Sub Print2()
            
            'Set server and print queue objects
            server2 = New PrintServer(serverName)
            pq2 = server2.GetPrintQueue(printerName2)
    
            'Set directory object where xps files are 
            Dim dir As New DirectoryInfo(xpsPath2)
    
            'Loop through each .xps files and print them
            For Each f As FileInfo In dir.GetFiles("*.xps")
    
                Dim nextFile As String = xpsPath2 & "\" & f.Name
    
                Dim xpsPrintJob As PrintSystemJobInfo = pq2.AddJob(f.Name, f.FullName, False)
                Console.WriteLine("Printing file: " & f.FullName & " || threadid: " & Threading.Thread.CurrentThread.ManagedThreadId & " || printer server: " & server2.Name & " || print queue: " & pq2.Name)
    
            Next f ' end for each XPS file
    
        End Sub
    
        Public Sub Print3()
            
            'Set server and print queue objects
            server3 = New PrintServer(serverName)
            pq3 = server3.GetPrintQueue(printerName3)
    
            'Set directory object where xps files are 
            Dim dir As New DirectoryInfo(xpsPath3)
    
            'Loop through each .xps files and print them
            For Each f As FileInfo In dir.GetFiles("*.xps")
    
                Dim nextFile As String = xpsPath3 & "\" & f.Name
    
                Dim xpsPrintJob As PrintSystemJobInfo = pq3.AddJob(f.Name, f.FullName, False)
                Console.WriteLine("Printing file: " & f.FullName & " || threadid: " & Threading.Thread.CurrentThread.ManagedThreadId & " || printer server: " & server3.Name & " || print queue: " & pq3.Name)
    
            Next f ' end for each XPS file
    
        End Sub
    
    End Class



    • Edited by slevesque1 Monday, July 30, 2012 6:46 PM
    Monday, July 30, 2012 6:37 PM

All replies

  • Does the problem happen with more than one PC?  Are the files on the local PC drive, or on a network drive?  Try both.  What operating system are you using?  Does it happen with more than one version of Windows?

    jdweng

    Monday, July 30, 2012 10:10 PM
  • It happens on four different PC. (two VMs and two work laptops).

    The .xps files are local but we also tried from a network drive.

    This has been reproduced on Windows 7 and Windows 2008 server.

    We cannot reproduce the issue in a single threaded environment but in a multi threaded environment, it happens on occasion.

    Thanks!

    Tuesday, July 31, 2012 11:10 AM
  • From what you described it appears to be a Net Library issue.  Here is 3 more questions to help isolate the problem

    1) What machine did you use to compile the software?

    2) What version of Net Library did you use do compile the software.

    3) How did you install the software if you didn't compile the software on the same machine.


    jdweng

    Tuesday, July 31, 2012 12:06 PM
  • 1. The sample app I provided was compiled with my Windows 7 dev environment using Microsoft Visual Studio 2010. The .Net framework used was 4.0.

    2. .Net Framework 4.0. 

    3. I ran this from my visual studio so no installation required.

    The issue is easily reproducible using the sample I included in my original post.

    Thanks everyone for the help on this so far. 


    • Edited by slevesque1 Tuesday, July 31, 2012 4:58 PM
    Tuesday, July 31, 2012 4:57 PM
  • Hi,

    You could  visit the below link to see the various paid support options that are available to better meet your needs if you requires a more in-depth level of support.

    http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone 

    Regards

    Tuesday, September 4, 2012 7:13 AM