Results 1 to 2 of 2

Thread: [2005] Need advice (Multi threading)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    [2005] Need advice (Multi threading)

    I recently start learning about multi threading. I'd want to know is my code right or wrong.

    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Delegate Sub SetTextCallBack(ByVal val As Integer)
    4.     Dim t As Threading.Thread
    5.     Dim i As Integer = 0
    6.  
    7.     Private Sub SetText(ByVal val As Integer)
    8.         If Me.Label1.InvokeRequired Then
    9.             Dim d As New SetTextCallBack(AddressOf SetText)
    10.  
    11.             'I have used Me.Label1.begininvoke; Me.Label1.invoke; Me.begininvoke; Me.invoke
    12.             'The result is same
    13.             'The only difference is if doloop didn't sleep, begininvoke will stop responding
    14.             'Me.BeginInvoke(d, val)
    15.             Me.Invoke(d, val)
    16.         Else
    17.             Label1.Text = val
    18.         End If
    19.     End Sub
    20.  
    21.     Private Sub doLoop()
    22.         While i < 10000
    23.             If i > 9000 Then
    24.                 i = 0
    25.             End If
    26.  
    27.             If i Mod 100 = 0 Then
    28.                 Me.SetText(i)
    29.                 Threading.Thread.Sleep(100)
    30.             End If
    31.  
    32.             i += 1
    33.         End While
    34.  
    35.         MessageBox.Show("Finished")
    36.     End Sub
    37.  
    38.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    39.         'Me.doLoop()
    40.  
    41.         t = New Threading.Thread(New Threading.ThreadStart(AddressOf doLoop))
    42.         t.Start()
    43.     End Sub
    44.  
    45.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    46.         't.Abort() 'I've read some article that I should not use this.
    47.         'Me.Text = "Button2"
    48.         i = 10000
    49.         t.Join() 'Or use t=nothing
    50.     End Sub
    51. End Class

    My question is: Does this code right? It seems that doloop procedure must sleep for a while if I want my UI thread keep responding.
    Check my Blog at VB Corner,Component Crafts

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Need advice (Multi threading)

    The whole point of using multi-threading in most cases is that the UI will continue to respond while the background operation takes place. That said, if the background operation is extremely intensive then your UI may still not get much of a look in, especially on single-processor systems.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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