none
Command execution in VB RRS feed

  • Question

  • Hi all,

    Executing some commands / performing tasks with Shell like this:

    Shell("Task 1 .bat file1", AppWinStyle.MaximizedFocus, False, 0)

    Shell("Task 2 .PS1 file", AppWinStyle.MaximizedFocus, False, 0)

    Shell("Task N copying files", AppWinStyle.MaximizedFocus, False, 0)

    Question: In current situation, these command run simultaneously regardless if the previous task is completed.

    Is it possible to execute above command Asynchronously so task 2 initiated only when task 1 is completed?

    Wednesday, March 11, 2020 1:49 PM

Answers

  • You can use Process.WaitForExit

    For example, Notepad then Calc =>

    Dim process1 As Process = New Process()
    Dim psi1 As New ProcessStartInfo("notepad.exe")
    process1.StartInfo = psi1
    process1.Start()
    process1.WaitForExit()
    process1.Close()
    
    Dim psi2 As New ProcessStartInfo("calc.exe")
    Process.Start(psi2)

    • Proposed as answer by leshay Wednesday, March 11, 2020 3:56 PM
    • Marked as answer by -OSD- Friday, March 13, 2020 2:47 PM
    Wednesday, March 11, 2020 3:56 PM

All replies

  • Hi

    Here is one way (possibly). This lets you set a time in milliseconds between each command - here I have used 2000 (2 seconds) I have just used notepad for this example.

    Maybe not what you want, but a very effective way to space out the commands.

        Shell("notepad", AppWinStyle.MinimizedFocus, False, 0)
        Threading.Thread.Sleep(2000)
    
        Shell("notepad", AppWinStyle.NormalFocus, False, 0)
        Threading.Thread.Sleep(2000)
    
        Shell("notepad", AppWinStyle.MaximizedFocus, False, 0)
    


    Regards Les, Livingston, Scotland

    Wednesday, March 11, 2020 2:27 PM
  • yeah, that is one way when you are certain or have clear estimate, but not always.
    Wednesday, March 11, 2020 2:35 PM
  • Hi

    OK, how about this ............

        Dim id As Integer = Shell("notepad", AppWinStyle.MinimizedFocus, False, 0)
        Do
          Try
            Process.GetProcessById(id)
          Catch ex As Exception
            Exit Do
          End Try
        Loop
    
        id = Shell("notepad", AppWinStyle.NormalFocus, False, 0)
        Do
          Try
            Process.GetProcessById(id)
          Catch ex As Exception
            Exit Do
          End Try
        Loop
    
        Shell("notepad", AppWinStyle.MaximizedFocus, False, 0)


    Regards Les, Livingston, Scotland

    Wednesday, March 11, 2020 3:29 PM
  • Thanks, I will check this out (by tomorrow) and will report the results.
    • Edited by -OSD- Wednesday, March 11, 2020 3:32 PM
    Wednesday, March 11, 2020 3:32 PM
  • You can use Process.WaitForExit

    For example, Notepad then Calc =>

    Dim process1 As Process = New Process()
    Dim psi1 As New ProcessStartInfo("notepad.exe")
    process1.StartInfo = psi1
    process1.Start()
    process1.WaitForExit()
    process1.Close()
    
    Dim psi2 As New ProcessStartInfo("calc.exe")
    Process.Start(psi2)

    • Proposed as answer by leshay Wednesday, March 11, 2020 3:56 PM
    • Marked as answer by -OSD- Friday, March 13, 2020 2:47 PM
    Wednesday, March 11, 2020 3:56 PM
  • Hi,

    Have you tried the methods above now?

    I think the above replies given by Castorix31 and Leshay can provide you with different solutions, have you tried them?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 12, 2020 1:53 AM
    Moderator
  • You can use Process.WaitForExit

    For example, Notepad then Calc =>

    Dim process1 As Process = New Process()
    Dim psi1 As New ProcessStartInfo("notepad.exe")
    process1.StartInfo = psi1
    process1.Start()
    process1.WaitForExit()
    process1.Close()
    
    Dim psi2 As New ProcessStartInfo("calc.exe")
    Process.Start(psi2)

    Hi and thanks for reply. I have checked it and found it working, thanks again.
    Friday, March 13, 2020 2:47 PM