In this guide, I am going to demonstrate how to write a timer in Visual Basic using Visual Studio 2015. The program allows you to configure a certain time after which the system will automatically log out users. This can be useful on kiosk computers.

I am then going to go through the steps of configuring Group Policy to download the timer to a temporary folder on the workstation, and then start the timer when a user logs on. The timer will appear in the bottom right-hand corner of the screen and will remain on top of all open windows. It will change color when five minutes remain and again when two minutes remain.

The timer notifies users about the remaining session time. When the timer runs out, users receive a message that their session has expired. If users do not log out within thirty seconds, the timer will gracefully log them out to enforce the session time limit.

Creating the GUI

For this procedure, you will need Visual Studio 2015. You can download a free trial of Microsoft Visual Studio from Microsoft's Website, or you can use a fully licensed copy acquired through your organization. If you are a student, you can get Visual Studio for free when you verify your student status.

Open Visual Studio 2015 and create a new project by going to File > Project or by pressing ctrl+shift+N. Under the Templates header, expand the sub-header Visual Basic and select Windows Forms Application. Give your project a name and specify a location to save your project. I will be saving all my files to following location:

\\FSMGMT\ITLIB\Administrator\~Projects

Check the box Create directory for solution and click OK.

New project

New project

After creating your solution, you'll see a new tab at the top of your screen called Form1.vb [Design] and a blank Windows Form application. Click on the newly created form to select it. In the bottom right-hand corner of the Properties box, find the Appearance section and set the FormBorderStyle to "FixedDialog." Also, clear the current text entry in the Text section. This will allow for removal of the title bar.

Form border style

Form border style

While still in the Properties box, find the Layout section and Size subsection and configure the width and height as follows:

Width: 272
Height: 124

Size: width and height

Size: width and height

Finally, while still in the Properties box, find the Window Style section, and set the following properties:

ControlBox: False
MaximizeBox: False
MinimizeBox: False
ShowIcon: False
ShowInTaskbar: False
TopMost: True

Window Style section

Window Style section

Now that we've configured the main form, we can add text for the title and time. On the left-hand side of the screen, you should see three tabs: Server Explorer, Toolbox, and Data Source. Open the Toolbox tab and expand the Common Controls section. Drag and drop a label control onto to your form. Click on your newly created label to select it. In the bottom right-hand corner of the Properties box, find the Appearance section and configure the following properties:

Font: Calibri, 26
Text: Time Remaining

Go back to the Toolbox tab and drag and drop three more labels onto your form: one for the minutes, one for the divider between the minutes and seconds, and one for the seconds. Repeat the process above for each newly created label using the same font but a different size of 48. For the minutes label text, you are going to want to put how long you want the timer to run for in minutes.

For this guide, I will have my timer running for 15 minutes. For the divider label text, simply put a colon, and for the seconds label text, put 00. When you've configured all your labels, go back to the Toolbox tab one more time and expand the Components section. Drag and drop a timer component anywhere onto your form. Try and get the "Time Remaining," minutes, divider, and seconds labels as centered as possible. At this point, your timer should resemble the following image:

Incomplete workstation timer

Incomplete workstation timer

Creating the Visual Basic code

Now we're going to write the code that's going to provide the timer functionality. Double click the Timer1 component at the bottom of the screen to bring up the tab that contains the form code. It's pretty empty at the moment, but we're going to change that. Under Public Class Form1, copy and paste the following code:

Dim wsh As Object
Dim min As Integer
Dim sec As Integer
Dim x As Integer
Dim y As Integer

Here we just defined the data type for some important variables so the form knows that we intend to use Windows Script Host as an object, min and sec as time integers, and x and y as measurement integers. Next, we are going to set the variables our form will be using. Under Private Sub Form1_Loader, copy and paste the following code:

x = Screen.PrimaryScreen.WorkingArea.Width - Width
y = Screen.PrimaryScreen.WorkingArea.Height - Height
Location = New Point(x, y)
Timer1.Enabled = True
Timer1.Interval = 1000
sec = 0
min = 15
wsh = CreateObject("WScript.Shell")

The only thing you are going to want to change here is the value for the variable min, which will be whatever you used for the minutes label on the form. In this case, I chose 15, so I'm going to leave the min variable as is.

Finally, under Private Sub Timer1_Tick, copy and paste the following code:

Label1.Text = Format(min, "0#")
Label3.Text = Format(sec, "0#")
        sec = sec - 1
        If sec < 0 Then
            sec = 59
            min = min - 1
            If Label1.Text = "05" And Label3.Text = "00" Then
                Label1.ForeColor = Color.Gold
                Label2.ForeColor = Color.Gold
                Label3.ForeColor = Color.Gold
                Select Case wsh.popup("You have 5 minutes of your session remaining.",, "Information", 0 + 64)
                    Case 1
                End Select
            End If
            If Label1.Text = "02" And Label3.Text = "00" Then
                Label1.ForeColor = Color.Red
                Label2.ForeColor = Color.Red
                Label3.ForeColor = Color.Red
                Select Case wsh.popup("You have 2 minutes of your session remaining. Please save your work now.",, "Information", 0 + 64)
                    Case 1
                End Select
            End If
            If Label1.Text = "00" And Label3.Text = "00" Then
                Timer1.Enabled = False
                Select Case wsh.popup("Time's Up! You will automatically be logged off in 30 seconds.", 30, "Information", 0 + 64)
                    Case 1
                        Process.Start("shutdown.exe", "/l")
                        Close()
                    Case -1
                        Process.Start("shutdown.exe", "/l")
                        Close()
                End Select
            End If
        End If

