<< Chapter < Page Chapter >> Page >

Visual basic semaphores

Semaphores in VB are used to control access to a resource. They act as a counting semaphore except that there is a maximum value that the semaphore can have. This value is set when the semaphore is declared. The following is an example of the definition of a semaphore.

Dim Sem As New Semaphore(1, 2)

This defines a semaphore object named Sem that has a maximum value of 2 and an initial value of 1. The main methods for using the semaphore are WaitOne() and Release() . The WaitOne() method decrements the semaphore and the Release() method increases it. If the value of the semaphore is 0 when the WaitOne() method is called the thread will block. When the Release() method is called when a thread is blocked on the semaphore, it will unblock a thread without incrementing the semaphore. If the Release() method is called too any times and the semaphore value is increased past the maximum value, a SemaphoreFullException exception will occur. This module does not discuss how to handle the exception.

The main way to use the semaphore is shown in the following example.

Dim Sem As New Semaphore(1, 1)Sem.WaitOne() ' Use the protected resource hereSem.Release()

Writing to the console

To write to the console use function calls such as

Console.WriteLine("Name = {0}, hours = {1:hh}", My.Name, DateTime.Now)

The fixed text is "Name = " and ", hours = " . The format items are "{0}" , whose index is 0, which corresponds to the object My.Name , and "{1:T}" , whose index is 1, which corresponds to the object DateTime.Now . The format T will print the time in the long time format. An example of the output is:

Name = Form1, Time = 5:50:54 PM


This simple example uses Visual Studio 2010 and VB.NET Framework 4. Create a Form Application and enter the following code:

Imports System.Threading Public Class Form1Dim thread1 As New Thread(AddressOf Task)Dim thread2 As New Thread(AddressOf Task) Dim Sem As New Semaphore(1, 1)' When the form program starts or is loaded the following function is' called. This is where you can put the commands to set up our program. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load' Make this a background thread so it automatically' aborts when the main program stops. thread1.IsBackground = True' Set the thread priority to lowest thread1.Priority = ThreadPriority.Lowest' Start the thread thread1.Start("thread1")' Make this a background thread so it automatically' aborts when the main program stops. thread2.IsBackground = True' Set the thread priority to lowest thread2.Priority = ThreadPriority.Lowest' Start the thread thread2.Start("thread2")End SubPrivate Sub Task(ByVal name As Object)While 1' wait on the semaphore Sem.WaitOne()' Print the name of this thread and the current time Console.WriteLine("Name = {0}, Time1 = {1:T}", name, DateTime.Now)' Sleep to 2 seconds Thread.Sleep(2000)Console.WriteLine("Name = {0}, Time2 = {1:T}", name, DateTime.Now) ' Release the semaphoreSem.Release()End WhileEnd SubEnd Class

This program will start up two threads, thread1 and thread2 . Each thread will execute the method Task() and will pass the name of the thread. This will allow each thread to print something different even though they execute the same method. A semaphore is use to prevent the other thread from interrupting the two print statements. The two print statements for the thread should always print after each other. The following shows the results of the run. Note that the two times always print together.

Name = thread1, Time1 = 6:17:16 PM Name = thread1, Time2 = 6:17:18 PMName = thread1, Time1 = 6:17:18 PM Name = thread1, Time2 = 6:17:20 PMName = thread1, Time1 = 6:17:20 PM Name = thread1, Time2 = 6:17:22 PMName = thread2, Time1 = 6:17:22 PM Name = thread2, Time2 = 6:17:24 PMName = thread2, Time1 = 6:17:24 PM Name = thread2, Time2 = 6:17:26 PMName = thread2, Time1 = 6:17:26 PM Name = thread2, Time2 = 6:17:28 PM

The following shows the loop with the semaphore statements removed.

While 1 ' Print the name of this thread and the current timeConsole.WriteLine("Name = {0}, Time1 = {1:T}", name, DateTime.Now) ' Sleep to 2 secondsThread.Sleep(2000) Console.WriteLine("Name = {0}, Time2 = {1:T}", name, DateTime.Now)End While

This causes a printout like the following. Notice the two prints do not occur next to each other.

Name = thread2, Time1 = 6:20:44 PM Name = thread1, Time1 = 6:20:44 PMName = thread2, Time2 = 6:20:46 PM Name = thread1, Time2 = 6:20:46 PMName = thread1, Time1 = 6:20:46 PM Name = thread2, Time1 = 6:20:46 PMName = thread2, Time2 = 6:20:48 PM Name = thread1, Time2 = 6:20:48 PMName = thread1, Time1 = 6:20:48 PM Name = thread2, Time1 = 6:20:48 PMName = thread2, Time2 = 6:20:50 PM Name = thread2, Time1 = 6:20:50 PMName = thread1, Time2 = 6:20:50 PM Name = thread1, Time1 = 6:20:50 PM

Questions & Answers

are nano particles real
Missy Reply
Hello, if I study Physics teacher in bachelor, can I study Nanotechnology in master?
Lale Reply
no can't
where we get a research paper on Nano chemistry....?
Maira Reply
nanopartical of organic/inorganic / physical chemistry , pdf / thesis / review
what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
has a lot of application modern world
what is variations in raman spectra for nanomaterials
Jyoti Reply
ya I also want to know the raman spectra
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
Nasa has use it in the 60's, copper as water purification in the moon travel.
nanocopper obvius
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
analytical skills graphene is prepared to kill any type viruses .
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now

Source:  OpenStax, Ti dsp/bios lab. OpenStax CNX. Sep 03, 2013 Download for free at http://cnx.org/content/col11265/1.8
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Ti dsp/bios lab' conversation and receive update notifications?