<< 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

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
Rachel Woolard
Start Quiz
Heather McAvoy
Start Quiz
Yacoub Jayoghli
Start Quiz
Kimberly Nichols
Start Test