多线程文件备份(VB.NET版)

简介:
原文链接:Multithreading Backup Utility

 
   
简介
多线程迟早是我们要面对的一个东西,本文向你展示了一个简单的使用两个线程来拷贝文件的应用程序
Backup将一个目录中文件拷贝到另一个目录中,它使用了一个线程来显示正被拷贝的文件名称,另一个线程用来在拷贝的文件的同时统计文件数目和文件夹数目。这就意味着在拷贝可以开始之前不用浪费时间去等待文件数目的统计完成,我们使用了两个线程同时完成拷贝和统计工作。
Backup对于大数量文件的拷贝也是快速和有效率的。它很快是因为当目标文件已经存在并且没有改变过时就不做拷贝工作,因此它对于重复拷贝来说不错,因为它只拷贝新文件或更新过的文件。
这个程序的另一个特点是当在一个特点文件上发生安全性错误或其他类型错误(当然不包括硬件错误),它不会停止拷贝工作,它将会记录下错误信息并继续完成工作。过后你可以去查看日志文件,它会告诉你发生了什么错误。大部分错误都是由于安全配置问题产生的。
背景知识
为什么要使用多线程呢?一个原因可能一个窗口在忙碌时,你想能点击窗口上的一个按钮。另一个原因是多核时代已经来临,硬件和操作系统级别的多任务也存在,线程无可避免,尤其当我们关注性能时。
好的,你已经决定你想在.Net中使用多线程。你可以使用BackgroundWorker, 不过我假定你应该从System.Threading开始,并直接使用Thread类。个人看来,它更容易使用,并且更具灵活性。
那么线程到底是什么呢?它就好比于一个源自于你的主程序的另一个完全分离的程序。一旦线程启动,主程序对线程完全不知道。它们生死自控。要启动一个或两个线程,你可能想知道如何创建线程,如何传递数据,如何从子线程中回调主应用程序以及主应用程序如何知道是哪个子线程回调了它?下面的代码片段会回答这些问题的。
最后一点,作者将一个类实例的方法赋予了一个线程,尽管可以将主程序的方法赋予线程,但作者认为这样做更好。

    ' Declare first thread variable. This will be used to copy the files.
    Private CopyThread As Thread '拷贝线程

    ' Delclare second thread variable. This will be used to count the folders and files.
Private CountThread As Thread '统计线程

统计线程一般在拷贝线程前面完成,除非你拷贝的文件数目很小,那种情况下一切都发生的很快。
另一方面,统计线程结束后,总文件数目会被统计出来,从而用来设置ProgressBar1.Max属性,
下面是启动拷贝线程和统计线程的代码:
  Private Sub StartCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartCopy.Click
        '开始拷贝
        ' Validate from and to folders
        If Not SetRootPath Then Exit Sub
        If Not CheckFromPath() Then Exit Sub
        If Not CheckToPath() Then Exit Sub

        ' Create an instance of the copy class that will be assigned to the first thread.
        Dim FileCopy As New CopyClass(Me)

        ' Set required properties
        FileCopy.FromPath = FromPathTextbox.Text
        FileCopy.ToPath = ToPathTextbox.Text & _rootDir
        FileCopy.StartDateTime = DateTime.Now

        'Save log file name
        _logFile = FileCopy.LogFileName

        ' Create the thread and assign the class instance and method to execute 
        ' (CopyFiles in this case) when the thread is started.
        CopyThread = New Thread(AddressOf FileCopy.CopyFiles) '拷贝线程

        ' Start the first thread to copy the files.
        CopyThread.Name = "Copy"
        CopyThread.IsBackground = True
        CopyThread.Start()

        ' Create another instance of the copy class that will be assigned to the second thread.
        Dim FileFolderCount As New CopyClass(Me)

        ' Set required properties
        FileFolderCount.FromPath = FromPathTextbox.Text
        FileFolderCount.ToPath = ToPathTextbox.Text

        ' Create the thread and assign the class instance and method to execute 
        ' (CopyFiles in this case) when the thread is started.
        CountThread = New Thread(AddressOf FileFolderCount.GetCountData) '计数线程

        ' Start the second thread to count folders and files while the copy is running at the same time.
        CountThread.Name = "Count"
        CountThread.IsBackground = True
        CountThread.Start()

        ' Reset form controls
        StartCopy.Enabled = False
        Panel1.Enabled = False
        StopCopy.Enabled = True

    End Sub

