【问题标题】:VB.NET - How to copy file first before it move by the other servicesVB.NET - 如何在其他服务移动文件之前先复制文件
【发布时间】:2017-01-31 06:50:18
【问题描述】:

我们有一个现有的 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

【问题讨论】:

标签: vb.net multithreading


【解决方案1】:

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

Watcher.EnableRaisingEvents = True 将进入您的服务 OnStart。

Module Module1

    Private WithEvents Watcher As New IO.FileSystemWatcher("C:\POS", "*.xdf")

    Private Const xdf_temp As String = "c:\pos\temp"

    Sub Main()
        Watcher.EnableRaisingEvents = True

        Console.WriteLine("Watching . . .")
        Console.ReadKey()
    End Sub

    Private Sub Watcher_Created(sender As Object, e As IO.FileSystemEventArgs) Handles Watcher.Created
        Select Case True
            Case e.FullPath.EndsWith("event.xdf")
                Task.Run(Sub() CopyFile(e.FullPath))
            Case e.FullPath.EndsWith("statistics.xdf")
                Task.Run(Sub() CopyFile(e.FullPath))
            Case e.FullPath.EndsWith("order.xdf")
                Task.Run(Sub() CopyFile(e.FullPath))
        End Select
    End Sub

    Private Sub CopyFile(FullPath As String)
        Dim Timeout As New Stopwatch
        Dim Success As Boolean = False

        Timeout.Restart()

        Do Until Success Or Timeout.Elapsed.Minutes > 0
            Try
                IO.File.Copy(FullPath, IO.Path.Combine(xdf_temp, IO.Path.GetFileName(FullPath)), True)
                Success = True
            Catch
                ' File Not accessible...locked, write in progress, etc.
                Threading.Thread.Sleep(New TimeSpan(0, 0, 1))
            End Try
        Loop

        If Not Success Then
            'Timeout expired - write something in an event log
        End If
    End Sub

End Module

【讨论】:

  • 非常感谢先生!我刚刚添加了AddHandler Watcher.Created,AddressOf Watcher_created。选择案例也不起作用。此外,有时文件完整,有时我仍然错过 1 或 2 个文件。
  • 是的,如果您不使用 WithEvents 实例化它并自动生成处理程序,则必须这样做。
  • 不要忘记点击左侧的复选标记来标记答案。
猜你喜欢
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
相关资源
最近更新 更多