<< Chapter < Page Chapter >> Page >

To create an AutoResetEvent event use a command like:

Dim mySyncdQEvent As New AutoResetEvent(False)

This will create an AutoResetEvent event called mySyncdQEvent with the initial value of False . When your code writes to a queue you can use the Set method to signal to the reader thread that something is on the queue. The reader thread can use the WaitOne method to wait to be signaled. The code for the writer thread to put something on the queue and then signal the event is:

mySyncdQ.Enqueue(Something) mySyncdQEvent.Set()

The reader thread can use code like:

mySyncdQEvent.WaitOne() While mySyncdQ.CountConsole.WriteLine(mySyncdQ.Dequeue()) End While

This will wait until something is available on the queue and then take all the items in the queue off and print the contents. This assumes that there is only one reader for the queue. If you have more than one reader for the queue you will need to lock the resource before using it. This is not explained here.

Example

This example starts with a VB Form application that contains one TextBox named TextBox1 . Double clicking on the TextBox will open the code view and make a TextBox1_TextChanged method. As you type into the TextBox this method will be executed for each character that changes.

The example has one queue called myQ and is used as a synchronized queue with mySyncdQ . There is also a thread named thread1 that is started that runs the method ThreadMethodReceive . This method will remove items from the queue and print them to the console.

Every time the text changes in the TextBox the TextBox1_TextChanged method puts the contents of the TextBox onto the queue and signals a AutoResetEvent named mySyncdQEvent . The thread thread1 calls the method WaitOne for the AutoResetEvent event. This will cause the thread to block until there is something on the queue.

Imports System.Threading Public Class Form1' Thread that will receive the messages that are put on the queue Dim thread1 As New Thread(AddressOf ThreadMethodReceive)' Queue where message will be stored Dim myQ As New Queue' Thread safe queue that will be used Dim mySyncdQ As Queue = Queue.Synchronized(myQ)' Event used to syncronize the TextBox putting items on the queue and the thread Dim mySyncdQEvent As New AutoResetEvent(False)' Method executed when the form is loadedPrivate 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 lowestthread1.Priority = ThreadPriority.Lowest ' Start the threadthread1.Start() End Sub' Thread that runs seperate from the Form1 threadPrivate Sub ThreadMethodReceive() ' Loop foreverWhile 1 ' Half second pause before checking the sync eventThread.Sleep(500) ' Wait on the sync event. If something has been put on' the queue there will be an event. mySyncdQEvent.WaitOne()' Continue removing items from the queue until it is empty While mySyncdQ.Count' Print the items removed from the queue Console.WriteLine(mySyncdQ.Dequeue())End WhileEnd WhileEnd SubPrivate Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged ' Put the content of the text box on the queuemySyncdQ.Enqueue(TextBox1.Text) ' Signal that something has been added to the queue.mySyncdQEvent.Set()End Sub End Class

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