Thursday 16 February 2012

Thread in VB.net

This is a simple App for Demonstrate threading in VB.net
   Add
          1) ListBox 1,ListBox 2
          2)Button1(Start Thread)
                                       To our Form (Form1)
Now we are ready for coding ..... :)


  • Import "System.threading" to our thread App
        Import System.Threading
  • Create  Sample functions in app called "FillList1" and "FillList2"
        Private Sub FillList1()
       for i as integer=0 to 25
           ListBox1.Items.Add(i)
           Threading.Thread.Sleep(1000) 'To visible working of threads
       next i
     End Sub
     Private Sub FillList2()
       for j as integer=0 to 25
           ListBox2.Items.Add(j)
           Threading.Thread.Sleep(1000)
       next i
     End Sub
  • Now we call our sample threads from our start Button (Button1)
     Private Sub Button1_Click(ByVal sender As System.Object,_
                        ByVal e As System.EventArgs)Handles Button1.Click

        Dim thrd1 As Threading.Thread   'Declare thread1 named as thrd1

        Dim thrd2 As Threading.Thread   'Declare thread2 named as thrd2

        Control.CheckForIllegalCrossThreadCalls=False 'Avoid Illegal thread call

        thrd1=New Threading.Thread(AddressOf FillList1)  

        thrd2=New Threading.Thread(AddressOf FillList2)

        thrd1.Start()

        thrd2.Start()
End Sub

Now run your App.......:D
 

No comments:

Post a Comment