写入文件卡在网络共享上
|
我有一个程序可以一次从几(3)个线程将文件高速写入网络共享。
在运行一段时间(通常是一小段时间)后,其中一些线程被卡住了。使用Process Monitor,我可以看到对WriteFile和CloseFile的调用根本没有任何答案。
在这一点上,我根本无法关闭该进程,即使从任务管理器中将其杀死也无济于事。
有趣的是,当托管共享的计算机运行Windows Server 2008(R2)时,会发生这种情况。如果将共享移动到Windows 2003计算机,则看不到这些问题。另外,如果程序在运行Windows Server 2008的计算机(不同于共享主机的计算机)上运行,则只会看到此问题。
这是一个简短的程序,可以快速重现该问题。源目录中的文件大小范围为1到20 MB:
Imports System.IO
Imports System.Threading
Module Module1
Private m_sourceFiles As FileInfo()
Private m_targetDir As String
Sub Main(ByVal args As String())
Dim sourceDir As New DirectoryInfo(args(0))
m_sourceFiles = sourceDir.GetFiles()
m_targetDir = args(1)
For i As Integer = 0 To 2
ThreadPool.QueueUserWorkItem(AddressOf DoWork)
Next
Console.ReadLine()
End Sub
Private Const BUFFER_SIZE As Integer = (128 * 1024)
Private Sub DoWork(ByVal o As Object)
Console.WriteLine(Thread.CurrentThread.ManagedThreadId)
Dim random As New Random(Thread.CurrentThread.ManagedThreadId)
While True
Dim fileIndex As Integer = random.Next(m_sourceFiles.Count)
Dim sourceFile As FileInfo = m_sourceFiles(fileIndex)
Dim input As FileStream = sourceFile.OpenRead
Dim targetName As String = sourceFile.Name.Replace(sourceFile.Extension, random.Next(Integer.MaxValue) & sourceFile.Extension)
Dim targetPath As String = m_targetDir & \"\\\" & targetName
Dim output As FileStream = File.Create(targetPath)
Dim bytes() As Byte = New Byte((BUFFER_SIZE) - 1) {}
Dim read As Integer = input.Read(bytes, 0, bytes.Length)
While read <> 0
output.Write(bytes, 0, read)
read = input.Read(bytes, 0, bytes.Length)
End While
output.Flush()
output.Close()
Console.WriteLine(Thread.CurrentThread.ManagedThreadId & \" - \" & targetName)
End While
End Sub
End Module
没有找到相关结果
已邀请:
1 个回复
呕蹿尉