私は小さなデータベース管理ツールを開発しています。アプリケーションを起動してデータベースへの接続を試みたいのですが、データベースはネットワークドライブ上にあるため、時間がかかる場合があります。この間、アプリケーションはハングします。
以下のコードを使用して、別のスレッドでデータベースに接続するコードを実行して、アプリケーションをロードし、データベースを個別に接続できるようにします。
Public Class Incident_Form
'DELEGATE CLASSES
Delegate Sub UpdateTable()
'DELEGATE VARIABLES
Public UpdateTableVar As UpdateTable
Private Sub Incident_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
I_List.DataSource = Nothing
Display_Module.Maximise_Window()
Splash_Panel.BringToFront()
System.Windows.Forms.Application.DoEvents()
UpdateTableVar = New UpdateTable(AddressOf Me.UpdateTableHandler)
Me.Invoke(Me.UpdateTableVar)
End Sub
Public Sub UpdateTableHandler() 'Handles the background work for updating I_LIST
If (Me.InvokeRequired) Then
Me.Invoke(UpdateTableVar)
Else
Dim myCon = New OleDbConnection(My.Settings.Database_Connection_String)
myCon.Open()
Dim ds As DataSet = New DataSet
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String
sql = "SELECT [IN_REF], [Incident Name], [Date Created], [Created By] " & _
"FROM [Master_Record]"
adapter.SelectCommand = New OleDb.OleDbCommand(sql, myCon)
adapter.Fill(ds)
I_List.DataSource = ds.Tables(0)
I_List.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
I_List.Columns(0).HeaderText = "Reference"
I_List.Sort(I_List.Columns("Date Created"), System.ComponentModel.ListSortDirection.Descending)
myCon.Close()
End If
End Sub
上記の問題は、アプリケーションがフォームを正しくロードして表示しても、データベースの接続が完了するまでユーザーがフォームを操作できないことです。別のスレッドで実行されているようですが、続行する前にそのスレッドが終了するのを待っています。
私が見逃しているものがありますか、それともこれは正しいアプローチではありませんか?
更新1
助けてくれてありがとう、私はコードを次のように更新しました、そしてすべてが正しく働いています:)
Public Class Incident_Form
Dim ds As DataSet = New DataSet
Private Sub Incident_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
I_List.DataSource = Nothing
Display_Module.Maximise_Window()
Splash_Panel.BringToFront()
System.Windows.Forms.Application.DoEvents()
Try
BackgroundWorker1.RunWorkerAsync()
Catch ex As Exception
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.CancelAsync()
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
UpdateTableHandler()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
I_List.DataSource = ds.Tables(0)
I_List.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
I_List.Columns(0).HeaderText = "Reference"
I_List.Sort(I_List.Columns("Date Created"), System.ComponentModel.ListSortDirection.Descending)
End Sub
Public Sub UpdateTableHandler() 'Handles the background work for updating I_LIST
Dim myCon = New OleDbConnection(My.Settings.Database_Connection_String)
myCon.Open()
Dim adapter As New OleDb.OleDbDataAdapter
Dim sql As String
sql = "SELECT [IN_REF], [Incident Name], [Date Created], [Created By] " & _
"FROM [Master_Record]"
adapter.SelectCommand = New OleDb.OleDbCommand(sql, myCon)
adapter.Fill(ds)
myCon.Close()
End Sub
フォームに追加して、DoEvents()
通話をに置き換えますBackgroundWorker
。
Private Sub Incident_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
I_List.DataSource = Nothing
Display_Module.Maximise_Window()
Splash_Panel.BringToFront()
Try
BackgroundWorker1.RunWorkerAsync()
Catch ex As Exception
BackgroundWorker1p.WorkerSupportsCancellation = True
BackgroundWorker1.CancelAsync()
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As
System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
UpdateTableHandler()
End Sub
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加