如何使用多线程在datagridview中获取数据 [英] how to use multi thread to get data in datagridview
本文介绍了如何使用多线程在datagridview中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用多线程从数据库中获取数据并将其放入datagridview而不冻结我的应用程序,如单个Form1中的
我有datagridview和按钮和文本框,当我点击按钮时时间所有数据将同时填入datagridview如果我在文本框中键入任何内容它将被键入并且不会冻结我的应用程序
请帮帮我.. 。
解决方案
Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Friend Class CorrectMultiThreadedForm
Inherits Form
'构造方法
Public Sub New()
InitializeComponent()
'创建一个文本框
_text.Location = New Point(10,10)
_text.Size = New Size(50,20)
Controls.Add(_text)
'创建一个按钮
bu tton.Text =Beep
button.Size =新尺寸(50,20)
button.Location =新点(80,10)
'注册Click事件处理程序
AddHandler button.Click,AddressOf OnClick
Controls.Add(button)
'缓存重复使用的委托
_enableControls = New BooleanCallback(AddressOf EnableControls)
End Sub
'按钮的Click事件调用的方法
Private Overloads Sub OnClick(ByVal sender As Object,ByVal args As EventArgs)
'获取一个int来自字符串
Dim _count As Int32 = 0
尝试
_count = Int32.Parse(_text.Text)
Catch e1 As FormatException
结束尝试
'计数到那个数字
EnableControls(False)
Dim回调As New WaitCallback(AddressOf Count)
ThreadPool.QueueUserWorkItem(callback,_count)
End Sub
'异步方法每秒发出一次蜂鸣声
私有子计数(ByVal param As Object)
Dim seconds As Int32 = CInt(Fix(param))
索引为Int32 = 0到秒 - 1
Interaction.Beep()
Thread.Sleep(1000)
下一个索引
调用(_enableControls,New Object(){True} )
End Sub
Private Sub EnableControls(ByVal enable As Boolean)
button.Enabled = enable
_text.Enabled = enable
End Sub
'委托类型和匹配字段
私有委托子BooleanBallback(ByVal enable As Boolean)
私有_enableControls As BooleanCallback
'一些私有字段用于引用控件
私有按钮As New Button()
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Private _text As New TextBox()
Private Sub InitializeComponent ()
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.SuspendLayout()
'
'RichTextBox1
'
Me.RichTextBox1.Location = New System。 Drawing.Point(12,111)
Me.RichTextBox1.Name =RichTextBox1
Me.RichTextBox1.Size = New System.Drawing.Size(260,139)
Me.RichTextBox1。 TabIndex = 0
Me.RichTextBox1.Text =
'
'CorrectMultiThreadedForm
'
Me.ClientSize = New System.Drawing.Size(284,262)
Me.Controls.Add(Me.RichTextBox1)
Me.Name =CorrectMultiThreadedForm
Me.ResumeLayout(False)
End Sub
End类
how do i use multi thread to get data from database and put it to in datagridview without freezing my Application like
in single Form1 i have datagridview and button and textbox when i click on button at that time all the data will fill in to datagridview at same time if i type anything in textbox it will be typed and not to freez my Application
Please Please Help me...
解决方案
Imports Microsoft.VisualBasic Imports System Imports System.Drawing Imports System.Threading Imports System.Windows.Forms Friend Class CorrectMultiThreadedForm Inherits Form ' Constructor method Public Sub New() InitializeComponent() ' Create a text box _text.Location = New Point(10, 10) _text.Size = New Size(50, 20) Controls.Add(_text) ' Create a button button.Text = "Beep" button.Size = New Size(50, 20) button.Location = New Point(80, 10) ' Register Click event handler AddHandler button.Click, AddressOf OnClick Controls.Add(button) ' Cache a delegate for repeated reuse _enableControls = New BooleanCallback(AddressOf EnableControls) End Sub ' Method called by the button's Click event Private Overloads Sub OnClick(ByVal sender As Object, ByVal args As EventArgs) ' Get an int from a string Dim _count As Int32 = 0 Try _count = Int32.Parse(_text.Text) Catch e1 As FormatException End Try ' Count to that number EnableControls(False) Dim callback As New WaitCallback(AddressOf Count) ThreadPool.QueueUserWorkItem(callback, _count) End Sub ' Async method beeps once per second Private Sub Count(ByVal param As Object) Dim seconds As Int32 = CInt(Fix(param)) For index As Int32 = 0 To seconds - 1 Interaction.Beep() Thread.Sleep(1000) Next index Invoke(_enableControls, New Object() {True}) End Sub Private Sub EnableControls(ByVal enable As Boolean) button.Enabled = enable _text.Enabled = enable End Sub ' A delegate type and matching field Private Delegate Sub BooleanCallback(ByVal enable As Boolean) Private _enableControls As BooleanCallback ' Some private fields by which to reference controls Private button As New Button() Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Private _text As New TextBox() Private Sub InitializeComponent() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.SuspendLayout() ' 'RichTextBox1 ' Me.RichTextBox1.Location = New System.Drawing.Point(12, 111) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(260, 139) Me.RichTextBox1.TabIndex = 0 Me.RichTextBox1.Text = "" ' 'CorrectMultiThreadedForm ' Me.ClientSize = New System.Drawing.Size(284, 262) Me.Controls.Add(Me.RichTextBox1) Me.Name = "CorrectMultiThreadedForm" Me.ResumeLayout(False) End Sub End Class
这篇关于如何使用多线程在datagridview中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文