Help with a Timer control issue I'm having

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 8 years and 1 month 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
ojrivera381
Posts: 4
Last visit: Fri Apr 01, 2022 3:15 am

Help with a Timer control issue I'm having

Post by ojrivera381 »

Hi all
I have created a Powershell gui script for mapping a drive for a client with a timer to disconnect after 1 hour. Using the timer control sample in the tutorial.

The issue I'm having is the timer is starting during form load and not when the button is clicked. What happens is if the the button is clicked after the timer expires then script process immediately to the end Any help with this would greatly be appreciated. I have searched and been beating my head on this for days now.

$OnLoadFormEvent={
#TODO: Initialize Form Controls here
$ndrive = test-path N:
If ($ndrive -eq $true) { cmd /c "net use N: /delete" }
else { "Statement is False" }
$TotalTime = 30 #in seconds Set to 3600 for 1 hour timer. Testing with 30
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
#Write-Host "Timer disabled"

}

$buttonReMapNDrive_Click={
#TODO: Place custom script here
$buttonReMapNDrive.Enabled = $false
New-PSDrive -name "N" -PSProvider Filesystem -root <#"\\[enter server\Share here without brackets]"#> -Persist -Credential "" -Scope Global
$timerUpdate.Start()
Write-Host "Timer Enabled" #Comment to see the steps that occur will be cleaned up

}

$buttonDisconnectNDrive_Click={
#TODO: Place custom script here
Remove-PSDrive -Name N -force
Write-Host "Removed" #Comment to see the steps that occur Will be cleaned up
}



$timerUpdate_Tick={
#TODO: Place custom script here
[TimeSpan]$span = $script:StartTime - (Get-Date)
if ($span.TotalSeconds -le 0)
{
$timerUpdate.Stop()
Remove-PSDrive -Name N -force
Write-Host "SCRIPT ENDED" #Comment to see steps that occur will be cleaned up
$MainForm.close()
return
}
}
User avatar
ojrivera381
Posts: 4
Last visit: Fri Apr 01, 2022 3:15 am

Re: Help with a Timer control issue I'm having

Post by ojrivera381 »

My apologies I think I posted this in the wrong section looks like it should be in Powershell Studio
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with a Timer control issue I'm having

Post by jvierra »

No - you are in the correct place.
Just set the properties of the timer to enabled =$false and the timer won't start.

I suggest using a timer control and keeping a list of drives and time and just test the time.
User avatar
ojrivera381
Posts: 4
Last visit: Fri Apr 01, 2022 3:15 am

Re: Help with a Timer control issue I'm having

Post by ojrivera381 »

Thanks for the reply. The properties are set to false.
Image

Am I supposed to add it to the code? If so where. Ps I'm new to this IDE

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

Re: Help with a Timer control issue I'm having

Post by jvierra »

The timer does not start until you tell it to. If it is running when the form opens then you have enabled it in code or in the designer.

You say the "properties" are set to false What properties are you referring to?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with a Timer control issue I'm having

Post by jvierra »

The timer does not start until you tell it to. If it is running when the form opens then you have enabled it in code or in the designer.

You say the "properties" are set to false What properties are you referring to?
User avatar
ojrivera381
Posts: 4
Last visit: Fri Apr 01, 2022 3:15 am

Re: Help with a Timer control issue I'm having

Post by ojrivera381 »

Thanks for the help. I've gone through all the code I don't see it anywhere else.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Help with a Timer control issue I'm having

Post by jvierra »

Well you may have broken code in your project. Try starting a new project and adding the code a little bit at a time.
This topic is 8 years and 1 month 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