Creating the Basic Application

This simple application draws lines on a form on the screen, with the drawing of each line controlled by a separate thread.

Create a Windows application project, and place a panel and three buttons on the form, as shown in Figure 12.8. The Add button will be used to create extra drawing threads, whereas the Pause and Resume buttons will be used to control the threads.

Figure 12.8: The basic form for the multithreaded application.

Add four Imports statements to the top of the project:

Imports System.Threading Imports System.Windows.Forms Imports System.Drawing Imports System.Collections

You need System.Threading for the threading-related classes, Forms and Drawing for the GUI, and Collections because you'll use an ArrayList to store the threads as they are created.

Now add some member variables to the top of the class definition:

' The original drawing thread Dim thrd As Thread

' A graphics object to use for painting Dim grp As Graphics ' The form size Dim sz As Size

' An ArrayList to hold references to the active threads Dim threadList As New ArrayList()

The application will have one thread drawing lines when it starts up, and you can add more threads by pressing the Add button. All threads share the same Graphics object when drawing, so the code contains a reference to this object and another to the form's size.

0 0

Post a comment