Click here to Skip to main content
14,929,445 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[^].
   
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
Richard MacCutchan 2-May-21 7:02am
   
You need a loop with a delay in it to test the status repeatedly until the external process is complete. Something like:
Function DoTest() As Integer
    Dim p As System.Diagnostics.Process
    p = System.Diagnostics.Process.Start("cmd.exe", <your command parameters>)
    While p.HasExited = False
        System.Threading.Thread.Sleep(500)
    End While
End Function
Mr.Kim2050 2-May-21 7:24am
   
if i dont need System.Threading.Thread.Sleep(500) , to delay isn't ok ???
Mr.Kim2050 2-May-21 7:25am
   
because i dont know when FFEMG is finish to convert the .MP4 ready. so how can we do ?
Richard MacCutchan 2-May-21 7:45am
   
The above code sample does what you want. You just need to modify it to handle your specific conditions.
Mr.Kim2050 2-May-21 8:03am
   
thanks let me try it after this.
Mr.Kim2050 3-May-21 1:08am
   
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnCovert.Click

btnCovert.Enabled = False
Dim FileName1 As String = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName) 'Get only Flie name without extension
Dim FileName2 As String = OpenFileDialog1.FileName
Dim programName As String = "ffmpeg.exe"
System.Diagnostics.Process.Start(Application.StartupPath & "\" & programName)
'Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
'============================================================================

Dim p As System.Diagnostics.Process
Dim active As Boolean = True
AMins = 0
ASecond = 0
'p = Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")
p = System.Diagnostics.Process.Start("cmd.exe", "/c ffmpeg.exe -i " & FileName2 & " -c:v libx264 " & "Down\" & FileName1 & ".mp4")

Timer1.Start()
While p.HasExited = False

btnFileOutInfor.PerformClick()
Timer1.Stop()
End While

'============================================================================


End Sub
Richard MacCutchan 3-May-21 3:11am
   
I gave you sample code above that works. Why are you still trying to use code that does not? What did you expect to happen with the Timer you are running?
Mr.Kim2050 3-May-21 13:26pm
   
what i expected with this code do is like this, when i click the button convert it will go to call cmd.exe and FFEMG.exe start to process covert the .TS to .MP4 by using timer to count the how many minutes will spend to finish these jobs and after cmd.exe and FFEMG.exe finish the jobs, it will go to close windows and timer also stop to count period too. i try to find this solution but i dont know how to do.
Mr.Kim2050 4-May-21 0:10am
   
ooh yes, it working right now, thanks you so much for your help, finally i could finish it as what i dream.
Mr.Kim2050 3-May-21 1:10am
   
i try to follow using while loop but it still meet problem that timer is not running , i dont know why.
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.
   
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 ?
Mr.Kim2050 2-May-21 4:00am
   
i have follow what your code but not working well as what i need.
Mr.Kim2050 2-May-21 4:29am
   
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
Dave Kreskowiak 2-May-21 10:19am
   
So you changed out the while statement for an if? That's not going to work.
Mr.Kim2050 2-May-21 11:45am
   
it still meet the problem to call timer.stop() , i dont know why, i have change from IF to WHILE.
Dave Kreskowiak 2-May-21 12:03pm
   
What is the timer for?
Mr.Kim2050 3-May-21 0:58am
   
i want to create one program which able to convert .TS file to .MP4 file, by using one button click to make process convert with cmd.exe and FFMEG commend. when click button convert i want to know how many minutes will spend to convert this process finish and when cmd.exe is done, the timer also stop.
Dave Kreskowiak 3-May-21 11:14am
   
Your "Timer" better be an instance of the StopWatch class. Calling it a "Timer" suggest it's one of the Timer classes, which are not used for timing how long something tasks to execute.
Mr.Kim2050 3-May-21 13:22pm
   
i am not sure how to build this code , could you help to build this could out ?
Mr.Kim2050 3-May-21 13:26pm
   
what i expected with this code do is like this, when i click the button convert it will go to call cmd.exe and FFEMG.exe start to process covert the .TS to .MP4 by using timer to count the how many minutes will spend to finish these jobs and after cmd.exe and FFEMG.exe finish the jobs, it will go to close windows and timer also stop to count period too. i try to find this solution but i dont know how to do.
Dave Kreskowiak 3-May-21 17:17pm
   
Sure. I just have have 41 projects I'm currently working on at work.

No, I'm not writing your code for you, but, from what you described and posted so far, you get this:
Private Function ConvertAndTime(sourceFilename As String, destinationFilename As String) As Integer
    Dim process As Process
    Dim sw As New StopWatch
 
    sw.Start
    process = Process.Start("cmd", "/c ffmpeg.exe -i " & sourceFilename & " -c:v libs264 Down\" & destinationFilename & ".mp4")
    process.WaitForExit
    sw.Stop

    return sw.ElapsedMilliseconds
End Function

It will execute the ffmpeg command, timing how long it takes, and returning that time in milliseconds.
Mr.Kim2050 4-May-21 0:10am
   
ooh yes, it working right now, thanks you so much for your help, finally i could finish it as what i dream

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