<< 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 are components of cells
ofosola Reply
twugzfisfjxxkvdsifgfuy7 it
Sami
58214993
Sami
what is a salt
John
the difference between male and female reproduction
John
what is computed
IBRAHIM Reply
what is biology
IBRAHIM
what is the full meaning of biology
IBRAHIM
what is biology
Jeneba
what is cell
Kuot
425844168
Sami
what is cytoplasm
Emmanuel Reply
structure of an animal cell
Arrey Reply
what happens when the eustachian tube is blocked
Puseletso Reply
what's atoms
Achol Reply
discuss how the following factors such as predation risk, competition and habitat structure influence animal's foraging behavior in essay form
Burnet Reply
cell?
Kuot
location of cervical vertebra
KENNEDY Reply
What are acid
Sheriff Reply
define biology infour way
Happiness Reply
What are types of cell
Nansoh Reply
how can I get this book
Gatyin Reply
what is lump
Chineye Reply
what is cell
Maluak Reply
what is biology
Maluak
what is vertibrate
Jeneba
what's cornea?
Majak Reply
what are cell
Achol
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