VB.NET Execute An Array of Array processes

deskcheck1-0579 411 Reputation points
2021-11-19T16:05:03.137+00:00

I've created an array of arrays and corresponding .bat files so I could execute them from VB.Net.

The main array consists of models as follows:

Priority1 models executables:
SWAT_0410003.exe
SWAT_0410004.exe
SWAT_0410008.exe
Priority2 models executables:
SWAT_0410005.exe
SWAT_0410006.exe
SWAT_0410007.exe
Priority3 models executables:
SWAT_04100009.exe

I want to call a process to execute the above models but I need for Priority1
to execute first.

Only when all executables in priority1 finished executing should priority2
execute and so on; Priority 2 needs input from Priority1, and priority3 needs input from Priority2...and so on.

There could be more than 3 priority levels.

How do I create and call processes so that they execute one after the other?

Appreciate any response.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,178 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
11,728 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,101 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 44,706 Reputation points
    2021-11-19T17:14:33.573+00:00

    Creating and calling processes sequentially is pretty straightforward. You just need to wait for the process to complete before continuing on.

    Note: executing a process can be tricky especially if the process generates lots of output as you have to process the output buffer otherwise it'll deadlock. I'm ignoring all that here but to read about this issue go to the docs.

       ' Warning: My VB is rusty at best  
       Sub RunPrograms ( programs As String())   
          For Each program As String in programs  
             ' Use the appropriate Process.Start method for your needs, using the simplest one here  
             Dim process as Process = Process.Start(program)  
         
             ' Wait for it to exit, assuming it will not prompt for input or display so much data that the output buffer fills up  
             process.WaitForExit()  
         
             ' Optionally check the exit code  
            If process.ExitCode <> 0 Then  
                ' Something went wrong  
            End If  
          Next  
       End Sub  
    

    If you need a Task-based approach then use WaitForExitAsync instead.

    Now that you can execute a series of programs in sequential order you just need to add the priority to the list. For this it isn't clear how you modelled that so I'm going to envision that you have a simple type that assigns a priority to a program name.

       Public Class PriorityProgram  
       {  
           Public Property Priority As Integer  
           Public Property Program As String  
       }  
    

    Now you can use LINQ to order your programs by priority first or group them.

       Sub ExecutePrograms ( programs As List(Of PriorityProgram) )  
          ' Group by priority and then it doesn't matter  
          Dim orderedPrograms = From program In programs  
                                                  Group program By program.Priority into gr = Group  
                                                  Select Priority, Programs = gr  
         
          For Each item In orderedPrograms  
              RunPrograms(item.Programs)  
          Next  
       End Sub  
    

0 additional answers

Sort by: Most helpful