Multithreading in VB.NET

Introduction

Multithreading is new to VB developer, VB developer always wanted this feature in the language and its there in VB .NET.

One of the many great features in Windows 95 and Windows NT is multiple threads. The power of multithreaded programming requires responsible programming. It's up to you to ensure that the CPU switching between threads won't cause bad things to happen. For example, you might have data (such as a linked list) that can be accessed by more than one thread. Your code needs to ensure that a threads switch at the wrong moment won't leave your data in an inconsistent state. You prevent threads-switching problems by using synchronization objects.

Threading

One or more threads run in an AppDomain. An AppDomain is a runtime representation of a logical process within a physical process. A thread is the basic unit to which the operating system allocates processor time. Each AppDomain is started with a single thread, but can create additional threads from any of its threads.

Each thread maintains exception handlers, a scheduling priority, and a set of structures the system will use to save the thread context until it is scheduled. The thread context includes the thread's set of machine registers and stack, in the address space of the thread's process.

Operating systems that support preemptive multitasking create the effect of simultaneous execution of multiple threads from multiple processes. On a multiprocessor computer, some operating systems that are multi-processor aware can simultaneously execute as many threads as there are processors on the computer.

A multitasking operating system divides the available processor time among the processes or threads that need it. The system is designed for preemptive multitasking; it allocates a processor time slice to each thread it executes. The currently executing thread is suspended when its time slice elapses, allowing another thread to run. When the system switches from one thread to another, it saves the thread context of the preempted thread and restores the saved thread context of the next thread in the queue.

Synchronization

Synchronizing resource access between threads is a common problem when writing multithreaded applications. Having two or more threads simultaneously access the same data can lead to undesirable and unpredictable results. For example, one thread could be updating the contents of a structure while another thread is reading the contents of the same structure. It is unknown what data the reading thread will receive: the old data, the newly written data, or possibly a mixture of both. .NET provides a number of synchronization and synchronization access classes to aid in solving this problem. 

This article explains the Monitor class available and how to use them to create thread-safe classes in a typical multithreaded application.

Monitors

  • Monitors are associated with an object on demand.
  • Monitors are unbound i.e. can be called directly from any Context. 
  • Monitors cannot be instantiated; they are associated with an object.
Monitors expose the ability to take and release the sync block lock on an object on demand via Enter, TryEnter and Exit. The Monitor Wait, Pulse and PulseAll methods are related to SyncBlocks. It is necessary to be in a synchronized region on an object before calling Wait or Pulse on that object. Wait releases the lock if it is held and waits to be notified. When Wait is notified, it returns and has obtained the lock again. Notify signals for next thread in wait queue to proceed.

Monitor.Enter(b)
Monitor.Wait(b, 1000)
. . . ' do some work
Monitor.Exit(b)

How to run?

This is a console application, which demonstrates the Multithreading capability of .NET in Visual Basic.

This is a banking application which demonstrate the use of synchronization, to see why synchronization is necessary comment the Monitor.Enter(Me) and Monitor.Exit(Me), the result will vary and u will get the feel why synchronization is needed.


Similar Articles