所以我有一些代码可以将文件复制到网络上的5台远程PC。我有一个模拟需要登录的方法的类
''' <summary>
''' Returns the names of the files including their path that match the regex pattern found in this directory.
''' </summary>
''' <param name="searchOption">
''' The parameter option SearchOption.AllDirectories indicates that the search should include the current directory and any subdirectories
''' The parameter option SearchOption.TopDirectoryOnly indicates that the search should only include the current directory.
''' </param>
Public Function GetFileContent(filePath As String, searchPattern As String, Optional searchOption As SearchOption = Nothing) As String() Implements IEPMADirectoryAccess.GetFileContent
If searchOption = Nothing Then
searchOption = SearchOption.TopDirectoryOnly
End If
LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
Using WindowsIdentity.Impersonate(UserToken)
Return Directory.GetFiles(filePath, searchPattern, searchOption)
End Using
End Function
''' <summary>
''' Get the name of the Ward from the directory of the current file path
''' </summary>
Public Function GetWardName(filePath As String) As String Implements IEPMADirectoryAccess.GetWardName
LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
Using WindowsIdentity.Impersonate(UserToken)
Return New DirectoryInfo(Path.GetDirectoryName(filePath)).Name
End Using
End Function
''' <summary>
''' Creates a directory for the given path
''' </summary>
Public Sub CreateDirectory(directoryPath As String) Implements IEPMADirectoryAccess.CreateDirectory
LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
Using WindowsIdentity.Impersonate(UserToken)
Directory.CreateDirectory(directoryPath)
End Using
End Sub
''' <summary>
''' Deletes a file from the specified path
''' </summary>
Public Sub DeleteFile(filePath As String) Implements IEPMADirectoryAccess.DeleteFile
LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
Using WindowsIdentity.Impersonate(UserToken)
File.Delete(filePath)
End Using
End Sub
''' <summary>
''' Copies a file from the source path to the destination path
''' </summary>
Public Sub CopyFile(sourceFilePath As String, destFilePath As String, overwrite As Boolean) Implements IEPMADirectoryAccess.CopyFile
LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
Using WindowsIdentity.Impersonate(UserToken)
File.Copy(sourceFilePath, destFilePath, overwrite)
End Using
End Sub
并行循环中的部分代码如下:
'If PC is switched on
If IsOnline(device.PCName) Then
'For each PC of the ward, get the corresponding folder path
targetPath = "\\" + device.PCName + "\c$Report P\"
'Requires access to the network share
If Not Directory.Exists(targetPath) Then
DirectoryAccess.CreateDirectory(targetPath)
End If
'Requires access to the network share
For Each fileName As String In DirectoryAccess.GetFileContent(targetPath, "*.pdf")
'Purge (Delete) previous versions of this file in the destination folder
Dim fileConst As String
fileConst = arr(1)
If fileName.Contains(fileConst) Then
'Requires access to the network share
DirectoryAccess.DeleteFile(fileName)
End If
Next
DirectoryAccess.CopyFile(f, Path.Combine(targetPath, Path.GetFileName(f), True)
End If
在主模块中,我正在计时使用 正常的for循环与并行的for循环相比,但在for循环中具有相同的代码内容:
最初,并行复制为18秒,但第二轮运行为2.82秒。
第三轮为1.81秒。
我检查了用于模拟的using块是否已正确放置,并在退出using块后还原为我的原始凭据。我不知道为什么它在第一次运行时会很慢,然后又加速呢?最初可能是四台PC处于非活动状态,所以复制过程会激活它们,但是需要更长的时间吗?
我有一个IsOnline函数,用于检查PC是否已打开电源,否则,我只是跳过它们以确保它不会减慢该过程的速度。
答案 0 :(得分:0)
事实证明,它很慢,因为在任何一次都使用不同数量的PC来测量时间。一些PC在测量过程中进入睡眠状态,从而影响了处理时间