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.
This topic is 4 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.
Locked
klarlichter
Posts: 40
Last visit: Sun Jan 16, 2022 3:44 am

Progressbar while Job

Post by klarlichter »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Progressbar while Job

Post by jvierra »

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
Last visit: Sun Jan 16, 2022 3:44 am

Re: Progressbar while Job

Post by klarlichter »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Progressbar while Job

Post by jvierra »

You cannot display a ProgressBar from a job.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Progressbar while Job

Post by jvierra »

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
Last visit: Sun Jan 16, 2022 3:44 am

Re: Progressbar while Job

Post by klarlichter »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Progressbar while Job

Post by jvierra »

Why did you change it? Why use two writes? What you are doing doesn't make any sense.
klarlichter
Posts: 40
Last visit: Sun Jan 16, 2022 3:44 am

Re: Progressbar while Job

Post by klarlichter »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Progressbar while Job

Post by jvierra »

You have to set that after the loop completes.
klarlichter
Posts: 40
Last visit: Sun Jan 16, 2022 3:44 am

Re: Progressbar while Job

Post by klarlichter »

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"?
This topic is 4 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.
Locked