使用线程(节省时间)和委托(跨线程调用控件)的范例 1

 
 Private Sub tb_clientNumber_Leave(sender As Object, e As EventArgs) Handles tb_clientNumber.Leave 
       'getResultsCount_thread()
        '  上面是直接获取这个客户号的交易数量,没有用线程,可以运行,但是不能节省时间
        '  下面是用线程的方式
        Dim t As Threading.Thread = New Threading.Thread(AddressOf getResultsCount_thread)
        t.SetApartmentState(Threading.ApartmentState.STA)
        t.IsBackground = True ' 在后台运行(随宿主关闭而关闭)
        t.Start()
 End Sub
 

 Private Sub getResultsCount_thread()   '  注意,耗时的操作要放在 线程里,不能放在具体方法里,否则无法节省时间
 
        Dim strSelect = "select count(*) "
        If isTesting() Then
            strSelect &= strFromUAT
        Else
            strSelect &= strFromProd
        End If
        strSelect &= " where cst_id = '" & String.Format("{0:000000}", CInt(tb_clientNumber.Text.Trim)) & "' and acc_ind = '03'"
        Dim strCount03 = getDataString(strSelect)   '  这个查询数据库的操作是很耗时的

        '  注意,耗时的操作要放在 线程里,不能放在委托调用的具体方法里,否则无法节省时间
         Me.Invoke(New _delegate(AddressOf UpdateText), strCount03)    '  调用委托
    End Sub
 
 
    Public Delegate Sub _delegate(ByVal strShow As String)    '定义委托
 
 
    Public Sub UpdateText(ByVal _strInput As String)   ' 具体方法,  更新界面上某个控件
          ' Threading.Thread.Sleep(1000)   '  注意,耗时的操作不能放在被主线程委托调用的具体方法里,否则无法节省时间
          lbl_count03All.Text = _strInput
          lbl_count03All.Visible = True
    End Sub
 
posted @ 2019-12-18 17:28  gaoleionline  阅读(193)  评论(0)    收藏  举报