Avatar of GJDuquette
GJDuquette
 asked on

Vb.NET Threading Question

Experts,

I have a project to convert 1000's of tif images to pdf. In order speed up the process i'm trying to use a thread pool.

The code below loops through all the directories/files but only converts a few hundred files (no errors).

I think the issue is how I have implement the threading. Can you enlighten me on the correct threading method?

Imports System.IO
Imports System.Threading
Imports GdPicture14

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Get recursive List of all files starting in this directory.
        Dim tifFiles As New List(Of String)
        Dim folderStack As New Stack(Of String)
        Dim fileStack As New Stack(Of String)
        Dim currFile As String = ""
        Dim Count = 0
        Dim w As WaitCallback = New WaitCallback(AddressOf cvt1)
        folderStack.Push("C:\testfolder")
        Do While (folderStack.Count > 0)
            ' Get top directory string
            Dim dir As String = folderStack.Pop
            Try
                ' Add all immediate file paths
                If Not Directory.Exists(dir.Replace("C:\", "D:\")) Then
                    Directory.CreateDirectory(dir.Replace("C:\", "D:\"))
                End If
                For Each file In Directory.GetFiles(dir, "*.tif")
                    fileStack.Push(file)
                Next
                Do While (fileStack.Count > 0)
                    currFile = fileStack.Pop
                    ThreadPool.QueueUserWorkItem(w, currFile)
                    ConversionCount.Text += 1
                    ConversionCount.Refresh()
                    currFile = ""
                Loop
                ' Loop through all subdirectories and add them to the stack.
                Dim directoryName As String = ""
                For Each directoryName In Directory.GetDirectories(dir)
                    folderStack.Push(directoryName)
                Next
            Catch ex As Exception
                MsgBox("Error: " & vbCrLf & ex.ToString, MsgBoxStyle.Information, "Error!")
            End Try
        Loop
    End Sub

    Public Function cvt1(ByVal f As String) As String
        Dim oConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter()
        Dim status As GdPictureStatus = oConverter.LoadFromFile(f, GdPicture14.DocumentFormat.DocumentFormatIFF)
        If status = GdPictureStatus.OK Then
            f = f.Replace("C:\", "D:\")
            f = f.Replace(".tif", ".pdf")
            status = oConverter.SaveAsPDF(f, PdfConformance.PDF1_7)
            If status <> GdPictureStatus.OK Then
                MessageBox.Show("The file has failed to save. Status: " + status.ToString(), "TIFF to PDF", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
            Return True
        Else
            Return False
            MessageBox.Show("The file has failed to load. Status: " + status.ToString(), "TIFF to PDF Example", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Function

End Class
Visual Basic.NET

Avatar of undefined
Last Comment
GJDuquette

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
GJDuquette

ASKER
Thanks Saige! Works perfectly!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes