[VB.Net] How to make multi threading w/ cross calls?

Discussion in 'Mixed Languages' started by jcgo16, Mar 1, 2013.

  1. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    #1 jcgo16, Mar 1, 2013
    Last edited by a moderator: Apr 20, 2017
    [VB.Net] How to make multi threading w/ cross calls?


    Lets say i have to wait 500ms to run this code again but i wont use Sleep(500) but instead it will make another thread for that code here

    so there we're 2 threads running this code


    Code:
    
          For counter = 0 To DataGridView1.Rows.Count - 1
                    Select Case DataGridView1.Rows(counter).Cells(8).Value
                        Case 1
                            DataGridView1.Rows(counter).Cells(10).Value = Integer.Parse(DataGridView1.Rows(counter).Cells(10).Value) + 1
                            Dim x As New TimeSpan(0, 0, DataGridView1.Rows(counter).Cells(10).Value)
                            DataGridView1.Rows(counter).Cells(4).Value = x.ToString
                            DataGridView1.Rows(counter).Cells(5).Value = Format((Decimal.Parse(ini.ReadString("per15minbasis")) / 60) * DataGridView1.Rows(counter).Cells(10).Value, "0.00") & " Pesos"
                            If DataGridView1.Rows(counter).Cells(10).Value >= DataGridView1.Rows(counter).Cells(9).Value + Integer.Parse(ini.ReadString("timedelay")) Then
                                DataGridView1.Rows(counter).Cells(10).Value = 0
                                DataGridView1.Rows(counter).Cells(9).Value = 0
                                DataGridView1.Rows(counter).Cells(8).Value = 0
                                Threading.Thread.Sleep(100)
                                Send("state:" & ini.ReadString("timekill"), DataGridView1.Rows(counter).Cells(11).Value)
                            End If
                    End Select
                Next
    
    
     
  2. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    418
    60
    #2 PAYMYRENT, Mar 2, 2013
    Last edited by a moderator: Apr 20, 2017
    just an example from one of my programs

    Code:
    Public Delegate Sub UpdateMenuCallback(ByVal status As Boolean, ByVal size As ULong, ByVal Inum As ULong)
    
    Me.Invoke(New UpdateMenuCallback(AddressOf Me.UpdateMenu), New Object() {False, SH.i64Size, SH.i64NumItems})
    
        Public Sub UpdateMenu(ByVal status As Boolean, ByVal size As ULong, ByVal Inum As ULong)
            Dim ch As MenuItem = notificationMenu.MenuItems.Item(3).MenuItems.Item(1)
            ch.Text = "Empty Recycle Bin: Items:=" & Inum & ". Size:=" & SetBytes(size)
            ch.Enabled = status
        End Sub
    
    Ok the Me.Invoke is the code you place in the second thread to it tells the application that you need to run the sub UpdateMenu with the parameters the UpdateMenu will the update your UI with the object values you need to display
     
  3. jcgo16

    jcgo16 MDL Junior Member

    Sep 16, 2010
    74
    2
    0
    hope this will work <3

    thank you for the reply
     
  4. PAYMYRENT

    PAYMYRENT MDL Developer

    Jul 28, 2009
    1,462
    418
    60
    Did it work? I mean I could give you an example class to look at. :p