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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.