惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
IT之家
IT之家
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
V2EX
Last Week in AI
Last Week in AI
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
F
Fortinet All Blogs
I
InfoQ
宝玉的分享
宝玉的分享
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog

博客园 - 下载软件

我也想.... 祝愿 笑死 有意思... 日日本 哈哈 一则笑话 好诗 好久米有写代码了! ... 经典 哈哈 经典!! 有点忙。。有些时间没有来了。。 搞笑 这几天真忙啊! 好累哈。。 Q宠又生宝宝了。。这是第5个了。。哈哈。。
我做的一个例子。。保存下来。。以后可以回看一下。。
下载软件 · 2006-08-31 · via 博客园 - 下载软件

    Private o_ClassBaseData As New ClassBaseData
    Private b_Flag As Boolean = False
    Private o_Thread As Threading.Thread = Nothing

    Private Sub frmMoney_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BindDataThread()
        Me.Reset()
    End Sub

    '//新建线程绑到数据到DataGridView
    Private Sub BindDataThread()
        Me.MoneyDataGridView.Cursor = Cursors.WaitCursor
        Me.BaseDataStatus.Items.Item("Info").Text = "数据载入中..."

        Me.o_Thread = New Thread(New ThreadStart(AddressOf Me.LoadData))
        Me.o_Thread.IsBackground = True
        Me.o_Thread.Start()
    End Sub

    '//初始化
    Private Sub LoadData()
        Me.BindData(Me.GetDataSet)
    End Sub


    '//数据载入委托
    Private Delegate Sub BindDataGridView(ByVal o_Ds As DataSet)
    '//通过Invoke安全调用另一线程载入数据
    Private Sub BindData(ByVal o_Ds As DataSet)
        If Me.MoneyDataGridView.InvokeRequired Then
            Dim o_Delegate As New BindDataGridView(AddressOf BindData)
            Me.Invoke(o_Delegate, o_Ds)
        Else
            Me.MoneyDataGridView.DataSource = o_Ds
            Me.MoneyDataGridView.DataMember = "货币"
            Me.MoneyDataGridView.Columns.Item(0).Visible = False
            If Me.MoneyDataGridView.Rows.Count > 0 Then Me.MoneyDataGridView.Rows(0).Selected = False
        End If

        Me.BeginInvoke(New DoneLoadData(AddressOf Me.DoneData))
    End Sub

    '//数据载入完成委托
    Private Delegate Sub DoneLoadData()
    '//完成数据载入
    Private Sub DoneData()
        Me.BaseDataStatus.Items.Item("Info").Text = "就绪"
        Me.MoneyDataGridView.Cursor = Cursors.Default
    End Sub


    '//获取表内容,返回DataSet
    Private Function GetDataSet() As DataSet
        Dim o_Ds As New DataSet
        Dim o_Money As DataTableMapping

        If Me.o_ClassBaseData.b_DataType Then
            '//Access Query
            Dim o_Conn As New OleDbConnection
            Dim o_Ad As New OleDbDataAdapter
            Dim o_Cmd As New OleDbCommand

            Try
                o_Conn.ConnectionString = Me.o_ClassBaseData.s_ConnectionString
                o_Conn.Open()

                o_Cmd.CommandText = "Select * From asia_Money Order By ID"
                o_Cmd.CommandType = CommandType.Text
                o_Cmd.Connection = o_Conn

                o_Ad.SelectCommand = o_Cmd
                '//映射
                o_Money = o_Ad.TableMappings.Add("asia_Money", "货币")
                o_Money.ColumnMappings.Add("ID", "索引号")
                o_Money.ColumnMappings.Add("s_No", "货币编号")
                o_Money.ColumnMappings.Add("s_Name", "货币名称")
                o_Money.ColumnMappings.Add("i_Rate", "兑换率")
                o_Money.ColumnMappings.Add("d_Date", "添加日期")
                o_Money.ColumnMappings.Add("b_Lock", "冻结")

                o_Ad.Fill(o_Ds, "asia_Money")

                Return o_Ds
            Catch ex As OleDbException
                MessageBox.Show("数据表不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                '//End
            Catch ex As Exception
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                '//End
            Finally
                o_Cmd.Dispose() : o_Cmd = Nothing
                o_Ad.Dispose() : o_Ad = Nothing
                o_Conn.Close() : o_Conn.Dispose() : o_Conn = Nothing
            End Try

        Else
            '//SqlServer Query
            Dim o_Conn As New SqlConnection
            Dim o_Ad As New SqlDataAdapter
            Dim o_Cmd As New SqlCommand

            Try
                o_Conn.ConnectionString = Me.o_ClassBaseData.s_ConnectionString
                o_Conn.Open()

                o_Cmd.CommandText = "Select * From asia_Money Order By ID"
                o_Cmd.CommandType = CommandType.Text
                o_Cmd.Connection = o_Conn

                o_Ad.SelectCommand = o_Cmd
                '//映射
                o_Money = o_Ad.TableMappings.Add("asia_Money", "货币")
                o_Money.ColumnMappings.Add("ID", "索引号")
                o_Money.ColumnMappings.Add("s_No", "货币编号")
                o_Money.ColumnMappings.Add("s_Name", "货币名称")
                o_Money.ColumnMappings.Add("i_Rate", "兑换率")
                o_Money.ColumnMappings.Add("d_Date", "添加日期")
                o_Money.ColumnMappings.Add("b_Lock", "冻结")

                o_Ad.Fill(o_Ds, "asia_Money")
                Return o_Ds
            Catch ex As SqlException
                MessageBox.Show("数据表不存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                '//End
            Catch ex As Exception
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
                '//End
            Finally
                o_Cmd.Dispose() : o_Cmd = Nothing
                o_Ad.Dispose() : o_Ad = Nothing
                o_Conn.Close() : o_Conn.Dispose() : o_Conn = Nothing
            End Try
        End If

    End Function

上面体现了Invoke与BeginInvoke的区别所在