Multithreaded Code for Our Upload Problem
Listing 1 illustrates the main program for the code seen in Figure 2.
Listing 1 The main program.
Console.WriteLine("Network Management System Upload Application\n"); // Create the upload class instance. UploadContent uc = new UploadContent(); // Instantiate the worker thread. Thread backgroundThread = new Thread(new ThreadStart(uc.UploadFirmware)); backgroundThread.Name = "Worker"; backgroundThread.Start(); // Do some additional work in parallel with the thread. MessageBox.Show("Other application user options", "Now available"); Console.ReadLine();
If you compare the code in Listing 1 with the program output in Figure 2, you’ll see that not a whole lot is going on. The first thing that happens after the initial WriteLine() statement is an instantiation of the class UploadContent. Then we create the required background thread (called backgroundThread). We give this thread a name ("Worker") and start the thread. At this point we display the MessageBox shown in Figure 2.
Let’s take a step back and look at the code inside the backgroundThread entity.