Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim dtOr As New DataTable

VB
Dim trdOr As Threading.Thread

VB
Dim intMaxEtId As String

VB
Delegate Sub LoadOrGrid()

VB
Private Sub FillMyOrGrid()
       dgvCustOrders.DataSource = dtOr
       dgvCustOrders.Cursor = Cursors.Default
   End Sub

VB
Public Sub GetMaxOrId()
        Dim SQLcon As New SqlConnection
        SQLcon = Database.DataModule.GetSQLConnection()
        Try
            Dim sql As String = ("WITH CTEQuery(OrderID) as (SELECT TOP 5 OrderID from Acc_orders where OrderTypeID=5 and ActionTypeID=0 and CompanyID=1 ORDER BY OrderID) select MAX(OrderID) as LaOrderId from CTEQuery")
            Dim da As SqlDataAdapter = New SqlDataAdapter(sql, SQLcon)
            Dim dtOrId As New DataTable
            da.Fill(dtOrId)
            intMaxOrId = dtOrId.Rows(0).Item(0).ToString
        Catch ex As Exception
        End Try
    End Sub

VB
Public Sub UpdateOrInquiry()
        Dim SQLcon As New SqlConnection
        SQLcon = Database.DataModule.GetSQLConnection
        Try
            Dim SQL As String = String.Empty
            SQL = " update Acc_orders set ActionTypeID=1 where(OrderTypeID=5) and ActionTypeID=0 and CompanyID=1 and OrderID<=" & intMaxOrId
            SQLcon.Open()
            Dim command As New SqlCommand(SQL, SQLcon)
            command.ExecuteNonQuery()
        Catch ex As Exception
            'frmMain.lblmessage.Text = (ex.Message)
        End Try
        SQLcon.Close()
        SQLcon.Dispose()
    End Sub

VB
Public Sub GetOrSQLOrders()
       GetMaxOrId()
       If intMaxOrId Is Nothing OrElse IsDBNull(intMaxOrId) Then
           Exit Sub
       Else
           Dim SQLcon As New SqlConnection
           SQLcon = Database.DataModule.GetSQLConnection()
           Try
               Dim sql As String = "select top 5 OrderID,phone,Amount,CreateDate,CompanyID,(select UserName from Acc_Accounts where Acc_Accounts.AccountID = Acc_orders.AccountID) as UserName from Acc_orders where OrderTypeID=5 and ActionTypeID=0 and CompanyID=1 and OrderID<=" & intMaxOrId
               Dim daM As SqlDataAdapter = New SqlDataAdapter(sql, SQLcon)
               daM.Fill(dtOr)
               dgvCustOrders.DataSource = dtOr
               'dgvCustOrders.Invoke(New LoadOrGrid(AddressOf FillMyOrGrid))
               UpdateOrInquiry()
           Catch ex As Exception
           End Try
       End If
   End Sub

the code is working fine and stable if its in the main thread but in multithreading not stable
some times it make update for the record before it show it in the datagrideview
and unstable things
I call the code from timer
VB
Private Sub TBrain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBrain.Tick
        Select Case cboNom.Text
         LoadData()
        StartMoSend()
        IfRecharge()
    End Sub

VB
Private Sub LoadData()
                If lblMobRow.Text = 0 And txtMobiNorNumber.Text.Trim = "" Then
            GetOrSQLOrders()
        End If
        End Sub

thanks for your help

What I have tried:

I tryed all the code apove then I replace the
VB
dgvCustOrders.Invoke(New LoadOrGrid(AddressOf FillMyOrGrid))

with
VB
dgvCustOrders.DataSource = dtOr

its working and stable
Posted
Updated 5-Jun-17 7:26am
Comments
[no name] 4-Jun-17 9:17am    
>"I call the code from timer"
Timer does _not_ mean "multithreading"
GalaxyMan 5-Jun-17 8:21am    
this is the code of load data for mutithreding
Private Sub LoadData() 
   If lblMobRow.Text = 0 And txtMobiNorNumber.Text.Trim = "" Then                     dgvCustOrders.Cursor = Cursors.WaitCursor       
trdOr = New Threading.Thread(AddressOf GetOrSQLOrders)       
trdOr.IsBackground = True       
trdOr.Start()        
End If  
End Sub

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900