Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

i have one problem with this code below, i try to using cmd and add some condition on it to control or detect when cmd is finish process convert .mp4 by FFMEG, then it go to exit the cmd windows. but i got problem with logic thinking, please help me to edit this code if someone use to experience with it.

thanks you!

What I have tried:

VB
Dim active As Boolean = True

While active
  If active = True Then
     Process.Start("cmd.exe", "/k ffmpeg -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4") 'create file inside the folder
                active = False
  Else
    MsgBox("Finish")
   End If
End While
Posted
Updated 1-May-21 6:49am

Use the return value from the Start method: Process.Start Method (System.Diagnostics) | Microsoft Docs[^].
 
Share this answer
 
Comments
Mr.Kim2050 1-May-21 12:18pm    
Richard, could you give me example of it ?
Richard MacCutchan 1-May-21 12:21pm    
It's in the documentation. Please use the information that is already available.
Mr.Kim2050 1-May-21 13:55pm    
@Richard , can you advice me how to correct this code? i try to detect the cmd.exe when it finish job and close the main windows then timer will stop. but i can't set the right condition on it. could you help me how to do ?


Dim p As System.Diagnostics.Process
p = Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
Timer1.Start()

While p Is Nothing = True
Timer1.Stop()
MsgBox("Hello")
End While
Mr.Kim2050 2-May-21 4:28am    
i have try to using *.hasExited as you say but not working well, could you help check why ?



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCovert.Click
Dim p As Process
Dim active As Boolean = True
Timer1.Start()
p = Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
if p.HasExited then
Timer1.Stop()
msgbox("Close")
end if
end sub
The /k switch tells the CMD process to stay running, so it'll never end. Change that to /c if you want the process to stop.

If you want to know when ffmpeg is done and exists, you're going to have to skip launching CMD and just launch ffmpeg directly.
 
Share this answer
 
Comments
Mr.Kim2050 1-May-21 13:00pm    
^^ , Oh Yes, it really work. thanks you
Mr.Kim2050 1-May-21 13:01pm    
i have long time never touch with vb.net, since i finished from school and i just try to use with FFMEG first time. so i meet lot of confuse with logic and syntax sometime . thanks with your share
Mr.Kim2050 1-May-21 13:53pm    
@Dave Kreskowiak, can you advice me how to correct this code? i try to detect the cmd.exe when it finish job and close the main windows then timer will stop. but i can't set the right condition on it. could you help me how to do ?


Dim p As System.Diagnostics.Process
p = Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
Timer1.Start()

While p Is Nothing = True
Timer1.Stop()
MsgBox("Hello")
End While
Dave Kreskowiak 1-May-21 15:07pm    
p will never be Nothing. It will always be a Process object, even if the process isn't running anymore.

Look at the documentation on the Process class. It has a HasExited property that will tell you if the process is still running or not. So, in your code:
    While (Not p.HasExited)
        Thread.Sleep(500)
    End While
    Timer1.Stop()
    MsgBox("Whatever...")
Mr.Kim2050 2-May-21 1:38am    
hi , could you have told me, seem that not working well, i dont know why. do you have any solution more ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900