<< Chapter < Page Chapter >> Page >
This module describes the very basics of delegates in Microsoft Visual Basic.

Introduction

When developing Visual Basic applications in a multithreaded invironment it may be necessary for a thread to access a GUI object in the form thread. However it is not possible for the thread to access the object directly so it must us a delegate that runs in the thread that handles the GUI object. This module describes the very basics of how to use delegate methods so that more than one thread may access a GUI object.

Reading

Background

When a simple Visual Basic (VB) GUI application is made, there is a form class that handles all the GUI controls. This form class is a thread that handles all the interaction with the controls. It is possible to have other threads in our application. However, it is not possible for the other threads to have direct access to the GUI controls. The reason for this is it could be possible for the form thread and the other thread to try and change the same control at the same time.

To fix the problem of accessing the same control from two threads, a delegate function is made in the thread that handles the control object. The delegate function is invoked by other threads and the delegate function operates in the thread that handles the control object.

From the MSDN: "Delegates are similar to function pointers in C or C++ languages. Delegates encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code that calls the referenced method, and the method to be invoked can be unknown at compile time. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and more secure."

"The Delegate Class represents a delegate, which is a data structure that refers to a static method or to a class instance and an instance method of that class."

Visual basic delegates

Suppose you have a TextBox control object in your Form called TextBox1 . You can change the text in the TextBox by issuing a command like this:

TextBox1.Text = "New Text In Text Box"

This will change the Text property of the TextBox control object. When this is done within the Form thread everything works as expected. For instance, suppose you have a button in your Form called Button1 . Then in the click method for that button you could have something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = "I just got clicked"End Sub

This would all take place within the Form thread. The Form thread is the thread that created the TextBox control and the Button control so this is all within the same thread.

Suppose now you have a thread that you started that runs separate from the Form thread. If the thread tries to change the TextBox object you will get an error. If the thread looked like the following code, there would be a problem.

Private Sub Task1() TextBox1.Text = "Task Text"End Sub

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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?

Ask