Lesson 2: Managing Threads in Visual Basic .NET

Printing QR Code 2d barcode in Visual Basic .NET Lesson 2: Managing Threads

Lesson 2: Managing Threads
Printing QR Code In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
www.OnBarcode.com
Reading QR Code 2d Barcode In VB.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
// C# public void GetPageSize() { // Request the URL WebResponse wr = WebRequest.Create(url).GetResponse(); bytes = int.Parse(wr.Headers["Content-Length"]); // Display the value for the Content-Length header ///// Console.WriteLine(url + ": " + bytes.ToString()); wr.Close(); callback(this); // Let the parent thread know the process is done are.Set(); }
Bar Code Printer In Visual Basic .NET
Using Barcode generator for VS .NET Control to generate, create bar code image in .NET applications.
www.OnBarcode.com
Barcode Decoder In VB.NET
Using Barcode reader for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
6. Finally, update the foreach loop in the Main method to create and start a new Thread instance rather than calling ThreadPool.QueueUserWorkItem. Pass the callback method to the PageSize constructor, as shown here:
Paint QR Code In C#
Using Barcode generation for .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications.
www.OnBarcode.com
Create QR Code In VS .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
www.OnBarcode.com
' VB For Each url As String In urls waitHandles(i) = New AutoResetEvent(False) Dim ps As New PageSize(url, waitHandles(i), _ New ResultDelegate(AddressOf ResultCallback)) Dim t As New Thread(New ThreadStart(AddressOf ps.GetPageSize)) t.Start() i += 1 Next // C# foreach (string url in urls) { waitHandles[i] = new AutoResetEvent(false); PageSize ps = new PageSize(url, waitHandles[i], new ResultDelegate(ResultCallback)); Thread t = new Thread(new ThreadStart(ps.GetPageSize)); t.Start(); i++; }
Make QR Code In VS .NET
Using Barcode generator for VS .NET Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications.
www.OnBarcode.com
Printing Barcode In VB.NET
Using Barcode maker for VS .NET Control to generate, create bar code image in .NET framework applications.
www.OnBarcode.com
7. Build and run the application. Although it functions exactly the same as it did in Lesson 2, Exercise 1, the results are now being processed by a callback method. In the real world, this is a much more useful scenario background threads almost always need to return results to the foreground thread.
Printing EAN / UCC - 13 In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create GS1 128 image in .NET framework applications.
www.OnBarcode.com
PDF 417 Maker In VB.NET
Using Barcode creator for .NET framework Control to generate, create PDF417 image in .NET framework applications.
www.OnBarcode.com
7
Generate EAN-13 In Visual Basic .NET
Using Barcode generation for Visual Studio .NET Control to generate, create EAN / UCC - 13 image in Visual Studio .NET applications.
www.OnBarcode.com
ITF Generator In VB.NET
Using Barcode printer for .NET Control to generate, create 2/5 Interleaved image in .NET applications.
www.OnBarcode.com
Threading
Universal Product Code Version A Printer In Objective-C
Using Barcode generation for iPhone Control to generate, create GTIN - 12 image in iPhone applications.
www.OnBarcode.com
Creating Barcode In None
Using Barcode creation for Microsoft Word Control to generate, create bar code image in Office Word applications.
www.OnBarcode.com
Lesson Summary
PDF-417 2d Barcode Reader In Visual C#
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Generate ANSI/AIM Code 128 In Java
Using Barcode printer for Java Control to generate, create ANSI/AIM Code 128 image in Java applications.
www.OnBarcode.com
You can create an instance of the Thread class to provide more control over background threads than is available using ThreadPool.QueueUserWorkItem. Define the Thread.Priority property if you want to run the thread using a priority other than Normal. When the thread is configured, call Thread.Start to begin processing. If you need to stop the background thread, call Thread.Abort. If you might abort a thread, you should catch ThreadAbortException in the method to allow the method to close any open resources. If your foreground thread needs to monitor background threads, you can check the Thread.ThreadState property. When using the Thread class, the easiest way to pass data to a method is to create an instance of the class containing the method, and define attributes of the class. To pass data from a thread, define a callback method. Often, multiple threads need access to the same resources. To minimize resource conflicts, use Monitor locks to allow only a single thread to access a resource. If you want to provide separate logic for read locks and write locks, create an instance of the ReaderWriterLock class. To prevent basic mathematical calculations from being corrupted in multithreaded environments, use the static methods of the Interlocked class. The simplest way to wait for a thread to complete is to call Thread.Join. To wait for multiple threads to complete, create an array of AutoResetEvent objects, pass one item to each thread, and then call WaitHandle.WaitAll or WaitHandle.WaitAny from the foreground thread.
Read UCC - 12 In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
USS-128 Encoder In Java
Using Barcode creator for BIRT reports Control to generate, create GS1-128 image in BIRT applications.
www.OnBarcode.com
Lesson Review
Scanning Bar Code In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
PDF417 Generation In C#.NET
Using Barcode generator for VS .NET Control to generate, create PDF-417 2d barcode image in .NET framework applications.
www.OnBarcode.com
You can use the following questions to test your knowledge of the information in Lesson 2, Managing Threads. The questions are also available on the companion CD if you prefer to review them in electronic form.
NOTE
Answers
Answers to these questions and explanations of why each answer choice is right or wrong are located in the Answers section at the end of the book.
1. You will create an application that starts a new Thread object to run a method. You want the Thread to run as quickly as possible, even if that means it receives more processor time than the foreground thread. Which code sample does this correctly
Lesson 2: Managing Threads
' VB Dim DoWorkThread As New Thread(New ThreadStart(AddressOf DoWork)) DoWorkThread.ThreadState = ThreadState.Running DoWorkThread.Start() // C# Thread DoWorkThread = new Thread(new ThreadStart(DoWork)); DoWorkThread.ThreadState = ThreadState.Running; DoWorkThread.Start();
' VB Dim DoWorkThread As New Thread(New ThreadStart(AddressOf DoWork)) DoWorkThread.Priority = ThreadPriority.Highest DoWorkThread.Start() // C# Thread DoWorkThread = new Thread(new ThreadStart(DoWork)); DoWorkThread.Priority = ThreadPriority.Highest; DoWorkThread.Start();
' VB Dim DoWorkThread As New Thread(New ThreadStart(AddressOf DoWork)) DoWorkThread.Priority = ThreadPriority.Lowest DoWorkThread.Start() // C# Thread DoWorkThread = new Thread(new ThreadStart(DoWork)); DoWorkThread.Priority = ThreadPriority.Lowest; DoWorkThread.Start();
' VB Dim DoWorkThread As New Thread(New ThreadStart(AddressOf DoWork)) DoWorkThread.ThreadState = ThreadState.WaitSleepJoin DoWorkThread.Start() // C# Thread DoWorkThread = new Thread(new ThreadStart(DoWork)); DoWorkThread.ThreadState = ThreadState.WaitSleepJoin; DoWorkThread.Start();
2. You are creating a method that is part of a custom class. The method might be run simultaneously within multiple threads. You need to ensure that no thread writes to the file while any thread is reading from the file. You want to provide the highest level of efficiency when multiple threads are reading from the file simultaneously. Which code sample should you use
Copyright © OnBarcode.com . All rights reserved.