Florida web design - CHAPTER 14 BUILDING MULTITHREADED 464 TITHREADED APPLICATIONS
CHAPTER 14 BUILDING MULTITHREADED 464 TITHREADED APPLICATIONS To begin, set a reference to the System.Windows.Forms.dll assembly and display a message within Main() using MessageBox.Show() (you ll see the point of doing so once you run the program). Here is the complete implementation of Main(): static void Main(string[] args) { Console.WriteLine(”***** The Amazing Thread App *****n”); Console.Write(”Do you want [1] or [2] threads? “); string threadCount = Console.ReadLine(); // Name the current thread. Thread primaryThread = Thread.CurrentThread; primaryThread.Name = “Primary”; // Display Thread info. Console.WriteLine(”-> {0} is executing Main()”, Thread.CurrentThread.Name); // Make worker class. Printer p = new Printer(); switch(threadCount) { case “2″: // Now make the thread. Thread backgroundThread = new Thread(new ThreadStart(p.PrintNumbers)); backgroundThread.Name = “Secondary”; backgroundThread.Start(); break; case “1″: p.PrintNumbers(); break; default: Console.WriteLine(”I don’t know what you want…you get 1 thread.”); goto case “1″; } // Do some additional work. MessageBox.Show(”I’m busy!”, “Work on main thread…”); Console.ReadLine(); } Now, if you run this program with a single thread, you will find that the final message box will not display the message until the entire sequence of numbers has printed to the console. As you are explicitly pausing for approximately two seconds after each number is printed, this will result in a less-than-stellar end user experience. However, if you select two threads, the message box displays instantly, given that a unique Thread object is responsible for printing out the numbers to the console (see Figure 14-7).
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.