Solved; what is threading and do I need to use it

Posted on 2004-08-11
Last Modified: 2010-04-17
What is threading and do I need to know it???

No sure what it does????
Question by:bman9111

Expert Comment

ID: 11774072
Hi bman911,

Threading is a way to actually have your program to perform two tasks at once.

An example of threading would be if you have an accounting program and you want to log who makes certain transfers on certain accounts.  Rather then having it be a sequential program and have the log created after certain processes, you can create another thread from your main thread.

The second thread would be the log thread and now you will have two process happening at the same time.

So instead of having to have the log inside your main thread you could have a second thread that all it does is create the log and the main thread would be to run your program.

So you'll have two process running at the same time and the second thread will exist till you terminate the main program.

I say threading is nice to know, but sometimes a pain in the butt to use.  But I know that threading is a useful tool that I've used on a couple of occasions.

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11774141
A thread is simply a single path of execution through code executed in a serial manner.

Threading, or multithreading, is simply when multiple threads are executed at the same time in a parallel manner.

Why would you use threading?  Some possiblities are:

-- Placing a long calculation in another thread to keep the app responsive during the process.
-- Computing different calculations in parallel to save time.
-- Making autonomous "bots" move in parallel such as in simulations or games.
-- Handling requests from multiple users such as in web servers and socket based applications.
-- Displaying calculation intensive graphics such as graphing a mathematical formula.


LVL 55

Expert Comment

by:Jaime Olivares
ID: 11774230
I think this link will be useful to you:
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 85

Accepted Solution

Mike Tomlinson earned 25 total points
ID: 11775190
Try out this simple app that demonstrates threading.  The same two subroutines are executed directly after each other but they have very different results in a single thread versus parallel threads.



Imports System.Threading

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.Button3 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button1.Location = New System.Drawing.Point(8, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(120, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Single Thread"
        Me.Button2.Location = New System.Drawing.Point(136, 8)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(120, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Multiple Threads"
        Me.ProgressBar1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ProgressBar1.Location = New System.Drawing.Point(8, 64)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(402, 10)
        Me.ProgressBar1.TabIndex = 3
        Me.ProgressBar1.Value = 50
        Me.Button3.Location = New System.Drawing.Point(264, 8)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(120, 24)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "Abort Threads"
        Me.Label1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Label1.BackColor = System.Drawing.Color.Black
        Me.Label1.ForeColor = System.Drawing.Color.White
        Me.Label1.Location = New System.Drawing.Point(8, 40)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(402, 16)
        Me.Label1.TabIndex = 5
        Me.Label1.Text = "Label1"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(418, 80)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.Name = "Form1"
        Me.Text = "Simple Threading Example"

    End Sub

#End Region

    Private p As Thread
    Private n As Thread

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Value = 50
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        p = New Thread(AddressOf positiveProgess)
        n = New Thread(AddressOf negativeProgress)

        ProgressBar1.Value = 50
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    End Sub

    Private Sub positiveProgess()
        While ProgressBar1.Value < ProgressBar1.Maximum
            If DateTime.Now.Second Mod 2 = 0 Then
                Label1.BackColor = Color.Green
                ProgressBar1.Value = ProgressBar1.Value + 1
                Label1.Text = "Positive"
            End If
        End While
    End Sub

    Private Sub negativeProgress()
        While ProgressBar1.Value > ProgressBar1.Minimum
            If DateTime.Now.Second Mod 2 <> 0 Then
                Label1.BackColor = Color.Red
                ProgressBar1.Value = ProgressBar1.Value - 1
                Label1.Text = "Negative"
            End If
        End While
    End Sub

    Private Sub killThreads()
        If Not (p Is Nothing) Then
            p = Nothing
        End If
        If Not (n Is Nothing) Then
            n = Nothing
        End If
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    End Sub

End Class

Author Comment

ID: 11798311
so basically if I wanted to have the program do a calculation and open another form well it is doing it this is what I would do??

LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11798343
It is one way of accomplishing it yes.


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question