[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-08-11
7
Medium Priority
?
304 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
Simple Linear Regression

656 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