Michael
Michael

Reputation: 23

visual basic thread handling (wait for other thread)

I have the following sub which starts another another sub in a new thread when a new file is created. The second sub is printing the file (PDF). So far all works fine.

Private Sub PrintPDF(Datei As String)
    Try
        Dim startPrint As New Thread(AddressOf PrintNow)
        startPrint.IsBackground = False
        startPrint.Start(Datei)
    Catch ex As Exception
        sendmail(__FUNCTION__(), ex.Message, True)
        Err.Clear()
    End Try
End Sub

Now my problem. In some cases I have to print the file two or more times (different paper trays). When two files for the the same printer a created the same time it the following output happens:

Print 1 - Page 1
Print 2 - Page 1
Print 1 - Page 2
Print 2 - Page 2

Which printer is used I know from the filename:
00_99999999~FreePDF~1~0~1~1_K1_K2_AA_AA~0~16~201703020716530219.pdf
-> FreePDF

Here now my question: Is there a possibility to wait for the end of the thread if the same printer is used by another thread? If there is no running thread for the printer a parallel printing is ok and wanted.

Thank you in advance.

Michael

Upvotes: 0

Views: 129

Answers (1)

djv
djv

Reputation: 15774

You can synchronize the critical section of code used to print. Use a dictionary of printer name to object to organize the locking objects.

Private dictionaryLock As New Object()
Private printerLocks As New Dictionary(Of String, Object)()

Private Sub PrintNow(datei As String)
    Dim printerName = datei.Split("~"c)(1)
    SyncLock dictionaryLock
        If Not printerLocks.ContainsKey(printerName) Then
            printerLocks.Add(printerName, New Object())
        End If
    End SyncLock
    SyncLock printerLocks(printerName)
        Console.WriteLine("Started printing on {0} - {1}", printerName, datei)
        Thread.Sleep(2000) ' print here
        Console.WriteLine("Finished printing on {0} - {1}", printerName, datei)
    End SyncLock
End Sub

Tested with this

Sub Main()
    PrintPDF("0~FreePDF1~1")
    PrintPDF("0~FreePDF2~1")
    PrintPDF("0~FreePDF7~1")
    PrintPDF("0~FreePDF1~2")
    PrintPDF("0~FreePDF4~1")
    PrintPDF("0~FreePDF2~2")
    PrintPDF("0~FreePDF1~3")
    PrintPDF("0~FreePDF3~1")
    PrintPDF("0~FreePDF3~2")
    PrintPDF("0~FreePDF5~1")
    PrintPDF("0~FreePDF1~4")
    PrintPDF("0~FreePDF2~3")
    PrintPDF("0~FreePDF6~1")
    PrintPDF("0~FreePDF3~3")
End Sub

Started printing on FreePDF1 - 0~FreePDF1~1
Started printing on FreePDF4 - 0~FreePDF4~1
Started printing on FreePDF2 - 0~FreePDF2~1
Started printing on FreePDF7 - 0~FreePDF7~1
Started printing on FreePDF5 - 0~FreePDF5~1
Started printing on FreePDF3 - 0~FreePDF3~1
Started printing on FreePDF6 - 0~FreePDF6~1
Finished printing on FreePDF4 - 0~FreePDF4~1
Finished printing on FreePDF1 - 0~FreePDF1~1
Started printing on FreePDF1 - 0~FreePDF1~2
Finished printing on FreePDF2 - 0~FreePDF2~1
Started printing on FreePDF2 - 0~FreePDF2~2
Finished printing on FreePDF7 - 0~FreePDF7~1
Finished printing on FreePDF5 - 0~FreePDF5~1
Finished printing on FreePDF3 - 0~FreePDF3~1
Started printing on FreePDF3 - 0~FreePDF3~2
Finished printing on FreePDF6 - 0~FreePDF6~1
Finished printing on FreePDF1 - 0~FreePDF1~2
Started printing on FreePDF1 - 0~FreePDF1~3
Finished printing on FreePDF2 - 0~FreePDF2~2
Started printing on FreePDF2 - 0~FreePDF2~3
Finished printing on FreePDF3 - 0~FreePDF3~2
Started printing on FreePDF3 - 0~FreePDF3~3
Finished printing on FreePDF1 - 0~FreePDF1~3
Started printing on FreePDF1 - 0~FreePDF1~4
Finished printing on FreePDF2 - 0~FreePDF2~3
Finished printing on FreePDF3 - 0~FreePDF3~3
Finished printing on FreePDF1 - 0~FreePDF1~4

Upvotes: 1

Related Questions