Exécution d'un programme et attendre la fin de l'exécution de façon non bloquante

Contenu du snippet

Permet d'exécuter un fichier et d'attendre la fin de l'exécution sans figer le programme en cours

Source / Exemple :


#Region " Constantes "
    Public Const SW_HIDE As Short = 0
    Public Const SW_SHOWNORMAL As Short = 1 ' Restores Window if Minimized or
    'Minimized
    Public Const SW_SHOWMINIMIZE As Short = 2
    ' Maximized
    Public Const SW_SHOWMAXIMIZE As Short = 3
    'NoActivate
    Public Const SW_SHOWNOACTIVE As Short = 4
#End Region

#Region " API "
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
        (ByVal hwnd As Integer, ByVal lpOperation As String, ByVal lpFile As String, _
        ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Integer) _
        As Integer

    Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
        (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Integer
#End Region

    Public Const csExe$ = ".exe"
    Public Sub sbShell(ByVal S As String, Optional ByVal Args As String = "", _
            Optional ByVal ProcStyle As Short = SW_SHOWNORMAL)
        Try
            Dim MyProcess As New ProcessStartInfo(S, Args)
            Select Case ProcStyle
                Case SW_SHOWNORMAL
                    MyProcess.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
                Case SW_HIDE
                    MyProcess.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
                Case SW_SHOWMAXIMIZE
                    MyProcess.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized
                Case SW_SHOWMINIMIZE
                    MyProcess.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized
            End Select
            MyProcess.UseShellExecute = True
            Dim P As Process = Process.Start(MyProcess)
            Do
                Application.DoEvents()
                System.Threading.Thread.CurrentThread.Sleep(1)
            Loop Until P.HasExited
        Catch ex As Exception

        End Try
    End Sub

    Public Sub sbLanceFichier(ByVal hWnd As Integer, ByVal Fichier As String, _
            Optional ByVal bMax As Boolean = False, Optional ByVal typAff As Short = SW_SHOWNORMAL, _
            Optional ByVal bWait As Boolean = False)
        Try
            Dim RetVal As Integer
            Dim Ext$ = System.IO.Path.GetExtension(Fichier$)
            If Ext <> csExe And Ext <> "" Then
                Dim Rep As String, SH As Short = SW_SHOWNORMAL
                If bMax Then
                    SH = SW_SHOWMAXIMIZE
                ElseIf typAff <> SW_SHOWNORMAL Then
                    SH = typAff
                End If
                Dim Exec As String = New String(Chr(0), 255)
                RetVal = FindExecutable(Fichier, Rep, Exec)
                If RetVal > 32 Or Trim$(Exec) <> "" Then
                    If Rep Is Nothing Then
                        Rep = Left(Fichier, 3)
                    End If
                    Exec = Left(Exec, InStr(Exec, Chr(0)) - 1)
                    If Not bWait Then
                        ShellExecute(hWnd, "open", Exec, Fichier, _
                            Rep, SH)
                    Else
                        modProcess.sbShell(Exec, Fichier, SH)
                    End If
                End If
            ElseIf Ext <> "" Then
                sbShell(Fichier, , typAff)
            Else
                sbShell("explorer.exe", "/e,""" & Fichier & """", typAff)
            End If
        Catch ex As Exception
            gMsg.sbMsgCr(ex.Message, "modShell.sbLanceFichier")
        End Try
    End Sub

Conclusion :


La procedure sbShell permet de rendre la main aux événements de l'application sans altérer le déroulement du programme en attendant la fin de l'exécution

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.