Results 1 to 6 of 6

Thread: Threads how many and how to stop?

  1. #1

    Thread Starter
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Threads how many and how to stop?

    Using System.Threading

    I want to be able to start threads arbitrarily by my application. When I do, how can I tell how many threads are running?

    Also, how can I kill a thread?

    Finally, how does a thread kill itself?

    VB Code:
    1. Imports System.Threading
    2.  
    3. Public Class Form1
    4.     Inherits System.Windows.Forms.Form
    5.     Dim LFile As String
    6.     Dim LFileSize As Int32 = 32767
    7.     Dim LineArray() As String
    8.     Dim NameArray() As String
    9.  
    10.     Public nThreads As Int16 = 0
    11.  
    12. #Region " Windows Form Designer generated code "
    13.  
    14.     Public Sub New()
    15.         MyBase.New()
    16.  
    17.         'This call is required by the Windows Form Designer.
    18.         InitializeComponent()
    19.  
    20.         'Add any initialization after the InitializeComponent() call
    21.  
    22.     End Sub
    23.  
    24.     'Form overrides dispose to clean up the component list.
    25.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    26.         If disposing Then
    27.             If Not (components Is Nothing) Then
    28.                 components.Dispose()
    29.             End If
    30.         End If
    31.         MyBase.Dispose(disposing)
    32.     End Sub
    33.  
    34.     'Required by the Windows Form Designer
    35.     Private components As System.ComponentModel.IContainer
    36.  
    37.     'NOTE: The following procedure is required by the Windows Form Designer
    38.     'It can be modified using the Windows Form Designer.  
    39.     'Do not modify it using the code editor.
    40.     Friend WithEvents eLabel As System.Windows.Forms.Label
    41.     Friend WithEvents Timer1 As System.Windows.Forms.Timer
    42.     Friend WithEvents txtIP As System.Windows.Forms.TextBox
    43.     Friend WithEvents cmbLocale As System.Windows.Forms.ComboBox
    44.     Friend WithEvents Label1 As System.Windows.Forms.Label
    45.     Friend WithEvents Label2 As System.Windows.Forms.Label
    46.     Friend WithEvents Button1 As System.Windows.Forms.Button
    47.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    48.         Me.components = New System.ComponentModel.Container()
    49.         Me.eLabel = New System.Windows.Forms.Label()
    50.         Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
    51.         Me.txtIP = New System.Windows.Forms.TextBox()
    52.         Me.cmbLocale = New System.Windows.Forms.ComboBox()
    53.         Me.Label1 = New System.Windows.Forms.Label()
    54.         Me.Label2 = New System.Windows.Forms.Label()
    55.         Me.Button1 = New System.Windows.Forms.Button()
    56.         Me.SuspendLayout()
    57.         '
    58.         'eLabel
    59.         '
    60.         Me.eLabel.Location = New System.Drawing.Point(0, 64)
    61.         Me.eLabel.Name = "eLabel"
    62.         Me.eLabel.Size = New System.Drawing.Size(336, 168)
    63.         Me.eLabel.TabIndex = 0
    64.         '
    65.         'Timer1
    66.         '
    67.         Me.Timer1.Interval = 500
    68.         '
    69.         'txtIP
    70.         '
    71.         Me.txtIP.Location = New System.Drawing.Point(96, 8)
    72.         Me.txtIP.Name = "txtIP"
    73.         Me.txtIP.Size = New System.Drawing.Size(120, 20)
    74.         Me.txtIP.TabIndex = 1
    75.         '
    76.         'cmbLocale
    77.         '
    78.         Me.cmbLocale.Location = New System.Drawing.Point(96, 32)
    79.         Me.cmbLocale.Name = "cmbLocale"
    80.         Me.cmbLocale.Size = New System.Drawing.Size(232, 21)
    81.         Me.cmbLocale.TabIndex = 2
    82.         '
    83.         'Label1
    84.         '
    85.         Me.Label1.Location = New System.Drawing.Point(8, 8)
    86.         Me.Label1.Name = "Label1"
    87.         Me.Label1.Size = New System.Drawing.Size(80, 23)
    88.         Me.Label1.TabIndex = 3
    89.         Me.Label1.Text = "IP Address"
    90.         '
    91.         'Label2
    92.         '
    93.         Me.Label2.Location = New System.Drawing.Point(8, 32)
    94.         Me.Label2.Name = "Label2"
    95.         Me.Label2.Size = New System.Drawing.Size(80, 16)
    96.         Me.Label2.TabIndex = 4
    97.         Me.Label2.Text = "Temp Locale"
    98.         '
    99.         'Button1
    100.         '
    101.         Me.Button1.Location = New System.Drawing.Point(224, 8)
    102.         Me.Button1.Name = "Button1"
    103.         Me.Button1.Size = New System.Drawing.Size(104, 24)
    104.         Me.Button1.TabIndex = 5
    105.         Me.Button1.Text = "Start | Stop"
    106.         '
    107.         'Form1
    108.         '
    109.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    110.         Me.ClientSize = New System.Drawing.Size(336, 230)
    111.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.Label2, Me.Label1, Me.cmbLocale, Me.txtIP, Me.eLabel})
    112.         Me.Name = "Form1"
    113.         Me.Text = "Downloader"
    114.         Me.ResumeLayout(False)
    115.  
    116.     End Sub
    117.  
    118. #End Region
    119.     Public Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
    120.        Alias "DeleteUrlCacheEntryA" _
    121.       (ByVal lpszUrlName As String) As Long
    122.  
    123.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    124.         Dim theNet As New System.Net.WebClient()
    125.         Dim theDataBuffer As Byte()
    126.         Dim DLFile As String
    127.         Dim x As Int16
    128.  
    129.         For x = 0 To 25
    130.             cmbLocale.Items.Add(Chr(x + 65) & ":\")
    131.         Next
    132.         theDataBuffer = theNet.DownloadData("http://" & txtIP.Text)
    133.         DLFile = System.Text.Encoding.ASCII.GetString(theDataBuffer)
    134.         LineArray = Split(DLFile, (Chr(10)))
    135.         NameArray = Split(DLFile, (Chr(10)))
    136.         For x = 0 To UBound(LineArray)
    137.             If (Len(Mid(LineArray(x), InStrRev(LineArray(x), "<td>") + 4)) < (Len(LineArray(x)) - 5)) AndAlso (InStrRev(NameArray(x), "</a>") > 0) Then
    138.                 LineArray(x) = Mid(LineArray(x), InStrRev(LineArray(x), "<td>") + 4)
    139.                 NameArray(x) = Mid(NameArray(x), InStrRev(NameArray(x), """>") + 2)
    140.                 NameArray(x) = Mid(NameArray(x), 1, InStrRev(NameArray(x), "</a>") - 1)
    141.                 If (LFileSize > Val(LineArray(x))) AndAlso (Val(LineArray(x)) > 1000) Then
    142.                     LFileSize = LineArray(x)
    143.                     LFile = NameArray(x)
    144.                 End If
    145.             Else
    146.                 LineArray(x) = ""
    147.                 NameArray(x) = ""
    148.             End If
    149.         Next
    150.     End Sub
    151.  
    152.     Private Sub Form1_Unload(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    153.  
    154.     End Sub
    155.  
    156.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    157.         Try
    158.             Dim dlthd As DLThread = New DLThread(eLabel, cmbLocale, NameArray, txtIP)
    159.             Dim thd As Thread = New Thread(AddressOf dlthd.DoWork)
    160.             thd.Priority = ThreadPriority.BelowNormal
    161.             thd.Start()
    162.         Catch
    163.         End Try
    164.     End Sub
    165.  
    166.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    167.         Timer1.Enabled = Not Timer1.Enabled
    168.     End Sub
    169. End Class
    170.  
    171. Public Class DLThread
    172.     Public Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
    173.        Alias "DeleteUrlCacheEntryA" _
    174.       (ByVal lpszUrlName As String) As Long
    175.  
    176.     Private ginLabel As Label
    177.     Private gcmbLocale As ComboBox
    178.     Private gNameArray As System.Array
    179.     Private gtxtIP As TextBox
    180.  
    181.     Public Sub New(ByRef inLabel As Label, ByVal cmbLocale As ComboBox, ByVal NameArray As System.Array, ByVal txtIP As TextBox)
    182.         ginLabel = inLabel
    183.         gcmbLocale = cmbLocale
    184.         gNameArray = NameArray
    185.         gtxtIP = txtIP
    186.     End Sub
    187.  
    188.     Public Sub DoWork()
    189.  
    190.         Try
    191.             ginLabel.Text = ""
    192.             Randomize()
    193.             Dim theNet As New System.Net.WebClient()
    194.             Dim theFile As String = gcmbLocale.SelectedItem & "file." & Rnd() * 32767
    195.             Dim DLEntry As String = "http://" & gtxtIP.Text & "/" & gNameArray(Int(Rnd() * UBound(gNameArray)))
    196.             DeleteUrlCacheEntry(DLEntry)
    197.  
    198.                         theNet.DownloadFile(DLEntry, theFile)
    199.             Kill(theFile)
    200.         Catch ee As System.Net.WebException
    201.             ginLabel.Text = ee.ToString()
    202.         End Try
    203.     End Sub
    204. End Class
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can use Abort method to stop a thread.

    You can check whether its running or not with the ThreadState property.

    The easiest way to track them would to make a collection or array of the threads you've started until then end.

  3. #3
    Lively Member
    Join Date
    Jan 2002
    Location
    Malaysia
    Posts
    64
    threads kill themselves when they've finished running unless they're in an infinite loop or something.

    To call the abort function for a thread you'll need to have a permanent reference to your thread (easiest way is to keep the threads in a collection)

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You could create your own class that does thread management. It could have methods like Add, Abort, Count to get the state of your threads, as well as a function that runs every 3 seconds (or so) to get the status of the threads and update the count property.

  5. #5
    Lively Member
    Join Date
    Jan 2002
    Location
    Malaysia
    Posts
    64
    i think by using a (shared) collection to keep track of threads in effect you ARE creating a thread management class since you are adding and aborting threads.

    Anyway, just a suggestion to reduce your future headache when dealing with threads.

    Make good use of locks for your shared resources.

    The counter for the number of threads running is a good candidate. So is the collection of threads itself.

  6. #6
    Lively Member
    Join Date
    Aug 1999
    Location
    Amsterdam
    Posts
    117
    The counter for the number of threads running is a good candidate. So is the collection of threads itself.
    The counter will propably by of type int (32 bit), on a 32 bit OS it's not necessary to lock/sync the counter (of type int) because changing the value will be done within 1 CPU cycle.


    Looking at all the suggestions you'd probably want to take a look at using the CLR threadpool, and not managing threads yourself (if possible ofcourse) or building your own threadpool.

    An easy and good way is using async delegates, which make use of the CLR threadpool.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width