第一次编写多线程实现定时刷新listview

纯净水系统客户端因需要定时从数据库中读取服务端的新订单,所以要用多线程实现定时读取数据库并更新listview。
多线程的目的:让程序能更好更快的处理各种搜索信息,减轻计算机的负担。使用多线程。
定时的目的:让数据库更好的处理多个客户端的连接请求。减轻服务器的负担。必须定时连接数据库并断开。
由于对线程委托还不熟悉,不能用委托来实现跨线程调用控件,所以分两步实现,先把数据库数据读到dataset,然后再从dataset读到listview。
在主界面中加入timer控件,实现listview更新。
在主界面中加入一下代码:
Imports System.Threading
Imports System.Net
Imports System.Data
Imports System.Data.SqlClient

Public Class Admin
   ’ DBOpProvider.SWAllInfo为数据层
    Private objSWAllInfo As DBOpProvider.SWAllInfo
    Private objAllInfoDS As DataSet
    ‘KhdBusiness.AllInfo为业务层
    Private objAllInfo As KhdBusiness.AllInfo
    Private strAppTitle As String
    Private intIndex As Integer
    Dim t1 As Integer

    Public Sub a()
        Do

            objSWAllInfo = New DBOpProvider.SWAllInfo()
            ‘执行连接数据库的过程,定时把数据读入dataset
            objSWAllInfo.GetInfos()
            objSWAllInfo.GetInfo(
             ’定时30秒挂起线程
            Thread.Sleep(30 * 1000)
          Loop
    End Sub


    Private Sub Admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ’执行线程t1
        Dim t1 As New System.Threading.Thread(AddressOf a)
        t1.Start()
         ‘定时器30秒执行一次
        Timer1.Interval = 30000
        Timer1.Enabled = True
          End Sub

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim objListViewItem As ListViewItem

        ‘从dataset中读取数据到listview
        Using objAllInfo As New KhdBusiness.AllInfo()
            Try
                'Get a projects in a DataSet object
                objAllInfoDS = objAllInfo.GetAllInfos()
                'Clear previous list
                lvwProjects1.Items.Clear()
                For intIndex = 0 To objAllInfoDS.Tables("vKhdInfo").Rows.Count - 1
                    'Clear previous list
                    'lvwProjects1.Items.Clear()
                    'Create a new listview item
                    objListViewItem = New ListViewItem
                    'Add the data to the listview item
                    objListViewItem.Text = objAllInfoDS.Tables( _
                        "vKhdInfo").Rows(intIndex).Item("SsID")
                    objListViewItem.Tag = objAllInfoDS.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("DID")
                    'Add the sub items to the listview item
                    objListViewItem.SubItems.Add(objAllInfoDS.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("KID"))
                    objListViewItem.SubItems.Add(objAllInfoDS.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("khmc"))
                    objListViewItem.SubItems.Add(objAllInfoDS.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("hwm"))
                    objListViewItem.SubItems.Add(objAllInfoDS.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("ssts"))
                    objListViewItem.SubItems.Add(objAllInfoDS.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("ztxx"))
                    'Add the listview item to the listview control
                    lvwProjects1.Items.Add(objListViewItem)
                Next

            Catch ExceptionErr As Exception
                MessageBox.Show(ExceptionErr.Message, strAppTitle)
            End Try
        End Using

        Using objAllInfo2 As New KhdBusiness.AllInfo()
            Try
                'Get a projects in a DataSet object
                objAllInfoDS2 = objAllInfo2.GetAllInfo()
                'Clear previous list
                lvwProjects2.Items.Clear()
                For intIndex = 0 To objAllInfoDS2.Tables("vKhdInfo").Rows.Count - 1
                    'Clear previous list
                    'lvwProjects2.Items.Clear()
                    'Create a new listview item
                    objListViewItem = New ListViewItem
                    'Add the data to the listview item
                    objListViewItem.Text = objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("SsID")
                    objListViewItem.Tag = objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("DID")
                    'Add the sub items to the listview item
                    objListViewItem.SubItems.Add(objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("KID"))
                    objListViewItem.SubItems.Add(objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("khmc"))
                    objListViewItem.SubItems.Add(objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("hwm"))
                    objListViewItem.SubItems.Add(objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("ssts"))
                    objListViewItem.SubItems.Add(objAllInfoDS2.Tables( _
                    "vKhdInfo").Rows(intIndex).Item("ztxx"))
                    'Add the listview item to the listview control
                    lvwProjects2.Items.Add(objListViewItem)
                Next

            Catch ExceptionErr As Exception
                MessageBox.Show(ExceptionErr.Message, strAppTitle)
            End Try
        End Using
    End Sub
End Class
有时间再补充说明。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值