I'm having issues with figuring out the Task class, I'm trying to have a process from the Process class run x amount of times. I attempted to accomplish this using the Background worker but when I run the process, it waits for the first one to complete before beginning the second one.
Here's what I'm trying to do now:
ps = new Push(this)
for (int counter = 1; counter <= maxgroup; counter++){
t = Task.Run(() => { ps.runCommand(strBatchPath, counter, username, password, password.getString()); });
}
For my "Push" class here's the method I'm calling,
public void runCommand(string batchfile, int groupnumber, string username, SecureString securePassword, string password)
{
formControl.setTextbox(groupnumber.ToString());
string number = groupnumber.ToString();
string tmp = "/c c:\\psexec.exe -c @c:\\Computers\\group" + number + ".txt -u MYDOMAINNAME\\" + username + " -p " + password + " -h " + @batchfile;
Process process = new Process();
process.StartInfo.WorkingDirectory = @"C:\";
process.StartInfo.FileName = "cmd";
formControl.setTextbox(tmp);
process.StartInfo.Arguments = tmp;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.CreateNoWindow = true;
//* Read the output (or the error)
// string output = process.StandardOutput.ReadToEnd();
process.OutputDataReceived += process_DataReceived;
process.ErrorDataReceived += process_ErrorReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
My issue is when I'm looping (from 1 to 2 lets say) My first iteration Counter = 1 when I pass counter to
ps.runCommand();
counter starts at 3 so in the runCommand function, it's calling group3.txt twice, instead of group1.txt, group2.txt
I changed it and am now using async/await
ps = new Push(this);
for (int counter = 1; counter <= maxgroup; counter++)
{
await Task.Run(() => ps.runCommand(strBatchPath, counter, username, password, password.getString()) );
}
This is working actually.. kind of, It's just going one at a time. So after the first task finishes, it starts the second one. How can I make it so that it runs BOTH tasks at the same time? Thank you for any assistance!!