整個始末請看(前情提要)
這裡就接著上集,當我們收集到了這些失敗資訊我們該怎麼辦!
就是再上傳阿,一直上傳到上傳成功為止!
那大家會覺得那就是要讓他不斷地跑上傳上傳上傳的事情吧!!
是的沒錯,就是要讓他跑,它能多快就要他跑多快,
要馬兒跑也要給馬兒吃草(有通電就好了)
這邊居裡貓使用的方法是 BackgroundWorker 的方法,
人如其名,就是再背景執行工作的意思!
不會讓他因為要不斷地跑,使你的其他工作中斷的好方法啊!
好,那我們就看看到底怎麼一個方法可以叫他跑吧!!!
有請程式碼~~~
-------------------------------------------------------------------------------------------------------------------------
- '這次一樣先給大家看看 BackgroundWorker 的長像!
- Public Class Backwork
- 'Public WithEvents background As New System.ComponentModel.BackgroundWorker
- Public Shared background As New System.ComponentModel.BackgroundWorker
- Public Sub New()
- background.WorkerReportsProgress = True
- background.WorkerSupportsCancellation = True
- background.RunWorkerAsync()
- End Sub
- Public Shared Sub main()
- '這邊就是啟動 Backgroundwork 的主要程式段落
- '以及委派工作事件的地方
- background.RunWorkerAsync()
- AddHandler background.DoWork, AddressOf back_DoWork
- End Sub
- Private Shared Sub back_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
- '這裡才是 Backgroundwork 工作的內容
- '這邊要注意到,居裡貓開始使用我們紀錄的 List 來判斷該怎麼樣工作
- '然後當 List 裡面的檔案被上傳成功了就記得要清除該 List 項目喔!!!!
- Dim retry As Boolean = True
- Dim retrytime As Integer = 0
- 'Check upload NAS ro FTP
- If Copy_Delete_File.checkbox = False Then
- While retry
- 'Do upload to NAS back ground work
- For i As Integer = 0 To ErrorList.CountNas - 1
- Try
- File.Copy(Copy_Delete_File.copypath + "\" + ErrorList.NASList(i), Copy_Delete_File.uptonas + "\" + ErrorList.NASList(i), True)
- If ErrorList.CountNas = 0 Then
- 'If NasList count eaual 0, retry end
- retry = False
- Else
- 'retry not end and remove NasList now data then re do for
- retry = True
- ErrorList.RemoveNAS(i)
- Exit For
- End If
- Catch ex As Exception
- 'Retry failure add retry times and error log
- retry = True
- retrytime += 1
- 'Console.WriteLine("Error list upload to NAS retry failured, back do work times : " & retrytime)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Error list upload to NAS retry failured, back do work times : " & retrytime)
- End Try
- Next
- 'If NasList count eaual 0, retry end
- If ErrorList.CountNas = 0 Then
- retry = False
- End If
- End While
- Else
- While retry
- For i As Integer = 0 To ErrorList.CountFtp - 1
- Try
- File.Copy(Copy_Delete_File.copypath + "\" + ErrorList.FTPList(i), Copy_Delete_File.uptoftp + "\" + ErrorList.FTPList(i), True)
- System.Threading.Thread.Sleep(100)
- FTPupload.uploadtoftp()
- System.Threading.Thread.Sleep(100)
- If ErrorList.CountFtp = 0 Then
- 'If NasList count eaual 0, retry end
- retry = False
- Else
- 'retry not end and remove NasList now data then re do for
- retry = True
- ErrorList.RemoveFTP(i)
- Exit For
- End If
- Catch ex As Exception
- 'Retry failure add retry times and error log
- retry = True
- retrytime += 1
- 'Console.WriteLine("Error list upload to FTP retry failured, back do work times : " & retrytime)
- ErrorList.AddLog(Now.ToString("yyyyMMddHHmmss _ ") + "Error list upload to FTP retry failured, back do work times : " & retrytime)
- End Try
- Next
- 'If NasList count eaual 0, retry end
- If ErrorList.CountFtp = 0 Then
- retry = False
- End If
- End While
- End If
- End Sub
- End Class
- '----分割線,居裡貓是在 WriteXML 這個按鈕被工作的時候才要開始做不斷的上傳工作喔!
- Private Sub Btn_WriteXML_Click(sender As Object, e As EventArgs) Handles Btn_WriteXML.Click
- 'Check watch event was raising
- If Watcher.watcher.EnableRaisingEvents = False Then
- Watcher.watcher.EnableRaisingEvents = True
- End If
- Copy_Delete_File.GetPath(TxB_Local.Text, TxB_Remote.Text, TxB_NAS.Text, TxB_FTP.Text, ChB_UploadFTP.Checked)
- FTPupload.Get_FTPinfo(TxB_FTPAddress.Text, TxB_FTPAccount.Text, Txb_FTPPassword.Text)
- Dim wrXML As New XML
- wrXML.LocalPath = TxB_Local.Text
- wrXML.RemotePath = TxB_Remote.Text
- wrXML.NASPath = TxB_NAS.Text
- wrXML.FTPPath = TxB_FTP.Text
- wrXML.Data = "This is a Programming Practices " & Now.ToString
- wrXML.DateTime = Now.ToString
- If ChB_UploadFTP.Checked Then
- wrXML.CheckFTP = "Ture"
- Else
- wrXML.CheckFTP = "Flase"
- End If
- Dim objectStreamWriter As New StreamWriter(TxB_Local.Text + ("\Program Practices " & Now.ToString("_yyyyMMddHHmmss") & ".xml"))
- Dim xmlserialize As New XmlSerializer(wrXML.GetType)
- xmlserialize.Serialize(objectStreamWriter, wrXML)
- objectStreamWriter.Dispose()
- 'Check backwork status
- 'If Not back.IsBusy = True Then
- ' back.RunWorkerAsync()
- 'End If
- '在這裡~~~~
- Backwork.main()
- End Sub
--------------------------------------------------------------------------------------------------------------------------
好的,今天的分享就是利用 BackgroundWorker 的方法來解決我們再次重新上傳的工作,
有了這一層保險我們就可以無慮的繼續別的工作,
他會默默的幫我們不斷嘗試重新上傳~
下集應該就是差不多要完結了~
請各位期待一下了~拜託!感謝~~哈哈哈哈
感謝各位收看!!!!感謝各位先進的教學文~
底下就是這個
1. 上報 練習 _ 選擇資料夾
2. 上報 練習 _ 寫一個 .XML 資料
3. 上報 練習 _ 監控資料夾
4. 上報 練習 _ 複製檔案
5. 上報 練習 _ FTP Server 檔案上傳
6. 上報 練習 _ 上傳錯誤紀錄
7. 上報 練習 _ 使用 BackgroundWorker 再次上傳
8. 上報 練習 _ Log 紀錄
9. 上報 練習 _ 完整程式碼分享
沒有留言:
張貼留言