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.
User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

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.  
  3.         [TimeSpan]$span = $script:StartTime - (Get-Date)
  4.  
  5.         if ($span.TotalSeconds -le 0)
  6.  
  7.         {
  8.  
  9.         $timer.Stop()
  10.  
  11.         }
  12.  
  13.     $Users = @()
  14.  
  15.     $OfferGroup = [ADSI]"WinNT://$ComputerName/Offer Remote Assistance Helpers,group"
  16.  
  17.     $Members = @($OfferGroup.psbase.Invoke("Members"))
  18.  
  19.     $Members | ForEach-Object {
  20.  
  21.         $txtboxUsers.AppendText($_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null))
  22.  
  23.         $txtboxUsers.AppendText("`n")
  24.  
  25.     }
  26.  
  27. }
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
Site Admin
Posts: 1098
Joined: Mon Aug 17, 2015 2:25 pm

Re: Code runs at least twice

Post by DevinL »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
DevinL
SAPIEN Technologies, Inc.

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

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
Joined: Fri Mar 20, 2015 1:42 pm

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
Joined: Wed Oct 08, 2014 6:02 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: 14550
Joined: Tue May 22, 2007 9:57 am
Contact:

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
Joined: Fri Mar 20, 2015 1:42 pm

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: 14550
Joined: Tue May 22, 2007 9:57 am
Contact:

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.  
  3.     # your code here
  4.  
  5. }

User avatar
gutihz
Posts: 38
Joined: Fri Mar 20, 2015 1:42 pm

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: 14550
Joined: Tue May 22, 2007 9:57 am
Contact:

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.

Locked