下面是终止两个线程的代码:
    Private Sub StopCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopCopy.Click
        '终止线程
        If CopyThread.IsAlive Then CopyThread.Abort()
        If CountThread.IsAlive Then CountThread.Abort()
    End Sub

主界面的两个delegate方法,用来响应子线程的回调,刷新主界面
    Public Sub CopyThreadMessage(ByVal ThreadName As String, ByVal CopiedFiles As Long, ByVal Message As String)

        ' If finished copying
        If Message = "END" Then
            lblStatus.Text = "Status: Copy Finsihed. Copied " + _totalFiles.ToString + " files in " + _totalFolders.ToString + " folders."
            txtFile.Text = "Copy completed successfully."
            ProgressBar1.Value = ProgressBar1.Maximum
            CopyThread.Abort()
            CountThread.Abort()
            Exit Sub
        End If

        ' Show current file
        txtFile.Text = "Copying: " & Message

        ' Update progressbar
        If ProgressBar1.Maximum <> 0 Then ProgressBar1.Value = _totalFiles - (_totalFiles - CopiedFiles)

        ' Update status (TotalFiles not zero means counting has finished)
        If _totalFiles <> 0 Then
            lblStatus.Text = "Status: Copying. There are " + _totalFiles.ToString + " files in " + _totalFolders.ToString + " folders. Files copied so far " & CopiedFiles & "."
        End If

        ' Save for CountThreadMessage()
        _copiedFiles = CopiedFiles

    End Sub
    Public Sub CountThreadMessage(ByVal ThreadName As String, ByVal Files As Long, ByVal TotalFiles As Long, ByVal Folders As Long, ByVal Message As String)

        ' Display current count
        lblStatus.Text = "Status: Copying and Counting. So far there are " + Files.ToString + " files in " + Folders.ToString + " folders."

        ' Save totals when finished counting for CopyThreadMessage()
        If Message = "END" Then
            _totalFiles = TotalFiles
            _totalFolders = Folders
            lblStatus.Text = "Status: Copying. There are " + _totalFiles.ToString + " files in " + _totalFolders.ToString + " folders. Files copied so far " & _copiedFiles & "."
            ProgressBar1.Maximum = _totalFiles
            ProgressBar1.Value = _totalFiles - (_totalFiles - _copiedFiles)
        End If

    End Sub

