Support Forums

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.
User avatar
ojrivera381
Posts: 4
Joined: Wed May 20, 2015 5:11 am

Help with a Timer control issue I'm having

Postby ojrivera381 » Fri Feb 12, 2016 6:38 am

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
Joined: Wed May 20, 2015 5:11 am

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

Postby ojrivera381 » Fri Feb 12, 2016 7:02 am

My apologies I think I posted this in the wrong section looks like it should be in Powershell Studio
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Fri Feb 12, 2016 7:53 am

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
Joined: Wed May 20, 2015 5:11 am

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

Postby ojrivera381 » Fri Feb 12, 2016 8:38 am

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
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Fri Feb 12, 2016 8:43 am

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

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

Postby jvierra » Fri Feb 12, 2016 8:43 am

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
Joined: Wed May 20, 2015 5:11 am

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

Postby ojrivera381 » Fri Feb 12, 2016 11:47 am

Thanks for the help. I've gone through all the code I don't see it anywhere else.
User avatar
jvierra
Posts: 10044
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Postby jvierra » Fri Feb 12, 2016 11:58 am

Well you may have broken code in your project. Try starting a new project and adding the code a little bit at a time.

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 9 guests