troubleshooting Question

Vb.NET Threading Question

Avatar of GJDuquette
GJDuquette asked on
Visual Basic.NET
2 Comments1 Solution100 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
it_saige
Developer
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros