0

I write code using VB.net like:

Dim a As Thread
    Dim i As Integer
   [B] Private Sub Form1_Load[/B]        
              For i = 1 To 5
            a = New Thread(New ThreadStart(AddressOf coba))
            a.Start()
        Next
   [B] End Sub[/B]

    [B]Sub coba()[/B]       
        MsgBox(i)
        a.Sleep(500)
    [B]End Sub[/B]

why result different sometimes:
display MessageBox with text 1 ,3,3,5 and 6
every executed,the result different

i want display result 1,2,3,4,5
what's wrong with my coding?
plz help me
thanx

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Teme64
0

why result different sometimes:
display MessageBox with text 1 ,3,3,5 and 6
every executed,the result different

Threads are executed asynchronously. Different results come from, which order OS yields time for the threads to be executed.

i want display result 1,2,3,4,5

How about

For i = 1 To 5
  MsgBox(i)
Next

that's a bullet-proof solution :D

You could try

Private Sub Form1_Load()
  Dim i As Integer
  For i = 1 To 5
    Dim MyThread As Threading.Thread = New Threading.Thread(AddressOf coba)
    MyThread.Start(i)
    Threading.Thread.Sleep(500)
  Next
End Sub

Private Sub coba(ByVal i As Object)
  MsgBox(i)
End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.