Multithreading a Console Application


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 13.  Creating a Console Application

Multithreading a Console Application

The FileSort application is a utility application that sorts the contents of text files. The intent is that the text files contain fixed-length records, each record represented by a line of text, and we want to sort the lines of text by some subfields in the line of text. Perhaps the text file is an exported table from a legacy database.

The example presumes that there might be very large and very small files and some further processinglike loading the rows into a relational databasewill be performed on the data. To ensure that shorter files do not get stuck behind longer files, we elected to allow the user to specify several files on the command line and thread the sort process. Each file is sorted on its own thread.

From Listing 13.7, we can determine that the Main.ProcessEach is defined to enumerate the list of command-line arguments and process each argument that represents a valid command line argument. (For our purposes, ValidCommandLine ensures that the argument is an existing file. Refer to lines 5 through 8 and lines 119 through 121 of Listing 13.7 for the implementation of command-line validation.) If an element of the command line is an existing file, the shared method Sorter.Sort is called.

  20:  Private Overloads Shared Sub ProcessEach(_  21:  ByVal Enumerator As IEnumerator)  22:  While (Enumerator.MoveNext())  23:  If (ValidCommandLine(Enumerator.Current)) Then  24:  Sorter.Sort(Enumerator.Current)  25:  End If  26:  End While  27:  End Sub 

The preceding fragment from Listing 13.7 calls the shared method in the Sorter class. Sorter creates an instance of itself and creates and starts a thread to perform the actual sorting (see Listing 13.8).

Listing 13.8 The Sorter class performs the file sort on a separate thread as demonstrated
  42:  Public Class Sorter  43:  Private FFileName As String  44:  Private FData As ArrayList  45:   46:  Public Property FileName() As String  47:  Get  48:  Return FFileName  49:  End Get  50:  Set(ByVal Value As String)  51:  FFileName = Value  52:  End Set  53:  End Property  54:   55:  Public Sub New(ByVal AFileName As String)  56:  FData = New ArrayList()  57:  FFileName = AFileName  58:  End Sub  59:   60:  Public Sub Read()  61:  Dim Reader As TextReader = File.OpenText(FFileName)  62:   63:  Try  64:  While Reader.Peek <> -1  65:  FData.Add(Reader.ReadLine)  66:  End While  67:   68:  Finally  69:  Reader.Close()  70:  End Try  71:   72:  End Sub  73:   74:  Private Sub WriteElapsedTime(ByVal Elapsed As Double)  75:  Debug.WriteLine(String.Format("Elapsed milliseconds: {0}  ", Elapsed))  76:  End Sub  77:   78:  Public Sub TimedSort()  79:  Dim Start As Double = Timer  80:  Sort()  81:  WriteElapsedTime(Timer - Start)  82:  End Sub  83:   84:  Public Sub Sort()  85:  FData.Sort(New StringComparer())  86:  End Sub  87:   88:  Private Function TempFileName() As String  89:  Return "..\ " & Path.GetFileName(Path.GetTempFileName)  90:  End Function  91:   92:  Public Sub Write()  93:  Dim Writer As TextWriter = File.CreateText(TempFileName())  94:  Dim Enumerator As IEnumerator = FData.GetEnumerator  95:   96:  Try  97:  While Enumerator.MoveNext  98:  Writer.WriteLine(Enumerator.Current)  99:  End While  100:   101:  Finally  102:  Writer.Close()  103:  End Try  104:   105:  End Sub  106:   107:  Private Sub Run()  108:  Read()  109:  TimedSort()  110:  Write()  111:  End Sub  112:   113:  Public Shared Sub Sort(ByVal FileName As String)  114:  Dim Instance As New Sorter(FileName)  115:  Dim Thread As New Threading.Thread(AddressOf Instance.Run)  116:  Thread.Start()  117:  End Sub  118:   119:  Public Shared Function Valid(ByVal CommandLine As String) As Boolean  120:  Return System.IO.File.Exists(CommandLine)  121:  End Function  122:   123:  End Class 

The shared Sort method takes a FileName argument (lines 113 to 117) and creates an instance of the Sorter class, initializing the FFileName field. Fulfilling the requirements of threading (refer to Chapter 14, "Multithreaded Applications," for more on writing multithreaded VB applications), we must create an instance of the Thread class, passing a Delegate. Line 115 creates the thread and passes the address of the Instance.Run method. Run will be the starting point for the new thread. Line 116 starts the thread; at line 116 control branches to Instance.Run.


As a general strategy, you might want to consider adding timing code to algorithms that might be processor- or time- intensive . It is reasonable to assume that empirical data reflecting how long key processes are taking will be beneficial at some point during development. The TimedSort method in Listing 13.8 demonstrates an example using the Timer function.

Run is very short. Run calls Read to load the file into the ArrayList FData, calls TimedSort, and writes the results to a new file. (Run is defined on lines 107 to 111. We covered Read and Write in the earlier sections "Using a TextReader" and "Using a TextWriter," respectively.) TimedSort is implemented to call the ArrayList.Sort method wrapped by code that gets a relative start and stop time and writes the results to the Output window.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: