Windows Forms in VBNET Tutorial With Example Code
Windows Forms in VBNET Tutorial With Example Code
NET
To create your first program, drag the button control into the form, and change its default Text
Button1 to OK in the properties window, the word OK will appear on the button in the form, as
shown in Figure
Next, click on the OK button and the code window appears. Enter the code as shown in Figure
2.3:
Example 2-
Next I will show you how to create a simple calculator that adds two numbers using the TextBox
control. In this program, you insert two textboxes , three labels and one button. The two
textboxes are for the users to enter two numbers, one label is to display the addition operator and
the other label is to display the equal sign. The last label is to display the answer. Now change
the label on the button to Calculate,then click on this button and enter the following code:
End Sub
When you run the program and enter two numbers, pressing the calculate button can let the
progam performs addition of the two numbers, as shown in Figure 2.5.
MAXIMUM NO CALCULATOR
his program allows the user to enter three hidden numbers and the program can calculate the
maximum number among the three numbers. In order to allow the user to enter the password in
hidden mode, you have to set the PasswordChar property to alphanumeric symbols such as * .
You can create a function calMax that consists of three arguments x,y, z .You also need to write
a procedure to call this function. This procedure employs the If Then ElseIf statements and the
conditional operators to determine the maximum number. The function Str is used to convert a
numeric to string.
The Code
calMax = Str(x)
ElseIf y > x And y > z Then
calMax = Str(y)
ElseIf z > x And z > y Then
calMax = Str(z)
End If
End Function
Lbl_Display.Caption = calMax(a, b, c)
End Sub
Loading form
Now you are ready to write the code for the event procedure so that it will do something more than
loading a blank form. The code must be entered between Private Sub.......End Sub. Let's enter the
following code :
End Sub
Introduction
When two or more process cooperates with each other, their order of execution must be
preserved otherwise there can be conflicts in their execution and inappropriate outputs can be
produced.
A cooperative process is the one which can affect the execution of other process or can be
affected by the execution of other process. Such processes need to be synchronized so that their
order of execution can be guaranteed.
The procedure involved in preserving the appropriate order of execution of cooperative processes
is known as Process Synchronization. There are various synchronization mechanisms that are
used to synchronize the processes.
In order to synchronize the cooperative processes, our main task is to solve the critical section
problem. We need to provide a solution in such a way that the following conditions can be
satisfied.
Primary
1. Mutual Exclusion
Our solution must provide mutual exclusion. By Mutual Exclusion, we mean that if one
process is executing inside critical section then the other process must not enter in the
critical section.
2. Progress
Progress means that if one process doesn't need to execute into critical section then it
should not stop other processes to get into the critical section.
Secondary
1. Bounded Waiting
We should be able to predict the waiting time for every process to get into the critical
section. The process must not be endlessly waiting for getting into the critical section.
2. Architectural Neutrality
Our mechanism must be architectural natural. It means that if our solution is working fine
on one architecture then it should also run on the other ones as well.
Lock Variable
This is the simplest synchronization mechanism. This is a Software Mechanism implemented in
User mode. This is a busy waiting solution which can be used for more than two processes.
In this mechanism, a Lock variable lockis used. Two values of lock can be possible, either 0 or 1.
Lock value 0 means that the critical section is vacant while the lock value 1 means that it is
occupied.
A process which wants to get into the critical section first checks the value of the lock variable.
If it is 0 then it sets the value of lock as 1 and enters into the critical section, otherwise it waits.
1. Entry Section →
2. While (lock! = 0);
3. Lock = 1;
4. //Critical Section
5. Exit Section →
6. Lock =0;
If we look at the Pseudo Code, we find that there are three sections in the code. Entry Section,
Critical Section and the exit section.
Initially the value of lock variable is 0. The process which needs to get into the critical section,
enters into the entry section and checks the condition provided in the while loop.
The process will wait infinitely until the value of lock is 1 (that is implied by while loop). Since,
at the very first time critical section is vacant hence the process will enter the critical section by
setting the lock variable as 1.
When the process exits from the critical section, then in the exit section, it reassigns the value of
lock as 0.
1. Mutual Exclusion
2. Progress
3. Bounded Waiting
4. Portability
Out of the four parameters, Mutual Exclusion and Progress must be provided by any solution.
Let?s analyze this mechanism on the basis of the above mentioned conditions.
Mutual Exclusion
The lock variable mechanism doesn't provide Mutual Exclusion in some of the cases. This can be
better described by looking at the pseudo code by the Operating System point of view I.E.
Assembly code of the program. Let's convert the Code into the assembly language.
1. Load Lock, R0
2. CMP R0, #0
3. JNZ Step 1
4. Store #1, Lock
5. Store #0, Lock
Let us consider that we have two processes P1 and P2. The process P1 wants to execute its
critical section. P1 gets into the entry section. Since the value of lock is 0 hence P1 changes its
value from 0 to 1 and enters into the critical section.
Meanwhile, P1 is preempted by the CPU and P2 gets scheduled. Now there is no other process in
the critical section and the value of lock variable is 0. P2 also wants to execute its critical section.
It enters into the critical section by setting the lock variable to 1.
Now, CPU changes P1's state from waiting to running. P1 is yet to finish its critical section. P1
has already checked the value of lock variable and remembers that its value was 0 when it
previously checked it. Hence, it also enters into the critical section without checking the updated
value of lock variable.
Now, we got two processes in the critical section. According to the condition of mutual
exclusion, morethan one process in the critical section must not be present at the same time.
Hence, the lock variable mechanism doesn't guarantee the mutual exclusion.
The problem with the lock variable mechanism is that, at the same time, more than one process
can see the vacant tag and more than one process can enter in the critical section. Hence, the lock
variable doesn't provide the mutual exclusion that's why it cannot be used in general.
Since, this method is failed at the basic step; hence, there is no need to talk about the other
conditions to be fulfilled.
Test Set Lock Mechanism
Modification in the assembly code
In lock variable mechanism, Sometimes Process reads the old value of lock variable and enters
the critical section. Due to this reason, more than one process might get into critical section.
However, the code shown in the part one of the following section can be replaced with the code
shown in the part two. This doesn't affect the algorithm but, by doing this, we can manage to
provide the mutual exclusion to some extent but not completely.
In the updated version of code, the value of Lock is loaded into the local register R0 and then
value of lock is set to 1.
However, in step 3, the previous value of lock (that is now stored into R0) is compared with 0. if
this is 0 then the process will simply enter into the critical section otherwise will wait by
executing continuously in the loop.
The benefit of setting the lock immediately to 1 by the process itself is that, now the process
which enters into the critical section carries the updated value of lock variable that is 1.
In the case when it gets preempted and scheduled again then also it will not enter the critical
section regardless of the current value of the lock variable as it already knows what the updated
value of lock variable is.
Section 1 Section 2
1. Load Lock, R0 1. Load Lock, R0
2. CMP R0, #0 2. Store #1, Lock
3. JNZ step1 3. CMP R0, #0
4. store #1, Lock 4. JNZ step 1
TSL Instruction
However, the solution provided in the above segment provides mutual exclusion to some extent
but it doesn't make sure that the mutual exclusion will always be there. There is a possibility of
having more than one process in the critical section.
What if the process gets preempted just after executing the first instruction of the assembly code
written in section 2? In that case, it will carry the old value of lock variable with it and it will
enter into the critical section regardless of knowing the current value of lock variable. This may
make the two processes present in the critical section at the same time.
To get rid of this problem, we have to make sure that the preemption must not take place just
after loading the previous value of lock variable and before setting it to 1. The problem can be
solved if we can be able to merge the first two instructions.
In order to address the problem, the operating system provides a special instruction called Test
Set Lock (TSL) instruction which simply loads the value of lock variable into the local register
R0 and sets it to 1 simultaneously
The process which executes the TSL first will enter into the critical section and no other process
after that can enter until the first process comes out. No process can execute the critical section
even in the case of preemption of the first process.
1. TSL Lock, R0
2. CMP R0, #0
3. JNZ step 1
Mutual Exclusion
Progress
According to the definition of the progress, a process which doesn't want to enter in the
critical section should not stop other processes to get into it. In TSL mechanism, a
process will execute the TSL instruction only when it wants to get into the critical
section. The value of the lock will always be 0 if no process doesn't want to enter into the
critical section hence the progress is always guaranteed in TSL.
Bounded Waiting
Bounded Waiting is not guaranteed in TSL. Some process might not get a chance for so
long. We cannot predict for a process that it will definitely get a chance to enter in
critical section after a certain time.
Architectural Neutrality
TSL doesn't provide Architectural Neutrality. It depends on the hardware platform. The
TSL instruction is provided by the operating system. Some platforms might not provide
that. Hence it is not Architectural natural.