Progressbar while Job

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
klarlichter
Posts: 40
Joined: Tue Nov 19, 2019 1:17 am

Progressbar while Job

Post by klarlichter » Wed Nov 27, 2019 6:04 pm

Hello,

i try to display a progressbar while a job working.

I use the following code:

Code: Select all

$job = Start-Job -Script (Stop-Service OpenVPNService)
while ((Get-Job $job).Status -eq 'Running'){
		$progressbar1.Value += 1
		sleep -mil 500
}
$progressbar1.Value = $progressbar1.MaxValue
But it seems not working. Did anyone have some idea how to solve it and display progressbar since the job are done?

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar while Job

Post by jvierra » Wed Nov 27, 2019 6:26 pm

You can't do that. It is not supported. A job runs in a different process and has no GUI. The best you can do is output a message and retrive it.

klarlichter
Posts: 40
Joined: Tue Nov 19, 2019 1:17 am

Re: Progressbar while Job

Post by klarlichter » Wed Nov 27, 2019 6:33 pm

Hello,
so it is not possible to display a progressbar while the service is restart?
I mean display while wait that service is stop or start display any progressbar?

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar while Job

Post by jvierra » Wed Nov 27, 2019 6:41 pm

You cannot display a ProgressBar from a job.

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar while Job

Post by jvierra » Wed Nov 27, 2019 7:59 pm

This will show a progressbar will waiting for the service to stop.

Code: Select all

function Stop-ServicePB{
    param($ServiceName)

    Write-Progress -Activity StopService -Status 'Progress->' -PercentComplete 0 -CurrentOperation Stopping
    $service = Get-Service $ServiceName
    $service | Stop-Service
    do {
    $service.Refresh()
        Write-Progress -Id 1 -Activity Stopping -Status 'Progress' -PercentComplete 10 -CurrentOperation Stopping
    } until($service.Status -eq 'Stopped')
}
Stop-ServicePB OpenVPNService

klarlichter
Posts: 40
Joined: Tue Nov 19, 2019 1:17 am

Re: Progressbar while Job

Post by klarlichter » Thu Nov 28, 2019 11:40 am

Hello,
i try your code it is working 95% good.
If i press stop it load till 10% and then your window frozen....

I change it to

Code: Select all

function serviceStop
{
	param ($serviceName)
	
	Write-Progress -Activity StopService -Status 'Progress->' -PercentComplete 0 -CurrentOperation Stopping
	$service = Get-Service $ServiceName
	$service | Stop-Service
	do
	{
		$service.Refresh()
		Write-Progress -Id 1 -Activity Stopping -Status 'Progress' -PercentComplete 50 -CurrentOperation Stopping
		Write-Progress -Id 2 -Activity Stopping -Status 'Stopped' -PercentComplete 100 -CurrentOperation Stopping
	}
	until ($service.Status -eq 'Stopped')
	
}
Now it load till end. :)

But first time when i press stop it display normal after i restart service and stop again inside that write progress from it display "-1 remaining"
Why it only display after first call?

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar while Job

Post by jvierra » Thu Nov 28, 2019 11:43 am

Why did you change it? Why use two writes? What you are doing doesn't make any sense.

klarlichter
Posts: 40
Joined: Tue Nov 19, 2019 1:17 am

Re: Progressbar while Job

Post by klarlichter » Thu Nov 28, 2019 11:53 am

Because he was only work till 10%.

I now use your example again and replace 10 with 100. It work till end.
But after he reach "100%" he should change text from "Progress" to "Stopped" is that possible?
And why he show after second time call that stop action "-1 remaining"?

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Progressbar while Job

Post by jvierra » Thu Nov 28, 2019 11:56 am

You have to set that after the loop completes.

klarlichter
Posts: 40
Joined: Tue Nov 19, 2019 1:17 am

Re: Progressbar while Job

Post by klarlichter » Thu Nov 28, 2019 12:00 pm

Please take a look.
Shuch it should be:

Code: Select all

function serviceStop
{
	param ($serviceName)
	
	Write-Progress -Activity StopService -Status 'Progress->' -PercentComplete 10 -CurrentOperation Stopping
	$service = Get-Service $ServiceName
	$service | Stop-Service
	do
	{
		$service.Refresh()
		Write-Progress -Id 1 -Activity Stopping -Status 'Progress' -PercentComplete 50 -CurrentOperation Stopping
	}
	until ($service.Status -eq 'Stopped')
	Write-Progress -Activity Stopped -Status 'Stopped' -PercentComplete 100 -CurrentOperation Stopped
}
But if yes why only after you call that stop function 2 times it display "-1s reamining"?

Post Reply