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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| Imports System.Threading
Public Class VideoParam
Public Sub New(ByVal ExeFile As String, ByVal CommandLine As String, ByVal SyncMode As Boolean)
Me._ExeFile = ExeFile
Me._CommandLine = CommandLine
Me._SyncMode = SyncMode
End Sub
Public Sub New(ByVal ExeFile As String, ByVal CommandLine As String, ByVal SyncMode As Boolean, ByVal TaskName As String)
Me.New(ExeFile, CommandLine, SyncMode)
Me._TaskName = TaskName
End Sub
Private _ExeFile As String
Public ReadOnly Property ExeFile() As String
Get
Return _ExeFile
End Get
End Property
Private _CommandLine As String
Public ReadOnly Property CommandLine() As String
Get
Return _CommandLine
End Get
End Property
Private _SyncMode As Boolean
Public ReadOnly Property SyncMode() As Boolean
Get
Return _SyncMode
End Get
End Property
Private _TaskName As String
Public Property TaskName() As String
Get
Return _TaskName
End Get
Set(ByVal value As String)
_TaskName = value
End Set
End Property
End Class
Public Class CustomDataReceivedEventArgs
Public Sub New(ByVal ProcessId As Integer, ByVal VideoParam As VideoParam, ByVal e As DataReceivedEventArgs)
Me._ProcessId = ProcessId
Me._VideoParam = VideoParam
Me._e = e
End Sub
Private _ProcessId As Integer
Public ReadOnly Property ProcessId As Integer
Get
Return _ProcessId
End Get
End Property
Private _VideoParam As VideoParam
Public ReadOnly Property VideoParam() As VideoParam
Get
Return _VideoParam
End Get
End Property
Private _e As DataReceivedEventArgs
Public ReadOnly Property e As DataReceivedEventArgs
Get
Return _e
End Get
End Property
End Class
Public Class CustomExitedEventArgs
Public Sub New(ByVal ProcessId As Integer, ByVal VideoParam As VideoParam, ByVal e As EventArgs)
Me._ProcessId = ProcessId
Me._VideoParam = VideoParam
Me._e = e
End Sub
Private _ProcessId As Integer
Public ReadOnly Property ProcessId As Integer
Get
Return _ProcessId
End Get
End Property
Private _VideoParam As VideoParam
Public ReadOnly Property VideoParam() As VideoParam
Get
Return _VideoParam
End Get
End Property
Private _e As EventArgs
Public ReadOnly Property e As EventArgs
Get
Return _e
End Get
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 CustomDataReceivedEventArgs)
Event ProcessErrorDataReceived(ByVal Sender As Object, ByVal e As CustomDataReceivedEventArgs)
Event ProcessExited(ByVal Sender As Object, ByVal e As CustomExitedEventArgs)
Public 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
'MonProcess.Start()
'MonProcess.BeginOutputReadLine()
'MonProcess.BeginErrorReadLine()
''MonProcess.WaitForExit()
'MonProcess.Close()
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 ReadOnly Property Tasks() As SortedList(Of Integer, VideoParam)
Get
Return _Tasks
End Get
End Property
Public ReadOnly Property ListOfsyncProcess() As SortedList(Of Integer, Process)
Get
Return _listOfSyncProcess
End Get
End Property
Public ReadOnly Property ListOfAsyncProcess() As SortedList(Of Integer, Process)
Get
Return _ListOfAsyncProcess
End Get
End Property
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 _listOfSyncProcess
Process = kvp.Value
CloseProcess(Process)
Next
_listOfSyncProcess.Clear()
For Each kvp As KeyValuePair(Of Integer, Process) In _ListOfAsyncProcess
Process = kvp.Value
CloseProcess(Process)
Next
_ListOfAsyncProcess.Clear()
End Sub
Private Sub CloseProcess(ByVal Process As Process)
Dim i As Integer
For i = 0 To 4
If Not Process.HasExited Then
' Wait 2 seconds.
Thread.Sleep(2000)
Process.CloseMainWindow()
Else
Exit For
End If
Next i
Process.Kill()
End Sub
Private Function GetId(ByVal Process As Process) As Integer
If Me._listOfSyncProcess.ContainsValue(Process) Then
GetId = Me._listOfSyncProcess.IndexOfValue(Process)
ElseIf Me._ListOfAsyncProcess.ContainsValue(Process) Then
GetId = Me._ListOfAsyncProcess.IndexOfValue(Process)
End If
Return GetId
End Function
Private Sub OutputDataReceived(sender As Object, e As System.Diagnostics.DataReceivedEventArgs)
Dim ProcessId As Integer = GetId(CType(sender, Diagnostics.Process))
Dim Task As VideoParam = _Tasks.Item(ProcessId)
Dim EventArgs As New CustomDataReceivedEventArgs(ProcessId, Task, e)
RaiseEvent ProcessOutPutDataReceived(sender, EventArgs)
End Sub
Private Sub ErrorDataReceived(sender As Object, e As System.Diagnostics.DataReceivedEventArgs)
Dim ProcessId As Integer = GetId(CType(sender, Diagnostics.Process))
Dim Task As VideoParam = _Tasks.Item(ProcessId)
Dim EventArgs As New CustomDataReceivedEventArgs(ProcessId, Task, e)
RaiseEvent ProcessErrorDataReceived(sender, EventArgs)
End Sub
Private Sub Exited(sender As Object, e As System.EventArgs)
Dim Process As Process = CType(sender, Diagnostics.Process)
Dim ProcessId As Integer = GetId(Process)
If Me._listOfSyncProcess.ContainsKey(ProcessId) Then
' Process synchrone terminé
Me._listOfSyncProcess.Remove(ProcessId)
Me.StartAll()
ElseIf Me._ListOfAsyncProcess.ContainsKey(ProcessId) Then
' Process asynchrone terminé
Me._ListOfAsyncProcess.Remove(ProcessId)
End If
Dim Task As VideoParam = _Tasks.Item(ProcessId)
Dim EventArgs As New CustomExitedEventArgs(ProcessId, Task, e)
RaiseEvent ProcessExited(sender, EventArgs)
Me._Tasks.Remove(ProcessId)
Process.Close()
End Sub
End Class |
Partager