我想在VB.Net中建立一個日誌解析器,它將採用IIS日誌並將它們插入到數據庫中。從來沒有真正做出完整的桌面應用程序,我遇到了一些絆腳石,所以請原諒我,如果我的問題非常不瞭解;我正在學習跑步時走路。VB.Net - 從後臺工作人員更新進度欄
說我看起來與工作像這樣的代碼:
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim logfile = "C:\ex111124.log"
Dim FileLength As Long = New System.IO.FileInfo(logfile).Length
logFileLabel.Text = logfile
Dim objReader As New System.IO.StreamReader(logfile)
Do While objReader.Peek() <> -1
OngoingLog.AppendText(objReader.ReadLine)
'BackgroundWorker1.ReportProgress(e.percentProgress)
Loop()
objReader.Close()
objReader = Nothing
End Sub
Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
'Me.crunchingProgress.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Close()
End Sub
所以功能工作,打開這個窗口,當它開始讀取日誌文件,並與所有當前行的更新一個文本框閱讀,但我也希望它在我的主線程中更新一個名爲crunchingProgress的進度條。
任何幫助將不勝感激。
謝謝,這個工作伴隨着加入一些調用委派線程安全的潛艇其他控制更新 – 2013-03-11 19:38:57
你並不需要調用調用一個ProgressChanged事件。它在調用RunWorkerAsync的同一線程中觸發(通常是UI線程)。 http://stackoverflow.com/questions/1968222/backgroundworker-still-needs-to-call-invoke – Steve 2013-03-11 19:53:11