Page 1 of 1

Start-Job pause/stop with button click

Posted: Wed Dec 05, 2012 10:15 am
by espiman
Hi.

I´m testing Job Tracker Control Set and Button Start-Job with Progressbar.

I need to stop the for loop when i click one Stop Button.

I have declared a global variable $global:StopJob to false in onload form and set to true when click the Stop Button, but when job is working, dont get the new value.

I attach pff proyect.

Thanks.

Attached files /FileUpload/3b/14f69f3dd2a198ea536054ccfce00b.rar (3.9 KB)

Start-Job pause/stop with button click

Posted: Wed Dec 05, 2012 10:53 am
by davidc
The $Argument1 will only contains a copy of the value of the Stopjob variable and you can't access the original $Stopjob variable from a job.

Instead you can just call the Stop-JobTracker function and it will stop the all the running jobs for you.

David

Start-Job pause/stop with button click

Posted: Wed Dec 05, 2012 8:26 pm
by espiman
It now works.

Thanks David

Start-Job pause/stop with button click

Posted: Thu Dec 06, 2012 8:34 pm
by espiman
Hi.

Question:

if I want to process all the rows in datagridview in the job, I can´t send de object to job, right?

I will have to send an array with content of teh datagrid?

Thanks

Start-Job pause/stop with button click

Posted: Fri Dec 07, 2012 3:35 am
by davidc
You should never send a UI Control to a job. It will result in errors because they should never be access outside the main thread (process).

It really depends on how you load the grid. It can be fairly easy if you are using a Datasource. With a Datasource you only have to pass the DataSource property value to job.

If you are manually building a grid, then it becomes much more complicated. In that case you might want to build a PSObject that represent a row, where the column names are note property names and the cells are the values.

Here is a quick example on how to get the value of the first cell in the first row:

$datagridview1.Rows[0].Cells[0].Value

David

Start-Job pause/stop with button click

Posted: Fri Dec 07, 2012 8:43 am
by espiman
Thanks david

I load the grid manually from excel file, and can change the cells values.

I try to pass a array to job, but i have problems. I have a datagridview with variables cols and rows. I need to create and array multidimensional with header cols and values of the rows.

$Grid = New-Object 'object[,]' $Rows,$Cols
for ($i=0; $i -le $dgDestinatarios.RowCount - 1; $i++)
{
for ($x=0; $x -le $dgDestinatarios.ColumnCount - 1; $x++)
{
$Grid[$i,$x] = $dgDestinatarios.Rows[$i].Cells[$x].Value
}
}

I need and array $Grid like this:

cod1 mail1 name1 ....
cod2 mail2 name2 ....
cod3 mail3 name3 ....
....

and pass the job:
Add-JobTracker -Name "JobName" -ArgumentList (,$Grid)

and in the jobscript:

-JobScript {
#--------------------------------------------------
#TODO: Set a script block
#Important: Do not access form controls from this script block.

Param($DataGrid)#Pass any arguments using the ArgumentList parameter

foreach ($i in $DataGrid)
{
Write-Host $i[0] $i[1] $i[2] ...
}

#--------------------------------------------------
}`

Another way to pass datagridview to job?

Thanks in advance

Start-Job pause/stop with button click

Posted: Fri Dec 07, 2012 9:36 am
by davidc
Your way should work. An alternative is to load the excel data into a DataTable. Then can load the DataGridView using its DataSource property and pass it to the job. The advantage of using a DataTable is that it allows you to make the DataGridView sortable by column.

Look at the Search Grid Template for an example on how to sort. I also recommend looking at the Convert-ToDataTable function to learn how to create a DataTable. The function is also in a snippet with the same name.

David