Reputation: 689
I'm trying to synchronize different tasks at the same the time in PowerShell because they each take around 2-4 hours. The Script i have right now just processes each task one after another. I know there is an option called background jobs in PowerShell. Is there a way to transform the following code so it processes multiple .cmd files at the same time?
$handler_button1_Click=
{
$answer= $finalcheck.popup("text",0,"text",4)
If($answer-eq 6) {
[Windows.Forms.MessageBox]::Show("text", "Information", [Windows.Forms.MessageBoxButtons]::OK, [Windows.Forms.MessageBoxIcon]::Information)
$listBox1.Items.Clear();
if ($checkBox1.Checked) {
Try{
& K:\sample.cmd
}catch [System.Exception]{
$listBox1.Items.Add("Error")}
}
if ($checkBox2.Checked) {
Try{
& K:\sample2.cmd
}catch [System.Exception]{
$listBox1.Items.Add("Error")}
}
Upvotes: 0
Views: 288
Reputation: 4240
You can use Start-Job:
$job = Start-Job { & K:\sample.cmd }
At this point you are likely to need to add some code to watch the state of jobs you've started. Potentially you can periodically inspect $job.State.
Extending on this, you might refuse to start a job of the same type if one is already running.
In addition to Start-Job you should take a look at:
Upvotes: 1