我在表单上有一个按钮,它基本上从我的数据库中获取数据,如下所示。
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs)
Handles ToolStripButton1.Click
BackgroundWorker1.RunWorkerAsync()
Loading_Screen.ShowDialog()
End Sub
在我的代码(从数据库获取数据)在backgroundworker中运行后调用加载屏幕。 当backgroundworker完成后,我将关闭加载表单,如下所示
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
Handles BackgroundWorker1.RunWorkerCompleted
DataGridView1.DataSource = bSource
SDA.Update(dataTable)
ToolStripLabel1.Text = "RESULT : " + DataGridView1.RowCount.ToString
Loading_Screen.Close()
End Sub
这仅在我第一次启动应用程序时有效。每当我再次单击该按钮时,加载表单将不再显示,但代码仍然正常运行。有什么想法吗?
加载表单根本没有代码,每次加载时都只有一个正在运行的进度条。
我做了什么,但没有运气:
更新(我会在此处不断更新我的进度)
许多人要求创建一个新实例,这就是我已经做过的事情
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
BackgroundWorker1.RunWorkerAsync()
ldScreen = New Loading_Screen()
ldScreen.ShowDialog()
Me.Refresh()
End Sub
然后,在运行完成后,
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
DataGridView1.DataSource = bSource
SDA.Update(dataTable)
ToolStripLabel1.Text = "RESULT : " + DataGridView1.RowCount.ToString
ldScreen.Close()
BackgroundWorker1.Dispose()
End Sub
在我的加载表单中,代码只是这个
Private Sub Loading_Screen_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Me.Dispose()
End Sub
Private Sub Loading_Screen_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Refresh()
End Sub
更新2
通过剥离我的大部分代码并将系统线程休眠放入后台工作,确实可以正常显示加载表单。所以这是我在backgroundworkerdowork中关于实际发生的事情的代码。
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Connect2Database()
Try
sqlCommand.CommandText = "Select * from kup_table" 'Load full database into gridview
SDA.SelectCommand = sqlCommand
SDA.Fill(dataTable)
bSource.DataSource = dataTable
mySqlConn.Close()
Catch ex As MySqlException
MsgBox(ex.ToString)
If mySqlConn.State = ConnectionState.Open Then
mySqlConn.Close()
End If
Finally
mySqlConn.Dispose()
End Try
'System.Threading.Thread.Sleep(2000)
End Sub
这是Connect2Database功能代码
Private Sub Connect2Database()
sqlCommand = New MySqlCommand
dataTable = New DataTable
SDA = New MySqlDataAdapter
bSource = New BindingSource
Try
dataTable.Clear()
mySqlConn.ConnectionString = connString
sqlCommand.Connection = mySqlConn
mySqlConn.Open()
Catch ex As MySqlException
MsgBox(ex.ToString)
If mySqlConn.State = ConnectionState.Open Then
mySqlConn.Close()
End If
End Try
End Sub
更新3
我注意到的是,当我的System.Threading.Thread.Sleep(2000)
未被评论时,加载屏幕将正常显示。但如果我将其更改为System.Threading.Thread.Sleep(1)
,则不会显示加载屏幕。为什么会这样?代码在第一次运行后超速运行?
答案 0 :(得分:0)
这是因为调用Close()
会处理表单。因此,您每次都必须创建表单的新实例,或者需要使用Hide()
。从你的问题来看,我认为你想要前者。
答案 1 :(得分:-1)
您是否尝试过创建表单的新实例?
<form id='main'>
<fieldset>
<input id='in2' type='text' placeholder='2.50' pattern='[-0-9]+?|[-0-9]*?\.[0-9]+?' required autocomplete='off'>
<input id='in1' type="number" value="2.50" step="0.50">
<input type='submit'>
</fieldset>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>