负责拷贝和统计的类:
Imports System.Threading
Imports System.IO
Public Class CopyClass
    'This will hold the reference to the client form
    Private _clientApp As Form

    'Create a delegate method that will map to the CopyThreadMessage method of the client app
    Private Delegate Sub CallClientCopy(ByVal ThreadName As String, ByVal FilesRemaining As Long, ByVal Message As String)

   'Create a delegate method that will map to the CountThreadMessage method of the client app
    Private Delegate Sub CallClientCount(ByVal ThreadName As String, ByVal TotalFiles As Long, ByVal TotalFolders As Long, ByVal Files As Long, ByVal Message As String)

    'Create an object for each deletegate
    Private _callClientCopy As CallClientCopy
    Private _callClientCount As CallClientCount

    ' Property variables
    Private _firstTime As Boolean
    Private _fromPath As String
    Private _toPath As String
    Private _directories As Long
    Private _files As Long
    Private _copiedFiles As Long
    Private _totalFiles As Long
    Private _fileName As String
    Private _logFile As StreamWriter
    Private _startDateTime As Date
    Private _logFileName As String

    ' Constants
    Private Const LOG_FILE As String = "BackupLog.txt"
    Private Const ERR_MSG As String = "Error accessing file: "
    Public Sub New(ByRef ClientApp As Backup)

        ' Save the reference to the client app
        _clientApp = ClientApp

        ' Assign delegate objects
        _callClientCopy = AddressOf ClientApp.CopyThreadMessage
        _callClientCount = AddressOf ClientApp.CountThreadMessage

    End Sub
    Public Sub CopyFiles()
        'Do the work of the first thread here

        ' Give this thread a name
        If Thread.CurrentThread.Name = Nothing Then Thread.CurrentThread.Name = "Copy"

        ' Create a new DirectoryInfo object for from path.
        Dim dir As New DirectoryInfo(FromPath)

        ' Call the GetFileSystemInfos method.
        Dim FSinfo As FileSystemInfo() = dir.GetFileSystemInfos

        ' Open log file
        OpenLog()

        'Copy one file at a time looping until all files are copied
        ReallyCopyFiles(FSinfo)

        WriteLog("Copy completed successfully.")

        'Call client one last time to signal end of copy
        CallClient(Thread.CurrentThread.Name, _copiedFiles, _totalFiles, _directories, "END")

    End Sub
    Public Sub GetCountData()
        'Do the work of the second thread here

        ' Give this thread a name
        If Thread.CurrentThread.Name = Nothing Then Thread.CurrentThread.Name = "Count"

        ' Create a new DirectoryInfo object for from path.
        Dim dir As New DirectoryInfo(FromPath)

        ' Call the GetFileSystemInfos method.
        Dim FSinfo As FileSystemInfo() = dir.GetFileSystemInfos

        ' Count folder and files
        CountFiles(FSinfo)

        ' Save total files count
        _totalFiles = _files

        ' Send message to client form
        CallClient(Thread.CurrentThread.Name, _files, _totalFiles, _directories, "END")

    End Sub
    Private Sub ReallyCopyFiles(ByVal FSInfo As FileSystemInfo())
        ' Check the FSInfo parameter.
        If FSInfo Is Nothing Then
            Throw New ArgumentNullException("FSInfo")
        End If

        ' Iterate through each item.
        Dim i As FileSystemInfo
        For Each i In FSInfo

            Try
                ' Check to see if this is a DirectoryInfo object.
                If TypeOf i Is DirectoryInfo Then
                    ' Cast the object to a DirectoryInfo object.
                    Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)

                    ' Iterate (recurse) through all sub-directories.
                    ReallyCopyFiles(dInfo.GetFileSystemInfos())
                    ' Check to see if this is a FileInfo object.
                ElseIf TypeOf i Is FileInfo Then
                    'save the full path and file name
                    _fileName = i.FullName

                    'Get the copy path name only
                    Dim copypath As String = ToPath & Mid(_fileName, Len(FromPath) + 1, Len(_fileName) - Len(FromPath) - Len(i.Name))

                    'Create copy path if it does not exist
                    If Not Directory.Exists(copypath) Then
                        Directory.CreateDirectory(copypath)
                    End If

                    ' Get the to path and filename
                    Dim tofile As String = ToPath & Mid(_fileName, Len(FromPath) + 1)

                    ' Update status info on client
                    Dim fi As New FileInfo(_fileName)
                    Dim Message As String = _fileName & " is " & Decimal.Round(CDec(fi.Length / 1048576), 2) & "MB in length."
                    CallClient(Thread.CurrentThread.Name, _copiedFiles, _totalFiles, _directories, Message)

                    ' if file exists check if file has been updated since last copy
                    Dim OkayToCopy As Boolean = True
                    If File.Exists(tofile) Then
                        If File.GetLastWriteTime(_fileName) = File.GetLastWriteTime(tofile) Then
                            OkayToCopy = False
                        End If
                    End If

                    ' Copy file with overwrite
                    If OkayToCopy Then File.Copy(_fileName, tofile, True)

                    ' Increment copied file count
                    _copiedFiles += 1

                End If
            Catch ex As Exception
                ' Report error but continue processing
                WriteLog(ERR_MSG & _fileName & vbCrLf & ex.Message.ToString)
            End Try

        Next i

    End Sub
    Private Sub CountFiles(ByVal FSInfo As FileSystemInfo())
        Static ShowCount As Long = 0

        ' Check the FSInfo parameter.
        If FSInfo Is Nothing Then
            Throw New ArgumentNullException("FSInfo")
        End If

        ' Iterate through each item.
        Dim i As FileSystemInfo
        For Each i In FSInfo

            Try
                ' Check to see if this is a DirectoryInfo object.
                If TypeOf i Is DirectoryInfo Then
                    ' Add one to the directory count.
                    _directories += 1

                    ' Cast the object to a DirectoryInfo object.
                    Dim dInfo As DirectoryInfo = CType(i, DirectoryInfo)

                    ' Iterate (recurse) through all sub-directories.
                    CountFiles(dInfo.GetFileSystemInfos())
                    ' Check to see if this is a FileInfo object.
                ElseIf TypeOf i Is FileInfo Then
                    ' Add one to the file count.
                    _files += 1

                    'display count for first file in every folder then every 200 - for faster performance
                    Select Case ShowCount
                        Case 0
                            ' Display count
                            CallClient(Thread.CurrentThread.Name, _files, _totalFiles, _directories, "")
                        Case Is >= 200
                            ' Display count
                            CallClient(Thread.CurrentThread.Name, _files, _totalFiles, _directories, "")

                            'reset so display is every 200 files in folder
                            ShowCount = 0
                    End Select

                    'Increment show count
                    ShowCount += 1

                End If
            Catch ex As Exception
                'Record error then continue (like a resume next)
                WriteLog(ERR_MSG & _fileName & vbCrLf & ex.Message.ToString)
            End Try

        Next i

    End Sub
    Private Sub CallClient(ByVal ThreadName As String, ByVal Files As Long, ByVal TotalFiles As Long, ByVal Directories As Long, ByVal Message As String)

        Select Case ThreadName
            Case "Copy"
                'Call the delegated method
                _clientApp.Invoke(_callClientCopy, ThreadName, Files, Message)
            Case "Count"
                'Call the delegated method
                _clientApp.Invoke(_callClientCount, ThreadName, Files, TotalFiles, Directories, Message)
        End Select

        'Let the thread sleep before continuing so the client app will have time to be process (1 millisecond is enough)
        Thread.Sleep(0)
    End Sub
    Private Sub OpenLog()

        ' Create log file
        If Not File.Exists(StartDateTime & "-" & LOG_FILE) Then
            Using _logFile As StreamWriter = File.CreateText(LogFileName)
                _logFile.WriteLine("Logfile name is: " & LogFileName)
                _logFile.WriteLine("BACKUP LOG FILE STARTED AT: " & StartDateTime.ToString)
                _logFile.WriteLine("================================================")
                _logFile.Write("Copying FROM: " & _fromPath)
                _logFile.WriteLine()
                _logFile.Write("Copying TO: " & _toPath)
                _logFile.WriteLine()
                _logFile.Close()
            End Using
        End If

    End Sub
    Private Sub WriteLog(ByVal Message As String)

        ' Create an instance of StreamWriter to write text to a file.
        Using _logFile As StreamWriter = File.AppendText(LogFileName)
            ' Add some text to the file.
            _logFile.WriteLine()
            _logFile.WriteLine("TIME OF LOG ENTRY: " & DateTime.Now)
            ' Arbitrary objects can also be written to the file.
            _logFile.WriteLine(Message)
            _logFile.Flush()
            _logFile.Close()
        End Using

    End Sub
    Private Sub CloseLog()
        If File.Exists(LogFileName) Then _logFile.Close()
    End Sub
    Private Property FirstTime() As Boolean
        Get
            Return _firstTime
        End Get
        Set(ByVal value As Boolean)
            _firstTime = value
        End Set
    End Property
    Public Property FromPath() As String
        Get
            Return _fromPath
        End Get
        Set(ByVal value As String)
            _fromPath = value
        End Set
    End Property
    Public Property ToPath() As String
        Get
            Return _toPath
        End Get
        Set(ByVal value As String)
            _toPath = value
        End Set
    End Property
    Public Property StartDateTime() As Date
        Get
            Return _startDateTime
        End Get
        Set(ByVal value As Date)
            _startDateTime = value
        End Set
    End Property
    Public ReadOnly Property LogFileName() As String
        Get
            Return Format(StartDateTime, "yyMMdd-hhmmss") & "-" & LOG_FILE
        End Get
    End Property
    Public ReadOnly Property Directories() As Long
        Get
            Return _directories
        End Get
    End Property
    Public ReadOnly Property Files() As Long
        Get
            Return _files
        End Get
    End Property
    Public ReadOnly Property TotalFiles() As Long
        Get
            Return _totalFiles
        End Get
    End Property

