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.
This topic is 11 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
espiman
Posts: 29
Last visit: Thu Jul 30, 2015 11:36 am

Start-Job pause/stop with button click

Post 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)
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Start-Job pause/stop with button click

Post 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
David
SAPIEN Technologies, Inc.
User avatar
espiman
Posts: 29
Last visit: Thu Jul 30, 2015 11:36 am

Start-Job pause/stop with button click

Post by espiman »

It now works.

Thanks David
User avatar
espiman
Posts: 29
Last visit: Thu Jul 30, 2015 11:36 am

Start-Job pause/stop with button click

Post 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
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Start-Job pause/stop with button click

Post 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
David
SAPIEN Technologies, Inc.
User avatar
espiman
Posts: 29
Last visit: Thu Jul 30, 2015 11:36 am

Start-Job pause/stop with button click

Post 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
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Start-Job pause/stop with button click

Post 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
David
SAPIEN Technologies, Inc.
This topic is 11 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.