| 1 | #Region "Microsoft.VisualBasic::bc18109e0949131a98d1402c90e6a5f0, Microsoft.VisualBasic.Core\CommandLine\CLI\IIORedirectAbstract.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Interface IIORedirectAbstract |
| 35 | ' |
| 36 | ' Properties: Bin, CLIArguments, StandardOutput |
| 37 | ' |
| 38 | ' Function: Run, Start |
| 39 | ' |
| 40 | ' Structure ProcessEx |
| 41 | ' |
| 42 | ' Properties: Bin, CLIArguments, StandardOutput |
| 43 | ' |
| 44 | ' Function: Run, Start, ToString |
| 45 | ' |
| 46 | ' Sub: __wait |
| 47 | ' |
| 48 | ' |
| 49 | ' /********************************************************************************/ |
| 50 | |
| 51 | #End Region |
| 52 | |
| 53 | Imports System.Threading |
| 54 | Imports Microsoft.VisualBasic.Serialization.JSON |
| 55 | |
| 56 | Namespace CommandLine |
| 57 | |
| 58 | Public Interface IIORedirectAbstract |
| 59 | |
| 60 | ReadOnly Property Bin As String |
| 61 | ReadOnly Property CLIArguments As String |
| 62 | |
| 63 | ''' <summary> |
| 64 | ''' The target invoked process event has been exit with a specific return code.(目标派生子进程已经结束了运行并且返回了一个错误值) |
| 65 | ''' </summary> |
| 66 | ''' <param name="exitCode"></param> |
| 67 | ''' <param name="exitTime"></param> |
| 68 | ''' <remarks></remarks> |
| 69 | Event ProcessExit(exitCode As Integer, exitTime As String) |
| 70 | |
| 71 | ''' <summary> |
| 72 | ''' Gets the standard output for the target invoke process. |
| 73 | ''' </summary> |
| 74 | ''' <value></value> |
| 75 | ''' <returns></returns> |
| 76 | ''' <remarks></remarks> |
| 77 | ReadOnly Property StandardOutput As String |
| 78 | |
| 79 | ''' <summary> |
| 80 | ''' Start the target process. If the target invoked process is currently on the running state, |
| 81 | ''' then this function will returns the -100 value as error code and print the warning |
| 82 | ''' information on the system console.(启动目标进程) |
| 83 | ''' </summary> |
| 84 | ''' <param name="WaitForExit">Indicate that the program code wait for the target process exit or not?(参数指示应用程序代码是否等待目标进程的结束)</param> |
| 85 | ''' <returns>当发生错误的时候会返回错误代码,当当前的进程任然处于运行的状态的时候,程序会返回-100错误代码并在终端之上打印出警告信息</returns> |
| 86 | ''' <remarks></remarks> |
| 87 | Function Start(Optional WaitForExit As Boolean = False) As Integer |
| 88 | ''' <summary> |
| 89 | ''' 启动目标子进程,然后等待执行完毕并返回退出代码(请注意,在进程未执行完毕之前,整个线程会阻塞在这里) |
| 90 | ''' </summary> |
| 91 | ''' <returns></returns> |
| 92 | Function Run() As Integer |
| 93 | End Interface |
| 94 | |
| 95 | Public Structure ProcessEx |
| 96 | Implements IIORedirectAbstract |
| 97 | |
| 98 | Public Property Bin As String Implements IIORedirectAbstract.Bin |
| 99 | Public Property CLIArguments As String Implements IIORedirectAbstract.CLIArguments |
| 100 | |
| 101 | Public ReadOnly Property StandardOutput As String Implements IIORedirectAbstract.StandardOutput |
| 102 | Get |
| 103 | Throw New NotSupportedException |
| 104 | End Get |
| 105 | End Property |
| 106 | |
| 107 | Public Event ProcessExit(exitCode As Integer, exitTime As String) Implements IIORedirectAbstract.ProcessExit |
| 108 | |
| 109 | Public Overrides Function ToString() As String |
| 110 | Return Me.GetJson |
| 111 | End Function |
| 112 | |
| 113 | Public Function Run() As Integer Implements IIORedirectAbstract.Run |
| 114 | Return Start(True) |
| 115 | End Function |
| 116 | |
| 117 | Public Function Start(Optional WaitForExit As Boolean = False) As Integer Implements IIORedirectAbstract.Start |
| 118 | Dim proc As New Process |
| 119 | |
| 120 | Try |
| 121 | proc.StartInfo = New ProcessStartInfo(Bin, CLIArguments) |
| 122 | proc.Start() |
| 123 | Catch ex As Exception |
| 124 | ex = New Exception(Me.GetJson, ex) |
| 125 | Throw ex |
| 126 | End Try |
| 127 | |
| 128 | If WaitForExit Then |
| 129 | Call __wait(proc) |
| 130 | Return proc.ExitCode |
| 131 | Else |
| 132 | Dim h As Action(Of Process) = AddressOf __wait |
| 133 | Call New Thread(Sub() Call h(proc)).Start() |
| 134 | Return 0 |
| 135 | End If |
| 136 | End Function |
| 137 | |
| 138 | Private Sub __wait(proc As Process) |
| 139 | Call proc.WaitForExit() |
| 140 | RaiseEvent ProcessExit(proc.ExitCode, Now.ToString) |
| 141 | End Sub |
| 142 | End Structure |
| 143 | End Namespace |