VB.NET - 如何在文件被其他服务移动之前先复制文件

时间:2017-01-31 06:50:18

标签: vb.net multithreading

我们有一个现有的Windows服务移动文件(7个文件,5 * event.xdf,1个statistics.xdf和1个order.xdf文件)并将其转换为xml。

我创建了一个Windows服务,在其他Windows服务移动之前复制所有7个文件。我只能复制3 * event.xdf,1 statistics.xdf和1 order.xdf。

我想念2 event.xdf。

这是我的代码逻辑

我为每个不同的文件创建一个线程并复制每个文件。

将事件复制到临时文件夹:

thXDFevent = New System.Threading.Thread(AddressOf backup_xdf_events)
thXDFevent.IsBackground = True
thXDFevent.Start()

'order
thXDForder = New System.Threading.Thread(AddressOf backup_xdf_order)
thXDForder.IsBackground = True
thXDForder.Start()

'statistics
thXDFstatistics = New System.Threading.Thread(AddressOf backup_xdf_statistics)
thXDFstatistics.IsBackground = True
thXDFstatistics.Start()

功能:

Private Sub backup_xdf_events()
        Try
            While (True)
                getXDFevents()
            End While
        Catch ex As Exception
            'EventLog1.WriteEntry("Services XDF error:" & strDate.ToString)
        End Try
 End Sub

Private Sub getXDFevents()
    Dim f As String
    For Each f In Directory.GetFiles("C:\POS\", "*event.xdf")

        My.Computer.FileSystem.CopyFile("C:\POS\" & Path.GetFileName(f), xdf_temp & Path.GetFileName(f), True)
    Next f
    System.Threading.Thread.Sleep(500)
End Sub

Private Sub backup_xdf_order()
    Try
        While (True)
            getXDForder()
        End While
    Catch ex As Exception
        'EventLog1.WriteEntry("Services XDF error:" & strDate.ToString)
    End Try
End Sub
Private Sub getXDForder()
    Dim f As String
    For Each f In Directory.GetFiles("C:\POS\", "*order.xdf")

        My.Computer.FileSystem.CopyFile("C:\POS\" & Path.GetFileName(f), xdf_temp & Path.GetFileName(f), True)
    Next f
    System.Threading.Thread.Sleep(500)
End Sub

Private Sub backup_xdf_statistics()
    Try
        While (True)
            getXDFstatistics()
        End While
    Catch ex As Exception
        'EventLog1.WriteEntry("Services XDF error:" & strDate.ToString)
    End Try
End Sub


Private Sub getXDFstatistics()
    Dim f As String
    For Each f In Directory.GetFiles("C:\POS\", "*statistics.xdf")

        My.Computer.FileSystem.CopyFile("C:\POS\" & Path.GetFileName(f), xdf_temp & Path.GetFileName(f), True)
    Next f
    System.Threading.Thread.Sleep(500)
End Sub

1 个答案:

答案 0 :(得分:0)

使用FileSystemWatcher和“任务”的替代方法,在文件可用时立即复制该文件。

import Comments from './components/Comments.vue'; import Upload from './components/Upload.vue'; new Vue({ el: 'body', components: { Comments, Upload, Preview, Algolia, }, etc, }); 将在您的服务OnStart中使用。

Watcher.EnableRaisingEvents = True