0

I have 130 batch files stored in one central location, I want to create VB.NET program to execute those batch files in parallel NOT in sequence. and then return the output of these 130 batch files to Textbox control.

How can I cater the above requirements? or what's the recommended way of doing it, keeping in mind the program will run on a high Specs Server so performance is not an issue here.

What I have done is I placed this for loop inside Button_Click Event to get my .bat names & then to get them executed in parallel:

    Dim Directory As New IO.DirectoryInfo("C:\Batch\")
    Dim fileName As IO.FileInfo() = Directory.GetFiles("*.bat")
    For Each file As IO.FileInfo In fileName
        RichTextBox1.AppendText(file.ToString() & vbNewLine)
    Next

But now here after i got the names of .dat, how to get them executed in parallel?

8
  • It makes no sense to parallelize, because all rests in the speed of reading from the hard/solid drive. Commented Nov 28, 2015 at 13:31
  • @AlexanderPetrov, I think that would depend on the batch files. If they also fetch some data or something else that they have to wait for then parallel execution does make sense. Commented Nov 28, 2015 at 13:56
  • @RonDeijkers - Agree. Waiting for clarification from the author. Commented Nov 28, 2015 at 14:00
  • @RonDeijkers you're right, the batch files will fetch some data from certain location, for that i need to execute it in parallel not to wait for the first job & move on to the next one. Commented Nov 28, 2015 at 14:30
  • @AlexanderPetrov The batch files will fetch some data from certain location, for that i need to execute it in parallel not to wait for the first job & move on to the next one Commented Nov 28, 2015 at 14:32

2 Answers 2

0

This is as close as I could get, combining the comments and the updated description

Dim Directory As New IO.DirectoryInfo("C:\Batch\")
Dim fileName As IO.FileInfo() = Directory.GetFiles("*.bat")
Dim list As New List(Of String)
For Each file As IO.FileInfo In fileName
    list.Add(file.ToString())
Next

Dim processTasks As New List(Of Task(Of String))()

    For Each bat As String In list
        Dim processTask As Task(Of String) = Task.Factory.StartNew((Function()
                                             Dim filePath As String = IO.Path.Combine(myDirectory.FullName, bat) 
                                             Dim info As New System.Diagnostics.ProcessStartInfo()
                                             info.FileName = filePath
                                             info.CreateNoWindow = True
                                             info.WindowStyle = ProcessWindowStyle.Hidden
                                             System.Diagnostics.Process.Start(info)
                                             Return "hello"
                                             End Function))

        processTasks.Add(processTask)
    Next

' if you are using a synchronous method
' Dim results() As String = Task.WhenAll(processTasks).GetAwaiter().GetResult()
' if you are using an async method
Dim results() As String = await Task.WhenAll(processTasks)

' Either use the string array as a whole, or
For Each result As String In results
    ' Do something with every individual result
Next
Sign up to request clarification or add additional context in comments.

20 Comments

I have run the above code, but it end up in the following error: An exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll but was not handled in user code Additional information: The system cannot find the file specified
@Omran check again, had forgotten to add the path to the folder
@cFtozenDeath - now am getting this error An exception of type 'System.ArgumentException' occurred in mscorlib.dll but was not handled in user code Additional information: Illegal characters in path.
@Omran are you sure you are using the exact same code? Check that "list" does not contain any illegal character, like / ^ & etc
now it works fine, but here i want to keep all cmd windows hidden & result should be sent to textbox, can it be done?
|
0

Note I've answered the question using C# because OP tagged the question with .

You should simply use Task:

List<Task<string>> processTasks = new List<Task<string>>();

for(int i = 0; i < 130; i ++)
{
        Task<string> processTask = Task<string>
        (
            () => 
            {
                 // System.Diagnostics.Process stuff goes here, you should return a string
                 // since it represents the output of the whole process
            }
        );

        processTasks.Add(processTask);
        Task.Run(processTask);
}

// Asynchronously wait for all tasks to finish
await Task.WhenAll(processTasks);

// Now after all tasks have finished, each stored task in "processTasks" 
// will contain a task where their "Result" property is the string representing
// the whole process output

9 Comments

Thanks for your swift reply, however, here I have to place each batch file name inside for loop or no easy way to pick them up from central location "batch file named as numbers"? and after each task finished, how will i get my result outputted to Textbox for each executed Batch file.??
(1) Replace the for loop with whatever you need. For example a foreach of a list of file names. (2) I told you it in the whole answer!! Once you've awaited all processTasks you just need to get the results in each Task<string> instance using Task<string>.Result.
@Omran if they are literally files, why not just use the Directory class to get all file names?
@cFrozenDeath, exactly i have executed the following to get all my .bat files, but am confuse where to place in the above shared code by Matias Dim Directory As New IO.DirectoryInfo("c:\") Dim fileName As IO.FileInfo() = Directory.GetFiles("*.bat") For Each file As IO.FileInfo In fileName RichTextBox1.AppendText(file.ToString() & vbNewLine) Next
@Omran if you don't mind editing your question and adding the code you have right now, I'll give you an answer (kinda rusty in VB.NET but will try)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.