1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| Public Class VideoParam
Private _ExeFile As String
Public Property ExeFile() As String
Get
Return _ExeFile
End Get
Set(ByVal value As String)
_ExeFile = value
End Set
End Property
Private _CommandLine As String
Public Property CommandLine() As String
Get
Return _CommandLine
End Get
Set(ByVal value As String)
_CommandLine = value
End Set
End Property
Private _SyncMode As Boolean
Public Property SyncMode() As Boolean
Get
Return _SyncMode
End Get
Set(ByVal value As Boolean)
_SyncMode = value
End Set
End Property
End Class
Public Class clsVideoProcessing
Private _ProcessId As Integer
Private _ListOfAsyncProcess As New SortedList(Of Integer, Process)
Private _listOfSyncProcess As New SortedList(Of Integer, Process)
Private _Tasks As New SortedList(Of Integer, VideoParam)
Event ProcessOutPutDataReceived(ByVal Sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
Event ProcessErrorDataReceived(ByVal Sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
Event ProcessExited(ByVal Sender As Object, ByVal e As System.EventArgs)
Private Sub New(ByVal VideoParam As VideoParam)
Me.Add(VideoParam)
End Sub
Public Sub Add(ByVal VideoParam As VideoParam)
Dim Process As New Process
AddHandler Process.OutputDataReceived, AddressOf OutputDataReceived
AddHandler Process.ErrorDataReceived, AddressOf ErrorDataReceived
AddHandler Process.Exited, AddressOf Exited
'MonProcess.SynchronizingObject = Me
Process.EnableRaisingEvents = True
Process.StartInfo.FileName = VideoParam.ExeFile
Process.StartInfo.Arguments = VideoParam.CommandLine
'MonProcess.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
Process.StartInfo.CreateNoWindow = True ' True
Process.StartInfo.UseShellExecute = False ' False
Process.StartInfo.RedirectStandardOutput = True
Process.StartInfo.RedirectStandardInput = True
Process.StartInfo.RedirectStandardError = True
If VideoParam.SyncMode Then
Me._listOfSyncProcess.Add(Me._ProcessId, Process)
Else
Me._ListOfAsyncProcess.Add(Me._ProcessId, Process)
End If
Me._Tasks.Add(Me._ProcessId, VideoParam)
Me._ProcessId += 1
End Sub
Public Sub StartAll()
Dim Process As Process
If _ListOfAsyncProcess.Count > 0 Then
For Each kvp As KeyValuePair(Of Integer, Process) In _ListOfAsyncProcess
Process = kvp.Value
Process.Start()
Process.BeginOutputReadLine()
Process.BeginErrorReadLine()
Next
End If
If _listOfSyncProcess.Count > 0 Then
Process = _listOfSyncProcess.Item(0)
Process.Start()
Process.BeginOutputReadLine()
Process.BeginErrorReadLine()
End If
End Sub
Public Sub StopAll()
Dim Process As Process
For Each kvp As KeyValuePair(Of Integer, Process) In _ListOfAsyncProcess
Process = kvp.Value
Process.Kill()
Next
For Each kvp As KeyValuePair(Of Integer, Process) In _listOfSyncProcess
Process = kvp.Value
Process.Kill()
Next
End Sub
Public Property Tasks() As SortedList(Of Integer, VideoParam)
Get
Return _Tasks
End Get
Set(ByVal value As SortedList(Of Integer, VideoParam))
_Tasks = value
End Set
End Property
Private Sub OutputDataReceived(sender As Object, e As System.Diagnostics.DataReceivedEventArgs)
RaiseEvent ProcessOutPutDataReceived(sender, e)
End Sub
Private Sub ErrorDataReceived(sender As Object, e As System.Diagnostics.DataReceivedEventArgs)
RaiseEvent ProcessErrorDataReceived(sender, e)
End Sub
Private Sub Exited(sender As Object, e As System.EventArgs)
Dim Process As Process = CType(sender, Diagnostics.Process)
Dim ProcessId As Integer
If Me._listOfSyncProcess.ContainsValue(CType(sender, Process)) Then
' Process synchrone terminé
ProcessId = Me._listOfSyncProcess.IndexOfValue(CType(sender, Diagnostics.Process))
Me._listOfSyncProcess.Remove(ProcessId)
Me.StartAll()
ElseIf Me._ListOfAsyncProcess.ContainsValue(CType(sender, Diagnostics.Process)) Then
' Process asynchrone terminé
ProcessId = Me._ListOfAsyncProcess.IndexOfValue(CType(sender, Diagnostics.Process))
Me._ListOfAsyncProcess.Remove(ProcessId)
End If
Me._Tasks.Remove(ProcessId)
Process.Close()
End Sub
End Class |
Partager