Tia*_*ago 2 vb.net backgroundworker
当我的后台工作人员正在完成他的工作时,我想更改主要表单中的标签,告诉他们当时正在采取什么流程.
该应用程序适用于某些文件,因此我需要传递应用程序正在处理的文件的信息.
我的背景工作者
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If (Not System.IO.Directory.Exists(caminho & "\results")) Then
System.IO.Directory.CreateDirectory(caminho & "\results")
End If
For Each folder As String In System.IO.Directory.GetDirectories(caminho)
Dim split As String() = folder.Split("\")
Dim parentFolder As String = split(split.Length - 1)
If (Not System.IO.Directory.Exists(caminho & "\results" & "\" & parentFolder)) Then
System.IO.Directory.CreateDirectory(caminho & "\results" & "\" & parentFolder)
End If
For Each file As String In System.IO.Directory.GetFiles(folder)
output = file
'Dim imgFile As System.Drawing.Image = System.Drawing.Image.FromFile(file)
Dim thumbimage As Bitmap
Dim originalimage As Bitmap
Dim width As Integer = TextBox2.Text '# this is the max width of the new image
Dim height As Integer = TextBox3.Text '# this is the max height of the new image
originalimage = System.Drawing.Image.FromFile(file)
thumbimage = New Bitmap(width, height)
Dim novonome As String = Path.GetFileNameWithoutExtension(file)
Dim gr As Graphics = Graphics.FromImage(thumbimage)
gr.DrawImage(originalimage, 0, 0, width, height)
thumbimage.Save(caminho & "\results" & "\" & parentFolder & "\" & novonome & ".png", Imaging.ImageFormat.Png)
thumbimage.Dispose()
originalimage.Dispose()
Next
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
我的进步变化:
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Label3.Text = "Working file " & output
End Sub
Run Code Online (Sandbox Code Playgroud)
是不是应该使用它正在运行的文件更改form1中的label3.text?基本上没有任何标签3.
我的输出var在Public Class Form1下声明.
提前致谢.
确保您的BackgroundWorker对象具有 WorkerReportsProgress = True
你必须在循环中调用进度:
For Each file As String In System.IO.Directory.GetFiles(folder)
BackgroundWorker1.ReportProgress(0, file)
...
Run Code Online (Sandbox Code Playgroud)
然后在Progress事件中,阅读状态:
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If e.UserState IsNot Nothing Then
Label3.Text = "Working file " & e.UserState.ToString
End If
End Sub
Run Code Online (Sandbox Code Playgroud)