We help IT Professionals succeed at work.
Get Started

Vb.NET Threading Question

GJDuquette
GJDuquette asked
on
94 Views
Last Modified: 2020-02-19
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
Comment
Watch Question
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE