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的区别所在