There are a few things to note here. Label1 (min), Label2 (:), and Label3 (sec) correspond to the labels we dragged and dropped onto the form earlier. Make sure that the name for each label, which you can find under the Design section in the Properties box, corresponds with its respective label in our code. Also, even though the timer stops after 15 minutes, users have a grace period of 30 seconds to save their files and log out. After 30 seconds, the system will log users out automatically. To change the grace period, find the line "Select Case wsh.popup("Time's Up! You will automatically be logged off in 30 seconds.", 30, "Information", 0 + 64)" and change both instances of 30 to the desired grace period in seconds. You may also change the timer color and popup message as well.

That's all the code we are going to need for this application; your final code should look like the following:

Public Class Form1
    Dim wsh As Object
    Dim min As Integer
    Dim sec As Integer
    Dim x As Integer
    Dim y As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        x = Screen.PrimaryScreen.WorkingArea.Width - Width
        y = Screen.PrimaryScreen.WorkingArea.Height - Height
        Location = New Point(x, y)
        Timer1.Enabled = True
        Timer1.Interval = 1000
        sec = 0
        min = 15
        wsh = CreateObject("WScript.Shell")
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Label1.Text = Format(min, "0#")
        Label3.Text = Format(sec, "0#")
        sec = sec - 1
        If sec < 0 Then
            sec = 59
            min = min - 1
            If Label1.Text = "05" And Label3.Text = "00" Then
                Label1.ForeColor = Color.Gold
                Label2.ForeColor = Color.Gold
                Label3.ForeColor = Color.Gold
                Select Case wsh.popup("You have 5 minutes of your session remaining.",, "Information", 0 + 64)
                    Case 1
                End Select
            End If
            If Label1.Text = "02" And Label3.Text = "00" Then
                Label1.ForeColor = Color.Red
                Label2.ForeColor = Color.Red
                Label3.ForeColor = Color.Red
                Select Case wsh.popup("You have 2 minutes of your session remaining. Please save your work now.",, "Information", 0 + 64)
                    Case 1
                End Select
            End If
            If Label1.Text = "00" And Label3.Text = "00" Then
                Timer1.Enabled = False
                Select Case wsh.popup("Time's Up! You will automatically be logged off in 30 seconds.", 30, "Information", 0 + 64)
                    Case 1
                        Process.Start("shutdown.exe", "/l")
                        Close()
                    Case -1
                        Process.Start("shutdown.exe", "/l")
                        Close()
                End Select
            End If
        End If
    End Sub
End Class

Now we are going to finalize the application properties and save it as an executable. Navigate to the Solution Explorer pane, right-click on Visual Basic, and select Properties.

Workstation timer properties

Workstation timer properties

Click on the Assembly Information button and fill in the Description and Company fields. Click OK when you are finished. Next, add an icon by clicking on the drop-down scroll list under "Icon:" and selecting Browse. I simply searched for a free icon on the internet and chose the one I liked the best.

Timer Icon

Timer Icon

With the application coded and the deployment information set, we are now going to build the application. This is different from publishing the application, as we want to export the application as an executable, not an online installer. Go to the menu bar and under the Build menu bar option, select the option Build Solution. The solution should build fairly quickly, and you can see the results of the build in the bottom window pane called Output.

Build results

Build results

When you go to the output directory, you should see your solution outputted as an executable, as well as some other configs and files.

Debug directory

Debug directory

Deploy the timer with Group Policy

Copy the executable and paste it in your domain's NETLOGON folder. This file will need to be accessible during the logon process when downloading the application to a temporary folder on a workstation and running it when a user logs on. After building the timer application and copying it to the NETLOGON share, the last step is to configure the necessary group policies.

Launch the Group Policy Management Microsoft Management Console (MMC) snap-in and create a new group policy that will download and launch the workstation timer. I will be using this timer to manage session time limits of guests on virtual desktop infrastructure (VDI) clients. Thus, I have created a new group policy object called "Quick~WKS Guest Experience Policy" and applied the following security filtering settings:

Security filtering

Security filtering

Open your newly created Group Policy Object (GPO) and navigate to User Configuration > Policies > Administrative Templates > System > Logon, then find the setting "Run these programs at user logon." Next to "Items to run at logon," click the Show button and enter the full path to the executable. This path should be the one to where you plan to download the Workstation Timer executable.

Run at logon

Run at logon

Finally, we need to tell the GPO to download the workstation timer onto the computer. Navigate to User Configuration > Preferences > Windows Settings > Files and create a new file. For the source path, put the full path to the workstation timer in your NETLOGON share. For the destination path, put the path you specified earlier for the "Items to run at logon" list. Make sure to include the executable itself in the path name.

Files

Files

With your programs to run at logon and files preferences configured, your GPO report should resemble the following:

GPO report

GPO report

Force a Group Policy update on the machines on which you want the timer to run. When you login as a user in one of the filtered specified security groups, the timer should start to run.

Subscribe to 4sysops newsletter!

Workstation timer

Workstation timer

Five minutes remaining

Five minutes remaining

Two minutes remaining

Two minutes remaining

Time's up

Time's up

avataravatar
2 Comments
  1. Interesting procedure. Thanks for sharing.

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account