Solved

vb.net; what is threading and do I need to use it

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

No sure what it does????
0
Comment
Question by:bman9111
7 Comments
 
LVL 2

Expert Comment

by:rattmman
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.

RC
0
 
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.

Regarsds,

Idle_Mind
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11774230
I think this link will be useful to you:
http://www.codeproject.com/vb/net/threadingvbnet.asp
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 85

Accepted Solution

by:
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.

Regards,

Idle_Mind

Imports System.Threading

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Button1
        '
        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"
        '
        'Button2
        '
        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"
        '
        'ProgressBar1
        '
        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
        '
        'Button3
        '
        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"
        '
        'Label1
        '
        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"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(418, 80)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.Name = "Form1"
        Me.Text = "Simple Threading Example"
        Me.ResumeLayout(False)

    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
        killThreads()
        ProgressBar1.Value = 50
        positiveProgess()
        negativeProgress()
    End Sub

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

        ProgressBar1.Value = 50
        p.Start()
        n.Start()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        killThreads()
    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
                ProgressBar1.Refresh()
                Label1.Text = "Positive"
                Label1.Refresh()
                Thread.Sleep(0)
            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
                ProgressBar1.Refresh()
                Label1.Text = "Negative"
                Label1.Refresh()
                Thread.Sleep(0)
            End If
        End While
    End Sub

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

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

End Class
0
 
LVL 8

Author Comment

by:bman9111
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??



0
 
LVL 85

Expert Comment

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

Idle_mind
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adobe Customization Wizard XI issues 26 167
How to split this in C++ 4 77
topping2 challenge 13 61
SUM 2 INTEGER ARRAYS INTO 1 10 62
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now