代码之家  ›  专栏  ›  技术社区  ›  Tiago

背景工作者的VB网络问题

  •  2
  • Tiago  · 技术社区  · 8 年前

    当我的后台工作人员在做他的工作时,我想更改一个主窗体中的标签,告诉他这段时间内的流程。

    这个应用程序可以处理一些文件,所以我需要传递这个应用程序正在处理的文件的信息。

    我的背景工作者

        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
    

    我的进度变更:

        Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Label3.Text = "Working file " & output
    End Sub
    

    不应该用正在运行的文件更改Form1中的label3.text吗?基本上,Label3没有发生任何变化。

    我的输出变量在公共类Form1下声明。

    事先谢谢。

    1 回复  |  直到 8 年前
        1
  •  5
  •   LarsTech    8 年前

    确保BackgroundWorker对象具有 WorkerReportsProgress = True

    您必须从循环中调用进度:

    For Each file As String In System.IO.Directory.GetFiles(folder)
      BackgroundWorker1.ReportProgress(0, file)
      ...
    

    然后在进度事件中,读取状态:

    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
    
    推荐文章