我有一个程序,自动下载一个文件从FTP服务器
我有一个通过BackgroundWorker的代码运行,下载一个文件,它工作正常。
问题是,当我尝试用三个BackgroundWorkers同时下载不同的文件,还有一个文件,我不能下载(操作有超时)既不三个BackgroundWorkers的。
我可以下载错误档案(文件,我不能下载)当其他BackgroundWorkers一个完成下载的文件。(我可以做到这一点,因为我有一个循环的BackgroundWorker不能下载文件时,它会尝试再次下载)
同样的错误时发生的BackgroundWorker那里开始它的下载两个BackgroundWorker的。
是什么原因导致此错误发生呢?在FTP服务器或我的代码是什么?
其实,我怀疑,FTP服务器会导致错误,因为我看到一个应用程序,下载一个文件从FTP服务器(FTP服务器相同),可以下载10个文件在同一时间,但它不会自动下载。但我不知道如果这是正确的。
赞赏任何建议。
谢谢。 。
修订:
"这是后台工作,下载上传报表
"我使用OLEDB安装"应用程序从一个Excel文件的原因,我想程序运行,即使电脑没有一个Excel数据
"我把数据到主目录,然后主目录,如果有一个值一个定时器将开始下载过程
"这是定时器,开始下载过程
Private Sub bckReport_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bckReport.DoWork
Dim MyConnection As OleDbConnection
Dim DtSet As DataSet
Dim MyCommand As OleDbDataAdapter
Me.Invoke(safeLogs, "Connecting to FTP Server...", "", ftpSite, saveTo)
Dim request As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing
request = DirectCast(WebRequest.Create(ftpSite + Format(Now, "ddMMyyyy")), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential(username, password)
response = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
Dim CrLf As Char() = {vbCr, vbLf}
Dim lines() As String = reader.ReadToEnd().Split(CrLf, StringSplitOptions.RemoveEmptyEntries)
Dim ctr As Integer = 0
For i As Integer = 0 To UBound(lines)
If lines(i).EndsWith(".xls") Then ctr = ctr + 1
Next i
Me.Invoke(safeLogs, "Downloading " & ctr & " Upload Report(s)...", "", ftpSite + Format(Now, "ddMMyyyy") + "/", saveTo)
For i As Integer = 0 To UBound(lines)
TimerReport.Enabled = False
If Not Directory.Exists(saveTo + "tmp\") Then
Directory.CreateDirectory(saveTo + "tmp\")
End If
If Not File.Exists(saveTo + "tmp\" + lines(i)) And lines(i).EndsWith(".xls") Then
Dim outputStream As New FileStream(saveTo + "tmp\" + lines(i), FileMode.Create)
Dim ftpStream As Stream
Try
request = DirectCast(FtpWebRequest.Create(New Uri(ftpSite + Format(Now, "ddMMyyyy") + "/" + lines(i))), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.UseBinary = True
request.Credentials = New NetworkCredential(username, password)
response = DirectCast(request.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim bufferSize As Integer = 10000 'size of maximum download
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
outputStream.Close()
MyConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source='" & saveTo + "tmp\" + lines(i) & " '; " & "Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"";")
MyCommand = New OleDbDataAdapter("SELECT * FROM [" + Mid(lines(i), 1, Len(lines(i)) - 4) + "$];", MyConnection)
MyCommand.TableMappings.Add("Table", "Attendence")
DtSet = New DataSet
MyCommand.Fill(DtSet)
Dim safePopulateDatGridView As New PopulateDatGridViewSafe(AddressOf PopulateDatGridView)
Me.Invoke(safePopulateDatGridView, DtSet)
MyConnection.Close()
For j As Integer = 1 To dtgView.RowCount - 1
If dtgView.Item(1, j).Value = "" And dtgView.Item(3, j).Value = "" Then
Exit For
End If
'Job Information | FTP Site | Excel File Location | Image Count
downloadList.Add(dtgView.Item(1, j).Value + "|" + ftpSite + Format(Now, "ddMMyyyy") + "/" + dtgView.Item(3, j).Value + "|" + saveTo + "tmp\" + lines(i) + "|" + dtgView.Item(5, j).Value)
Next j
Catch ex As Exception
Me.Invoke(safeLogs, ex.Message.ToString + " Downloading again...", "Error in Upload Report.", ftpSite + Format(Now, "ddMMyyyy") + "\" + lines(i), saveTo + lines(i))
outputStream.Close()
File.Delete(saveTo + "tmp\" + lines(i))
i = i - 1
End Try
End If
Next i
End Sub
"我分开从总表中的数据,把3列出每个为每个BackgrounWorker一个过程
{C}
"BACKGROUNDWORKERS
"我有相同的过程不同的是变量和函数我访问
"FTP1 = FTP文件站点
"list1的列表下载
'文件名1 =文件名
'request1 =的FtpWebRequest
"响应1 = FtpWebResponse
虽然list1.Count'当这个BackgorundWorker启动,它会不会停止从列表中得到(DO> 0)
Private Sub bckDownloader1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bckDownloader1.DoWork
Me.Invoke(safeButtonEnable, 2)
Dim downloaded As Long
Dim elapseTime As New Stopwatch
Dim safeDownloadStatus As New DownloadStatusSafe1(AddressOf DownloadStatus1)
Do While list1.Count > 0
Dim lstArr As Object = Split(list1.Item(0), "|")
Dim saveLocation As String = saveTo + Format(Now, "ddMMyyyy") + "\" + lstArr(0) + "\"
If Not Directory.Exists(saveTo + Format(Now, "ddMMyyyy") + "\") Then
Directory.CreateDirectory(saveTo + Format(Now, "ddMMyyyy") + "\")
End If
If Not Directory.Exists(saveTo + Format(Now, "ddMMyyyy") + "\" + lstArr(0)) Then
Directory.CreateDirectory(saveTo + Format(Now, "ddMMyyyy") + "\" + lstArr(0))
End If
Dim logStr As String
Dim del As String = list1.Item(0)
ftp1 = lstArr(1)
fileName1 = Split(ftp1, "/")(UBound(Split(ftp1, "/")))
If Not (File.Exists(saveLocation + Split(lstArr(2), "\")(UBound(Split(lstArr(2), "\"))))) Then
File.Copy(lstArr(2), saveLocation + Split(lstArr(2), "\")(UBound(Split(lstArr(2), "\"))))
End If
File.Delete(lstArr(2))
If Not (File.Exists(saveLocation + fileName1)) Then
Dim outputStream As New FileStream(saveLocation + fileName1, FileMode.Create)
Dim ftpStream As Stream
Try
downloaded = 0
elapseTime.Reset()
elapseTime.Start()
Me.Invoke(safeLogs, "Downloading " & fileName1, "", ftp1, saveLocation)
Me.Invoke(safeDownloadStatus, fileName1, 0, 0, 0, 2)
Dim FTPfileSize As Long = GetFTPFileSize1(saveLocation)
If FTPfileSize > 0 Then
logStr = lstArr(0) + "," + Format(Now, "ddMMyyyy") + "," + fileName1 + "," + Format(Now, "hh:mm:ss tt").ToString + "," + lstArr(3)
request1 = DirectCast(FtpWebRequest.Create(New Uri(ftp1)), FtpWebRequest)
request1.Method = WebRequestMethods.Ftp.DownloadFile
request1.UseBinary = True
request1.Credentials = New NetworkCredential(username, password)
response1 = DirectCast(request1.GetResponse(), FtpWebResponse)
ftpStream = response1.GetResponseStream()
Dim bufferSize As Integer = 10000 'size of maximum download
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
If bckDownloader1.CancellationPending Then 'If user abort download
Me.Invoke(safeButtonEnable, 1)
outputStream.Close()
File.Delete(saveLocation + fileName1)
Exit Do
End If
downloaded = downloaded + readCount
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
Me.Invoke(safeDownloadStatus, fileName1, elapseTime.ElapsedMilliseconds, downloaded, FTPfileSize, 1)
End While
Me.Invoke(safeLogs, fileName1 + " has been downloaded...", "Panel(1) Downloading File.", ftp1, saveLocation + fileName1)
logStr = logStr + "," + Format(Now, "hh:mm:ss tt").ToString
elapseTime.Stop()
list1.Remove(del)
Else
outputStream.Close()
File.Delete(saveLocation + fileName1)
End If
Catch ex As Exception
Try
Me.Invoke(safeLogs, ex.Message.ToString + " Downloading again...", "Panel(1) Downloading File.", ftp1, saveLocation + fileName1)
outputStream.Close()
File.Delete(saveLocation + fileName1)
Catch ex1 As Exception
End Try
End Try
Else
Me.Invoke(safeLogs, fileName1 + " has been downloaded...", "Panel(1) Downloading File.", ftp1, saveLocation + fileName1)
logStr = fileName1 + "already exist in folder directory " + saveLocation
elapseTime.Stop()
list1.Remove(del)
End If
Loop
Me.Invoke(safeDownloadStatus, "", 0, 0, 0, 3)
End Sub