.net 线程源码。各位高手帮我一下行吗?分数我还是有的(100分)

  • 主题发起人 主题发起人 hzjone
  • 开始时间 开始时间
H

hzjone

Unregistered / Unconfirmed
GUEST, unregistred user!
Imports System.Data.OleDb
Imports System.Threading.Thread
Public Class frmmain
Inherits System.Windows.Forms.Form
Private Conn As OleDbConnection
Private Ds As DataSet
Private cm As OleDbCommand
Private Da As OleDbDataAdapter
Public dt As DataTable
Private thread As Threading.Thread
Private WithEvents objnormal As Clsdata
Private WithEvents objthread As Clsdata
Private mythread As Threading.Thread
Private mydataset As DataTable
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing then
If Not (components Is Nothing) then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents btnnormal As System.Windows.Forms.Button
Friend WithEvents Btnthread As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnnormal = New System.Windows.Forms.Button
Me.Btnthread = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.DataGrid1 = New System.Windows.Forms.DataGrid
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).begin
Init()
Me.SuspendLayout()
'
'btnnormal
'
Me.btnnormal.Location = New System.Drawing.Point(208, 210)
Me.btnnormal.Name = "btnnormal"
Me.btnnormal.Size = New System.Drawing.Size(192, 23)
Me.btnnormal.TabIndex = 1
Me.btnnormal.Text = "使用一般方法(&amp;N)"
'
'Btnthread
'
Me.Btnthread.Location = New System.Drawing.Point(208, 240)
Me.Btnthread.Name = "Btnthread"
Me.Btnthread.Size = New System.Drawing.Size(192, 23)
Me.Btnthread.TabIndex = 2
Me.Btnthread.Text = "使用多线程(&amp;T)"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(440, 224)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(152, 21)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(656, 200)
Me.DataGrid1.TabIndex = 4
'
'frmmain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(648, 269)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Btnthread)
Me.Controls.Add(Me.btnnormal)
Me.Name = "frmmain"
Me.Text = "数据库"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
objthread = New Clsdata
objnormal = New Clsdata
Conn = New OleDbConnection
Da = New OleDbDataAdapter
dt = New DataTable
cm = New OleDbCommand
Ds = New DataSet
End Sub

Private Sub btnnormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnormal.Click
objnormal.readconn = Conn
objnormal.readda = Da
objnormal.readcm = cm
objnormal.readds = Ds
objnormal.readdt = dt
objnormal.read()
DataGrid1.DataSource = objnormal.dt
End Sub

Private Sub Btnthread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnthread.Click
objthread.readconn = Conn
objthread.readda = Da
objthread.readcm = cm
objthread.readds = Ds
objthread.readdt = dt
mythread = New Threading.Thread(AddressOf objthread.read)
mythread.Start()
End Sub
Private Sub btnthread_getdata(ByVal value As DataTable) Handles objthread.getdata
Dim lsmessage As String
Try
MsgBox(CStr(value.Rows.Count), MsgBoxStyle.OKOnly, "提示")
DataGrid1.DataSource = value//这边出错提示 "在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。"我纳闷的是,DT是在主线程创建的啊。怎么会有这样的提示呢?
Catch ex As Exception
lsmessage = ex.Message()
End Try
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
End Sub
End Class
底下这个类是为了线程用的。
Imports System.Data.OleDb
Public Class Clsdata
Private Connstr As String
Private Conn As OleDbConnection
Private Ds As DataSet
Private cm As OleDbCommand
Private Da As OleDbDataAdapter
Public dt As DataTable
Private thread As Threading.Thread
'Public datagrid1 As DataGrid
Public Event getdata(ByVal dt As DataTable)
Public Property readconn()
Get
Return Conn
End Get
Set(ByVal Value)
Conn = Value
End Set
End Property
Public Property readds()
Get
Return Ds
End Get
Set(ByVal Value)
Ds = Value
End Set
End Property
Public Property readda()
Get
Return Da
End Get
Set(ByVal Value)
Da = Value
End Set
End Property
Public Property readcm()
Get
Return cm
End Get
Set(ByVal Value)
cm = Value
End Set
End Property
Public Property readdt()
Get
Return dt
End Get
Set(ByVal Value)
dt = Value
End Set
End Property
Public Sub read()
Connstr = "Provider=SQLOLEDB.1;Password=succeed;Persist Security Info=True;User ID=sa;Initial Catalog=a_kg;Data Source=jdt-hzj"
Conn.Close()
Conn.ConnectionString = Connstr
Conn.Open()
cm.Connection = Conn
cm.CommandText = "select top 10 * from a_day"
Da.SelectCommand = cm
Da.Fill(Ds)
dt = Ds.Tables(0)
MsgBox(CStr(dt.Rows.Count), MsgBoxStyle.OKOnly, "提示")
RaiseEvent getdata(dt)
End Sub
End Class
 
其实不难。怎么会没人回答啊。我搞.net才几天。不熟啊。如果有经验的话。应该很快可以做到才对啊。
 
Private Sub btnthread_getdata(ByVal value As DataTable) Handles
objthread.getdata
改成
Private Sub btnthread_getdata(ByRef value As DataTable) Handles
objthread.getdata
试试。
 
没啥用。再给我一个办法行吗?
 
来了……
 
后退
顶部