home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic .NET - Read Less - Learn More / Visual_Basic.NET_Read_Less_Learn_More_Richard_Bowman_Visual_2002.iso / Resources / Code / Ch8-ModifyThreadExec / Form1.vb < prev   
Text File  |  2001-08-09  |  4KB  |  116 lines

  1. Public Class Form1
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'Form overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.     Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
  26.     Friend WithEvents Panel1 As System.Windows.Forms.Panel
  27.     Friend WithEvents btnPause As System.Windows.Forms.Button
  28.     Friend WithEvents btnStop As System.Windows.Forms.Button
  29.  
  30.     'Required by the Windows Form Designer
  31.     Private components As System.ComponentModel.Container
  32.  
  33.     'NOTE: The following procedure is required by the Windows Form Designer
  34.     'It can be modified using the Windows Form Designer.  
  35.     'Do not modify it using the code editor.
  36.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  37.         Me.btnPause = New System.Windows.Forms.Button()
  38.         Me.btnStop = New System.Windows.Forms.Button()
  39.         Me.ListBox1 = New System.Windows.Forms.ListBox()
  40.         Me.Panel1 = New System.Windows.Forms.Panel()
  41.         Me.Panel1.SuspendLayout()
  42.         Me.SuspendLayout()
  43.         '
  44.         'btnPause
  45.         '
  46.         Me.btnPause.Location = New System.Drawing.Point(0, 8)
  47.         Me.btnPause.Name = "btnPause"
  48.         Me.btnPause.Size = New System.Drawing.Size(72, 24)
  49.         Me.btnPause.TabIndex = 0
  50.         Me.btnPause.Text = "Pause"
  51.         '
  52.         'btnStop
  53.         '
  54.         Me.btnStop.Location = New System.Drawing.Point(72, 8)
  55.         Me.btnStop.Name = "btnStop"
  56.         Me.btnStop.Size = New System.Drawing.Size(72, 24)
  57.         Me.btnStop.TabIndex = 1
  58.         Me.btnStop.Text = "Stop"
  59.         '
  60.         'ListBox1
  61.         '
  62.         Me.ListBox1.Dock = System.Windows.Forms.DockStyle.Fill
  63.         Me.ListBox1.IntegralHeight = False
  64.         Me.ListBox1.Location = New System.Drawing.Point(5, 5)
  65.         Me.ListBox1.Name = "ListBox1"
  66.         Me.ListBox1.Size = New System.Drawing.Size(282, 231)
  67.         Me.ListBox1.TabIndex = 0
  68.         '
  69.         'Panel1
  70.         '
  71.         Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnStop, Me.btnPause})
  72.         Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom
  73.         Me.Panel1.Location = New System.Drawing.Point(5, 236)
  74.         Me.Panel1.Name = "Panel1"
  75.         Me.Panel1.Size = New System.Drawing.Size(282, 32)
  76.         Me.Panel1.TabIndex = 1
  77.         '
  78.         'Form1
  79.         '
  80.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  81.         Me.ClientSize = New System.Drawing.Size(292, 273)
  82.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Panel1})
  83.         Me.DockPadding.All = 5
  84.         Me.Name = "Form1"
  85.         Me.Text = "Form1"
  86.         Me.Panel1.ResumeLayout(False)
  87.         Me.ResumeLayout(False)
  88.  
  89.     End Sub
  90.  
  91. #End Region
  92.  
  93.     Sub RunThread()
  94.         Do
  95.             SyncLock (ListBox1)
  96.                 ListBox1.Items.Add("The thread is executing")
  97.             End SyncLock
  98.             Threading.Thread.CurrentThread.Sleep(1000)
  99.         Loop
  100.     End Sub
  101.  
  102.     Private newThread As New Threading.Thread(AddressOf RunThread)
  103.  
  104.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  105.         newThread.Start()
  106.     End Sub
  107.  
  108.     Private Sub btnPause_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPause.Click        If btnPause.Text = "Pause" Then            newThread.Suspend()            btnPause.Text = "Resume"        Else            newThread.Resume()            btnPause.Text = "Pause"        End If    End Sub
  109.     Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
  110.         newThread.Abort()
  111.         newThread.Join()
  112.         MsgBox("Thread terminated")
  113.     End Sub
  114.  
  115. End Class
  116.