我是背景工作者的新手。任何人都可以查看我的代码有什么问题吗?我的问题是从另一个函数调用reportprogress来改变我的UI上的任何内容。
鉴于:PopulateFolders是我的背景工作者。
此代码来自另一个类,它从主窗体调用setStatus。
Public Sub populate(Optional ByVal iFilePath As String = "")
'Dim strFilePaths() As String
'strFilePaths = IO.Directory.GetFiles(_Path)
Dim FileName() As String
If iFilePath = "" Then iFilePath = _Path
Dim counter As Integer = My.Computer.FileSystem.GetFiles(Path.GetDirectoryName(iFilePath)).Count
Dim i As Integer = 1
For Each file__1 As String In Directory.GetFiles(Path.GetDirectoryName(iFilePath))
frmMain.lblProgess.Text = "Getting info of:" & file__1
FileContent.Add(file__1)
FileName = Split(file__1, "\")
FileName(0) = IIf(FileName(UBound(FileName)) <> "", FileName(UBound(FileName)), FileName(UBound(FileName) - 1))
i = i + 1
If i Mod 25 = 0 Then frmMain.SetStatus(FileName(0), (i / counter) * 100)
Next
For Each folder As String In Directory.GetDirectories(Path.GetDirectoryName(iFilePath))
If folder(folder.Length - 1) <> "\" Then folder = folder & "\"
populate(folder)
Next
End Sub
这是我主班的代码。从PopulateFCollections我可以毫无问题地更新我的UI。
-----------------------------------
Public Structure ControlWithText
Public ControlName As Control
Public Text As String
Public Sub New(ByVal ctrl As Control, ByVal text As String)
Me.ControlName = ctrl
Me.Text = text
End Sub
End Structure
Private Sub PopulateFolders_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles PopulateFolders.DoWork
PopulateFCollections(folderTo)
End Sub
Private Sub PopulateFCollections(ByVal item As List(Of clsFolder))
Dim msg As String = ""
Dim iFldr As New List(Of clsFolder)
Dim iFldrTemp As New clsFolder
Dim i As Integer = 1
PopulateFolders.ReportProgress(0)
For Each iFolder As clsFolder In item
PopulateFolders.ReportProgress(CInt((i / item.Count) * 100), New ControlWithText(lblProgess, "Extracting FIles from:..." & iFolder.FolderName))
msg = msg & iFolder.FolderName & "; " & iFolder.FilePath & ";" & _
iFolder.Username & "; " & iFolder.Password & vbCrLf
iFldrTemp = iFolder
iFldrTemp.populate()
iFldr.Add(iFldrTemp)
i = +1
Next
MsgBox(msg)
End Sub
Public Sub SetStatus(ByVal txt As String, ByVal percentage As Integer)
PopulateFolders.WorkerReportsProgress = True
PopulateFolders.ReportProgress(percentage, New ControlWithText(lblProgess, "Adding.. " & txt & ".."))
End Sub
提前感谢您的帮助。
答案 0 :(得分:0)
我刚刚找到了问题的答案,但是如果你有更好的解决方案。我足够开放接受更正,所以我可以做,知道,更好地理解。
这是我的解决方案,我不太了解,但足以让我完成我的部分工作。
我只是创建一个新的变量实例来处理我的对象。
Public Shared bgw As System.ComponentModel.BackgroundWorker
Public Shared lblCurProgess As Label
来自Private Sub PopulateFCollections的我设置了bgw和标签
Private Sub PopulateFCollections(ByVal item As List(Of clsFolder))
bgw = PopulateFolders
lblCurProgess = lblProgess
Dim msg As String = ""
Dim iFldr As New List(Of clsFolder)
Dim iFldrTemp As New clsFolder
Dim i As Integer = 1
PopulateFolders.ReportProgress(0)
For Each iFolder As clsFolder In item
PopulateFolders.ReportProgress(CInt((i / item.Count) * 100), New ControlWithText(lblProgess, "Extracting FIles from:..." & iFolder.FolderName))
msg = msg & iFolder.FolderName & "; " & iFolder.FilePath & ";" & _
iFolder.Username & "; " & iFolder.Password & vbCrLf
iFldrTemp = iFolder
iFldrTemp.populate()
iFldr.Add(iFldrTemp)
i = +1
Next
MsgBox(msg)
End Sub
这就是我如何从另一个类/函数中调用它
If i Mod 25 = 0 Then frmMain.bgw.ReportProgress((i / counter) * 100, New frmMain.ControlWithText(frmMain.lblCurProgess, "Getting info... " & FileName(0) & ".."))
它只是有效。我现在可以看到我的移动:D
如果你有更好的解决方案,请发给你答案。我将非常感谢有人纠正我,以便能够做得更好并学习。
谢谢,