BackgroundWorker wird mehrmals ausgeführt?

  • VB.NET
  • .NET (FX) 4.0

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    BackgroundWorker wird mehrmals ausgeführt?

    Hallo!
    Für mein derzeitiges Projekt muss ich einen BackgroundWorker starten. Das mache ich so:

    VB.NET-Quellcode

    1. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    2. If Button3.Text = "Start" Then
    3. Button3.Text = "Cancel"
    4. Compressor.RunWorkerAsync()
    5. Else
    6. Button3.Text = "Start"
    7. Compressor.CancelAsync()
    8. ProgressBar1.Value = 0
    9. End If
    10. End Sub

    Danach wird folgender Code ausgeführt:

    VB.NET-Quellcode

    1. Private Sub Compressor_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles Compressor.DoWork
    2. For i = 1 To 10
    3. If Compressor.CancellationPending = True Then
    4. e.Cancel = True
    5. Exit For
    6. Else
    7. Dim g As New Tools
    8. g.CompressVideo(input, output)
    9. System.Threading.Thread.Sleep(500)
    10. Compressor.ReportProgress(i * 10)
    11. End If
    12. Next
    13. End Sub

    Der obige Code ruft folgenden Code auf:

    VB.NET-Quellcode

    1. Public Sub CompressVideo(ByVal sourceFile As String, ByVal outputPath As String)
    2. Dim P As New Process
    3. Dim i As New ProcessStartInfo
    4. Dim sr As StreamReader
    5. Dim ou As New FileInfo(sourceFile)
    6. i.FileName = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\used\ffmpeg.exe"
    7. i.Arguments = " -i """ + sourceFile + """ -ar 22050 -qscale " _
    8. & 50 & " -y """ + My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\used\temp\" & ou.Name.Replace(".mp4", "").Replace(".wmv", "").Replace(".flv", "").Replace(".avi", "") + """"
    9. i.UseShellExecute = False
    10. i.WindowStyle = ProcessWindowStyle.Hidden
    11. i.RedirectStandardError = True
    12. i.RedirectStandardOutput = True
    13. i.CreateNoWindow = True
    14. P.StartInfo = i
    15. P.Start()
    16. sr = P.StandardError
    17. Dim Name As String = ou.Name.Replace(".mp4", ".rm").Replace(".wmv", ".rm").Replace(".flv", ".rm").Replace(".avi", ".rm")
    18. ZipFile.CreateFromDirectory(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\used\temp\", outputPath & "\" & Name)
    19. End Sub

    Leider wird der BackgroundWorker mehrmals gestartet, weshalb ich eine Exception erhalte, das die Datei bereits vorhanden ist.
    Nun habe ich schon einiges versucht, zum Beispiel mit Booleans gearbeitet, um das wirklich nur einmal starten zu lassen, leider ohne Erfolg.
    Wie kann ich den BackgroundWorker wirklich nur einmal starten lassen?

    MfG,
    C8002.
    “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”
    -Unbekannt

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „C8002“ ()

    ErfinderDesRades schrieb:

    fein - und?

    Wie kann ich den BackgroundWorker wirklich nur einmal starten lassen?

    Deine Antwort passt nicht wirklich. :/
    “If debugging is the process of removing software bugs, then programming must be the process of putting them in.”
    -Unbekannt