Code runs at least twice

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 4 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
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Code runs at least twice

Post by gutihz »

Hi Everyone,

I have a weird problem. Basically I created a form where it asks for a computer name and then when the user clicks on the OK button, it's supposed to grab the users of the "offer Remote assistance helpers" group of that computer and write it in the text box.

The code grabs the users and writes it in the textbox, but instead of stopping, it re-runs and adds the same users in the text box. So I end up with duplicate users. I tried everything and couldn't come up with a solution.

Any help would be appreciated.
  1. $timer_Tick = {
  2.         [TimeSpan]$span = $script:StartTime - (Get-Date)
  3.         if ($span.TotalSeconds -le 0)
  4.         {
  5.         $timer.Stop()
  6.         }
  7.     $Users = @()
  8.     $OfferGroup = [ADSI]"WinNT://$ComputerName/Offer Remote Assistance Helpers,group"
  9.     $Members = @($OfferGroup.psbase.Invoke("Members"))
  10.     $Members | ForEach-Object {
  11.         $txtboxUsers.AppendText($_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null))
  12.         $txtboxUsers.AppendText("`n")
  13.     }
  14. }
And this is the result from the textbox:

Authenticated Users
Administrators
Users
Authenticated Users
Administrators
Users

Product, version and build: PowerShell Studio 2016, 5.2.124
32 or 64 bit version of product: 64 bit
Operating system:
32 or 64 bit OS: 64 bit
PowerShell Version: 4

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Code runs at least twice

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: Code runs at least twice

Post by jvierra »

Whey would you use a timer to do this? Just do it in the button click event.
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

I have more stuff that is running besides checking for group. I have another form that the user enters all the information and that form calls where all the results are shown. If I don't use the timer, it takes a long time for the result form to pop-up.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Code runs at least twice

Post by dan.potter »

if you really must do it that way( I wouldn't).. check for text in the text box and run your code only if it's empty.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

gutihz wrote:I have more stuff that is running besides checking for group. I have another form that the user enters all the information and that form calls where all the results are shown. If I don't use the timer, it takes a long time for the result form to pop-up.
Why d you think that a timer is the solution. Why not just place the code in the form shown event so the form opens and then is populated.

With what you have posted it is not really possible to understand what you are trying to do or where your issue originates.
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

jvierra wrote:
gutihz wrote:I have more stuff that is running besides checking for group. I have another form that the user enters all the information and that form calls where all the results are shown. If I don't use the timer, it takes a long time for the result form to pop-up.
Why d you think that a timer is the solution. Why not just place the code in the form shown event so the form opens and then is populated.

With what you have posted it is not really possible to understand what you are trying to do or where your issue originates.
I first tried that but it still takes a long time for the form to pop-up. Then I found the article on this site suggesting to use the timer.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

That is because you are not using the "shown" event of the form. YOU are using the "load" event. Change to the "shown" event which happens after the form "pops up" and it only happens once.
  1. $form1_Shown={
  2.     # your code here
  3. }
User avatar
gutihz
Posts: 38
Last visit: Fri Nov 10, 2023 10:54 am

Re: Code runs at least twice

Post by gutihz »

I tried the shown event too. The child form pops-up after everything has finished.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Code runs at least twice

Post by jvierra »

That doesn't make any sense so I suspect you have other issues. Yu will need to create a simple example of your issue and post the PSF so we can look at it.
This topic is 7 years and 4 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