End Class

作者的程序中还实现了一个比较有意思的特点,用户可以通过指定命令行参数进入后台命令行模式,而非界面模式,

Last Words
      感谢作者的这篇文章,让我能参照他的思路用VC++实现了一个类似的多线程的文件备份工具。
 
Reference:
 
原文链接:Multithreading Backup Utility



本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/06/11/1217244.html,如需转载请自行联系原作者
目录
相关文章
|
16天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
1月前
|
并行计算 安全 Java
C# .NET面试系列四:多线程
<h2>多线程 #### 1. 根据线程安全的相关知识,分析以下代码,当调用 test 方法时 i > 10 时是否会引起死锁? 并简要说明理由。 ```c# public void test(int i) { lock(this) { if (i > 10) { i--; test(i); } } } ``` 在给定的代码中,不会发生死锁。死锁通常是由于两个或多个线程互相等待对方释放锁而无法继续执行的情况。在这个代码中,只有一个线程持有锁,且没有其他线程参与,因此不
105 3
|
2天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
4月前
|
IDE C# 开发工具
C# | 多线程批量下载文件(创建N个线程同时批量下载文件,只需要几行代码而已)
批量下载文件时使用多线程可以有效缩短完成时间,本文将讲解如何使用C#+CodePlus扩展库快速完成多线程的文件下载。 大部分代码由IDE自动生成,需要我们自己编写的代码正好**10行**。也就是说,只需要10分钟,就可以手撸一个多线程的批量下载器。
87 0
C# | 多线程批量下载文件(创建N个线程同时批量下载文件,只需要几行代码而已)
|
1月前
|
消息中间件 测试技术 Python
Python使用多线程解析超大日志文件
Python使用多线程解析超大日志文件
22 0
|
3月前
|
Java 数据处理
fastdfs源码阅读:文件传输原理与网络IO模型(accept线程、work线程(网络io处理)、dio线程(文件io处理))
fastdfs源码阅读:文件传输原理与网络IO模型(accept线程、work线程(网络io处理)、dio线程(文件io处理))
44 0
|
5月前
|
Java
java多线程分片下载文件
java多线程分片下载文件
|
6月前
|
对象存储
.net core 阿里云接口之拷贝文件
紧接上文, 1)[.net core 阿里云接口之获取临时访问凭证](https://developer.aliyun.com/article/1363447?spm=a2c6h.12873639.article-detail.7.2b0e5b1cpeWbZ5 ".net core 阿里云接口之获取临时访问凭证") 2)[.net core 阿里云接口之将指定的OSS文件下载到流](https://developer.aliyun.com/article/1363886 ".net core 阿里云接口之将指定的OSS文件下载到流") 本文继续阿里云接口调用,将指定的OSS文件下载到流。
34 0
|
6月前
|
C#
.net core 从(本地)服务器获取APK文件并解析APK信息
## 1、apk解析除了使用客户端利用aapt.exe、unzip.exe开发客户端解析外,还可以直接利用服务进行解析 ```csharp /// <summary> /// 从本地服务器获取APK文件并解析APK信息 /// </summary> /// <param name="fileName">APK文件的完整路径</param> /// <returns></returns> [HttpPost, HttpGet, HttpOptions, CorsOptions] public IActionResult DecodeAPK(string fileName) { if(fi
30 0
|
6月前
|
JavaScript 前端开发 关系型数据库
.net core + vue + elementui 删除指定日期段、指定路径下的所有文件
# 1、呈现效果 ![image.png](https://ucc.alicdn.com/pic/developer-ecology/j2ygdazy447va_0782583bbc894c33a079db9e44385acd.png) # 2、后端 ## 1)服务层 ```csharp /// <summary> /// 删除指定修改日期段及指定路径下的所有文件 /// </summary> /// <param name="filepath">指定路径</param> /// <returns>返回删除结果提示</returns> public string DeleteSpecif
42 0

热门文章

最新文章