Trouble understanding Button- Start 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 7 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
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Trouble understanding Button- Start Job

Post by swindmiller »

I am trying to understand the start job and how to update some textboxes and was hoping someone could help me out. I read through the tutorial understand the components but am having trouble writing it out.

I want to start a background job when I certain flag is set to "1" then stop or complete once it is "0". I also want to update some textboxes while the job is running.
I have this While statement that works but obviously freezes the the gui while running hence needing the job:
  1. While ($itunes.PlayerState -eq "1")
  2.         {
  3.             $current = $itunes.CurrentTrack | Select Name, Artist, Album
  4.             $textboxCurrentArtist.Text = $current.Artist
  5.             $textboxCurrentTrack.Text = $current.Name
  6.             $textboxCurrentAlbum.Text = $current.Album
  7.             Start-sleep -Seconds 5
  8.         }
I would need the job to continue to loop while $itunes.PlayerState -eq "1" and update the 3 Textboxes every 5 seconds or so while the job is running. Once $itunes.PlayerState -eq "0" the job would complete.

Thanks for any help you can give,
Scott
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Trouble understanding Button- Start Job

Post by swindmiller »

OK, I think I am starting to get somewhere. I added what I did below but what happens is every 5 seconds it updates the textboxes but just flashes the correct text for not even a second then blanks the boxes, this obviously repeats every 5 seconds. Feels like I am making a stupid mistake.

This is what I added:
  1. $buttonStartJob_Click={
  2.    
  3.     $buttonStartJob.Enabled = $false   
  4.    
  5.     #Create a New Job using the Job Tracker
  6.     Add-JobTracker -Name 'JobName' `
  7.                    -JobScript {
  8.         #--------------------------------------------------
  9.         #TODO: Set a script block
  10.         #Important: Do not access form controls from this script block.
  11.        
  12.         Param ($Argument1) #Pass any arguments using the ArgumentList parameter
  13.         $itunes = New-Object -ComObject iTunes.Application
  14.         While ($itunes.PlayerState -eq "1")
  15.         {
  16.             $itunes.CurrentTrack | Select Name, Artist, Album
  17.             Start-sleep -Seconds 5
  18.         }
  19.         #--------------------------------------------------
  20.     }`
  21.                    -CompletedScript {
  22.         Param ($Job)
  23.         #$results = Receive-Job -Job $Job
  24.         #Enable the Button
  25.         $buttonStartJob.ImageIndex = -1
  26.         $buttonStartJob.Enabled = $true
  27.     }`
  28.     -UpdateScript {
  29.         Param ($Job)
  30.         $results = Receive-Job -Job $Job | Select-Object -Last 1
  31.        
  32.         $textboxCurrentArtist.Text = $results.Artist
  33.         $textboxCurrentTrack.Text = $results.Name
  34.         $textboxCurrentAlbum.Text = $results.Album
  35.     }`
  36.     -ArgumentList $null
  37. }
  38.  
  39. $jobTracker_FormClosed=[System.Windows.Forms.FormClosedEventHandler]{
  40. #Event Argument: $_ = [System.Windows.Forms.FormClosedEventArgs]
  41.     #Stop any pending jobs
  42.     Stop-JobTracker
  43. }
  44.  
  45. $timerJobTracker_Tick={
  46.     Update-JobTracker
  47. }
EDIT:
So I updated my UpdateScript to this:
  1. -UpdateScript {
  2.         Param ($Job)
  3.         $results = Receive-Job -Job $Job | Select-Object -Last 1
  4.         If (!($results.Name -eq $test))
  5.         {
  6.             $textboxCurrentArtist.Text = $results.Artist
  7.             $textboxCurrentTrack.Text = $results.Name
  8.             $textboxCurrentAlbum.Text = $results.Album
  9.             $test = $results.Name
  10.         }
  11.     }
and that seems to work adding the IF statement to ignore the Update if the data is the same :D I guess my question now is, is this the correct way to do this? Is there any downfall to having this loop every 5 seconds, will it cause any computer resource problems (memory/cpu usage) if it is looping for long periods of time? This will be running whenever we are listening to music.

BTW....love the Button – Start Job control set.....so may uses
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Trouble understanding Button- Start Job

Post by DevinL »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trouble understanding Button- Start Job

Post by jvierra »

  1. If ($results.Name -ne $textboxCurrentTrack.Text )){
  2.     $textboxCurrentArtist.Text = $results.Artist
  3.     $textboxCurrentTrack.Text = $results.Name
  4.     $textboxCurrentAlbum.Text = $results.Album
  5. }
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Trouble understanding Button- Start Job

Post by swindmiller »

Yeah that simplifies it...lol...thanks!

If I have it loop every second (5 seconds currently) will I run into problems doing it that way?

I just feel like this is a bad way to do things but don't know any better, maybe this is just how it's done....still learning :)

Thanks,
Scott
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Trouble understanding Button- Start Job

Post by jvierra »

Set the time to one second.
On modern systems this is not an issue.
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Trouble understanding Button- Start Job

Post by swindmiller »

Thanks...I appreciate all the help!!
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Trouble understanding Button- Start Job

Post by dan.potter »

sorry, I didn't see this until now. Does $itunes actually carry over to the job? In my standard shell script I define the com object inside the jobscript. I haven't tested the job tracker yet as I haven't wrapped my head around what to do with play/pause and skip.
User avatar
swindmiller
Posts: 64
Last visit: Tue Aug 22, 2023 11:59 am

Re: Trouble understanding Button- Start Job

Post by swindmiller »

I verified that $itunes does not carry over, I had to define it in the background job...no biggie.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Trouble understanding Button- Start Job

Post by dan.potter »

ah, I see it now:-) I should have a finished project tomorrow. Working remotely via rdp without sound is not very fun.
This topic is 7 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