Start-Job pause/stop with button click

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
espiman
Posts: 29
Joined: Wed Aug 31, 2011 11:24 pm

Start-Job pause/stop with button click

Post by espiman » Wed Dec 05, 2012 11:15 am

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)

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Start-Job pause/stop with button click

Post by davidc » Wed Dec 05, 2012 11:53 am

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
David
SAPIEN Technologies, Inc.

User avatar
espiman
Posts: 29
Joined: Wed Aug 31, 2011 11:24 pm

Start-Job pause/stop with button click

Post by espiman » Wed Dec 05, 2012 9:26 pm

It now works.

Thanks David

User avatar
espiman
Posts: 29
Joined: Wed Aug 31, 2011 11:24 pm

Start-Job pause/stop with button click

Post by espiman » Thu Dec 06, 2012 9:34 pm

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

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Start-Job pause/stop with button click

Post by davidc » Fri Dec 07, 2012 4:35 am

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
David
SAPIEN Technologies, Inc.

User avatar
espiman
Posts: 29
Joined: Wed Aug 31, 2011 11:24 pm

Start-Job pause/stop with button click

Post by espiman » Fri Dec 07, 2012 9:43 am

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

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Start-Job pause/stop with button click

Post by davidc » Fri Dec 07, 2012 10:36 am

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
David
SAPIEN Technologies, Inc.