Click here to Skip to main content
15,893,814 members
Articles / Web Development / HTML

Multithreading Backup Utility

Rate me:
Please Sign up or sign in to vote.
4.89/5 (72 votes)
8 Oct 2019CPOL12 min read 238.7K   14.3K   208  
Multithreading is something we will all have to deal with sooner or later. This relatively simple application shows you how to use two threads to copy files. It is also a very handy Windows backup utility with a mirror feature, a batch mode feature, a log file feature and a help button!
Public Class AppManager

    <STAThread()> _
    Shared Sub Main()
        ' Set form style defaults
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)

        ' Test for command line parameters.
        If Environment.GetCommandLineArgs.Length > 1 Then
            ' If in batch mode just instantiate the FileCopy class as no form is needed.
            ' Also don't declare this class "withevents" as the events only report status
            ' data (like the number of files copied so far).
            Dim BatchCopy As New FileCopy

            ' Setup the copy parameters for the BatchCopy instance from command line arguments
            Dim Argument As String
            Dim Index As Integer = 0
            For Each Argument In Environment.GetCommandLineArgs
                Select Case Index
                    Case 0
                        ' The first argument is the app file/path so do nothing
                    Case 1
                        BatchCopy.FromPath = Environment.GetCommandLineArgs(1).ToString
                    Case 2
                        BatchCopy.ToPath = Environment.GetCommandLineArgs(2).ToString
                    Case 3
                        If Environment.GetCommandLineArgs(3).ToString.ToUpper = "TRUE" Then
                            BatchCopy.MirrorCopy = True
                        Else
                            BatchCopy.MirrorCopy = False
                        End If
                    Case 4
                        If Environment.GetCommandLineArgs(4).ToString.ToUpper = "TRUE" Then
                            BatchCopy.QuietLog = True
                        Else
                            BatchCopy.QuietLog = False
                        End If
                    Case Else
                        ' Ignore any remaining arguments as something is wrong. The BatchCopy class
                        ' will log any errors in the log file.
                End Select
                Index += 1
            Next

            ' Start the multithreaded copy 
            BatchCopy.InitialMessage = "*** MODE IS BATCH ***"
            If BatchCopy.MirrorCopy Then
                BatchCopy.InitialMessage = "*** MODE IS BATCH - MIRROR COPY IS SET ***"
            End If
            If BatchCopy.QuietLog Then
                BatchCopy.InitialMessage += vbCrLf & "Logging is quiet - not verbose."
            Else
                BatchCopy.InitialMessage += vbCrLf & "Logging is not quiet - its verbose."
            End If
            BatchCopy.BatchCopy = True
            BatchCopy.StartCopy()

            BatchCopy.WaitForThreads()
        Else
            ' Forms mode so open the Backup form. The Backup from will instantiate the FileCopy class
            ' when the "Go" button is pressed.
            Dim BackupForm As Backup
            BackupForm = New Backup()
            Application.Run(BackupForm)
        End If

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Nice people only.
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions