我正在制作一个程序,该程序打开ffmpeg并运行一个作业,然后用户可以通过按按钮退出该作业。
我收到“流中已经开始异步读取操作”。当我尝试第二次退出工作时,在“ BeginOutputReadLine”上显示。
如果我尝试将“ Dim p3 As New Process”放入“ test_run()”内,则会得到:“ StandardIn尚未重定向。”如果我尝试一次退出工作,请在“ StandardInput.WriteLine”上输入。
这是我的代码的有效示例。
Imports System.IO
Public Class Form1
Dim quote As String = """"
Dim p3 As New Process
Function generate_batch(aargs As String) As Integer
Dim sb As New System.Text.StringBuilder
sb.AppendLine("@echo off")
sb.AppendLine("CD " + quote + Application.StartupPath + quote)
sb.AppendLine("ffmpeg " + aargs)
sb.AppendLine("(goto) 2>nul & del " + quote + "%~f0" + quote)
IO.File.WriteAllText(Path.GetTempPath + "ffmpeg.bat", sb.ToString())
End Function
Private Sub test_run()
If TextBox1.Text = "1" Then
p3.StandardInput.WriteLine("q")
p3.Close()
Else
generate_batch(arg.Text)
With p3.StartInfo
.FileName = Path.GetTempPath + "\ffmpeg.bat"
.CreateNoWindow = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.RedirectStandardInput = True
.UseShellExecute = False
End With
AddHandler p3.OutputDataReceived, AddressOf consoleOutputHandler
AddHandler p3.ErrorDataReceived, AddressOf consoleErrorHandler
p3.Start()
p3.BeginOutputReadLine()
p3.BeginErrorReadLine()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
test_run()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = "1"
test_run()
End Sub
'///////////////////
'/ Standard Output /
'///////////////////
Private Delegate Sub consoleOutputDelegate(ByVal outputString As String)
Private Sub consoleOutput(ByVal outputString As String)
'Invoke the sub to allow cross thread calls to pass safely
If Me.InvokeRequired Then
Dim del As New consoleOutputDelegate(AddressOf consoleOutput)
Dim args As Object() = {outputString}
Me.Invoke(del, args)
Else
'Now we can update your textbox with the data passed from the asynchronous thread
RichTextBox1.AppendText(String.Concat("", outputString, Environment.NewLine))
End If
End Sub
'Catch the Standard Output
Sub consoleOutputHandler(ByVal sendingProcess As Object, ByVal outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
'If the Current output line is not empty then pass it back to your main thread (Form1)
consoleOutput(outLine.Data)
End If
End Sub
'///////////////////
'// Error Output ///
'///////////////////
Private Delegate Sub consoleErrorDelegate(ByVal errorString As String)
Private Sub consoleError(ByVal errorString As String)
'Invoke the sub to allow cross thread calls to pass safely
If Me.InvokeRequired Then
Dim del As New consoleErrorDelegate(AddressOf consoleError)
Dim args As Object() = {errorString}
Me.Invoke(del, args)
Else
'Now we can update your textbox with the data passed from the asynchronous thread
RichTextBox1.AppendText(String.Concat("", errorString, Environment.NewLine))
End If
End Sub
'Catch the Error Output
Private Sub consoleErrorHandler(ByVal sendingProcess As Object, ByVal errLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(errLine.Data) Then
'If the Current error line is not empty then pass it back to your main thread (Form1)
consoleError(errLine.Data)
End If
End Sub
End Class
“ arg.text”里面有什么
-re -i video.mp4 -c copy -f rtp_mpegts rtp://127.0.0.1:10000?pkt_size=1316
我希望这项工作可以无限次地停止和启动,但是我一直遇到问题,我不知道为什么。
我想知道是否必须停止异步读取操作,但是我不知道这是否可能。