In this program, I have tried to add the dose of multimedia to the system clock. At every new hour, it plays an .mp3 file that ticks the number of times the current hour is.
For this, the program has to be loaded on Windows startup. So with this program, you can also automatically start programs whenever Windows launches. We refer to it as loading applications on Windows startup. If you have programs automatically starting that you have not loaded, then you can remove them using this code as well.
How’s it possible?
Loading an application on Windows startup is easy. You could manually do it using regedit.exe – a registry editor.
Open your registry and find the key: [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]. For each program you want to start automatically, create a new string value using a descriptive name, and set the value of the string to the program executable. For example, to automatically start Notepad, add a new entry of "Notepad"="c:\windows\notepad.exe".
Now, we will implement the same thing in our program. Instead of Notepad, we will load our own program “AudioClock.exe” on Windows startup.
VB.NET and Registry
Editing registry via VB.net requires importing
Microsoft.Win32 namespace that provides us the
Registry provides the set of standard root keys found in the registry on machines running Windows. The keys are all read-only since the registry depends on their existence.
RegistryKey class is a registry encapsulation and represents a key level node in the Windows registry.
RegistryKeys are the base units of organization in the registry, and can be compared to folders in Windows Explorer. A particular key can have subkeys (just as a folder can have subfolders), and can be deleted, as long as the user has the appropriate permissions to do so and the key is not a base key, or the level directly under the base keys. Each key can also have multiple values associated with it (a value can be compared to a file), which are used to store the information about the application you are interested in. Each value holds one particular piece of information, which can be retrieved or updated when required.
It’s time we had some coding.
Importing necessary namespaces
Private thisTime As Date
Private thisHour As Integer
Private directory As directory
Dim filename As String
Creating new string value
Private Sub writeToRegistry ()
Dim regKey As RegistryKey
regKey = _
Registry.CurrentUser.OpenSubKey(" SOFTWARE\" & _
fileName = directory.GetCurrentDirectory().ToString + "\audioClock.exe"
regKey.SetValue ("audioClock", fileName)
The above code snippet instantiates the
RegistryKey using the static member
Boolean parameter “
TRUE” provides a write access to the particular key. A new string value “
audioClock” is added with value equal to the full path and filename of our current application. Remind you, our application name is “audioClock.exe”. Change in its name requires the change to be made here. Please note the backslash before audioClock.exe.
When to tick?
Now, the time has come to add a dose of multimedia to Windows system clock. We want a .mp3 file to be played at every new hour, so what we need to keep track of is the system clock time. Use this simple arithmetic implementation to get the first tick.
Private Sub startClocking ()
thisTime = TimeOfDay
newTimer.Interval = (60 - thisTime.Minute) _
* 60 * 1000 - thisTime.Second * 1000
It should be noted that the timer interval is expressed in milliseconds.
Setting new Timer interval after the first tick has occurred
Once the clock has ticked first time, the next tick will occur after one hour. So, the new interval is set to one hour.
Private Sub continueClocking ()
newTimer.Interval = 60 * 60 * 1000
Time to play a .mp3 file
Private Sub newTimerTickEvent ( ByVal sender As Object, _
ByVal e As System.EventArgs) Handles newTimer.Tick
command= "mplay32.exe -play" + " "" " + getFilenameToPlay(thisTime) + " "" "
Shell( command, AppWinStyle.Hide)
The .mp3 file is played using the Media Player application that comes with the Windows OS. The application name is mplay32.exe. As you can see, I have used a shell function to launch the mplay32.exe program without displaying it. After all, we don’t want the Media Player to pop up whenever the music plays, do we?
Private Function getFilenameToPlay ( ByVal thisTime AsDate) As String returns the full path and filename of a file to be played.
What more can you do with this code?
This code has enough room for improvement and enhancement. It can be used in a wide range of applications viz., alert messaging applications, stop watch, reminders as that of Microsoft Outlook, automatic execution of other applications (automatic dialup connections!), shut down/restart/hibernate automatically at some X time, and many more. Truly speaking, it can be implemented in any kind of application that requires keeping track of system time.
It keeps track of system time only once on Windows startup. Thereafter, it uses its own timer to tick at every new hour. Any change in the system clock after the Windows startup is not detected until next startup. The limitation is a tradeoff against simplicity.
While writing this code …
Playing with registry has always become a fun part of programming. It provides a sense of power and superiority. Playing with system time added extra doses of control and authority.
Handle with care …
Modifying the registry can cause serious problems that may require you to reinstall your operating system. Administrative/Proper privilege is required for the modification.