Thursday, June 11, 2020

[VB][Example] WFA call console application with arguments and get the exitCode

This is an example function from a window form application with visual basic,
Private Sub CallProcess()
  Const WorkingDir As String = "C:\"
  Const ExeFilename As String = "test.exe"
  Const StrSrcDir As String = "C:\photo"
  Const StrDstDir As String = "C:\photo_new"
  Const IntResolution AS Integer = 256
  Const ProcessTimeout AS Integer = 10
  Try
    Dim objProcess As New Process
    objProcess.StartInfo.WorkingDirectory = WorkingDir
    objProcess.StartInfo.FileName = ExeFilename
    objProcess.StartInfo.Arguments = String.Fomat(" ""{0}"" ""{1}"" {2}",StrSrcDir,StrDstDir,IntResolution)
    objProcess.StartInfo.UseShellExecute = True
    objProcess = Process.Start(objProcess.StartInfo)
    Me.Refresh()
    If objProcess.WaitForExit(ProcessTimeout * 60000) = False Then
      objProcess.Kill()
    End If
    Dim intProcessExitCode AS Integer = objProcess.ExitCode()
    MessageBox.Show(_getErrorMsg(intProcessExitCode))
  Catch e As Exception
    MessageBox.Show(e.ToString())
  End Try
End Sub

Private Function _getErrorMsgStr(ByVal exitCode AS Integer) As String
  Dim message As String
  Select Case exitCode
    Case 0
      message = "Success"
    Case -2
      message = "File is Existed in "+My.Settings
    Case -3
      message = "Invalid file format"
    Case -4
      message = "Fail - error occurred while trying to save the image"
    Case Else
      message = "Application ""linGenThumbnail"" occur execetion."
  End Select
  If exitCode < -1 Then
    Return message + ", this process suspend."
  Else
    Return message
  End If
End Function

Reference:
How do I specify the exit code of a console application in .NET?
How do I return a value from a console application to a service in .NET?

No comments :

Post a Comment