Hi Dw.

I have a project that uses Process to marge some files, now because the merging can take quite sometime I've used BackgroundWork. The main problem I'm facing is to know when the Process is done because I want to delete the files that it was merging with so that the file will only contain the finished output which is a file that is produced by merging these other files.

I have this code.

 Private proc1 As New Process


 proc1.StartInfo.FileName = Application.StartupPath & "\ffmpeg.exe"
 proc1.StartInfo.Arguments = String.Format(" -I " & "C:\V.mp4" & " -I " & "C:\M.wav c:\final.mp4")
 proc1.StartInfo.UseShellExecute = False
 proc1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
 proc1.StartInfo.RedirectStandardInput = True
 proc1.StartInfo.RedirectStandardOutput = True
 proc1.StartInfo.CreateNoWindow = True
 proc1.Start()

Now I need to know when this process is done. I've tried this:

 If proc1.HasExited Then
 Kill("C:\V.mp4")
 Kill("C:\M.wav")
 End if

But sometime is points to Kill("C:\V.mp4") and it says its can't delete this file because another process is busy with this file.

Anyone knows how I can solve this problem?

To just answer your question of what's lock the file, the reason why I'm getting that error is because or should I say its occur only if the files are a bit longer e.g.( V.mp4 is 00:01:09 and M.wav let's say its 00:02:05 because the times are not equal when stopping the recording so that's why I have files with different lengths) so if I marge them up it will take sometime before the final product/output is fully created so the backgroundwork does wait its time and then fire the delete code under the WorkComplete but because Proc1 on the other side is not done, is still using these file or is still merging(the files will remain in used by it till it done) that's why I'm getting that error sometimes not always as I've said its depends on the file lengths.

What I want is a way of checking if the proc1 is done or perhaps be able to get its status whether its done or busy so that when busy I will wait but when done I will delete these files.

I think its now clear.

You said you were using BackgroundWork but it looks to me that you are spawning a separate process. If you actually do it with a BackGroundWorker then you can tell when it is finished by the RunWorkerCompleted event.

I've tried that but the problem was the same. The problem is that I call the Close()

How about using a timer which periodically tests for the existence of the ffmpeg process? For example

Public Class Form1

    Private proc1 As New Process

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        'ffmpeg -y -i d:\test.flv -vcodec copy -acodec copy d:\test.mp4

        proc1.StartInfo.FileName = "ffmpeg"
        proc1.StartInfo.Arguments = "-y -i d:\test.flv -vcodec copy -acodec copy d:\test.mp4"
        proc1.StartInfo.UseShellExecute = False
        proc1.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        proc1.StartInfo.RedirectStandardInput = False
        proc1.StartInfo.RedirectStandardOutput = False
        proc1.StartInfo.CreateNoWindow = False
        proc1.Start()

        Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        If proc1.HasExited Then
            Timer1.Stop()
            MsgBox("done")
        End If

    End Sub

End Class

Appears to do the trick. Just replace MsgBox with a call to your cleanup function.

OK. Note that I set certain flags to the opposite of yours so that I would see the progress of the spawned task.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.