Solved

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

Posted on 2004-08-11
7
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 86

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 86

Expert Comment

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

Idle_mind
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

724 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