Wpf 在dgv加载时显示加载动画
更新代码。我认为它很接近,但dgv没有加载任何数据 公开课密室作业 Public sqlcon As String=My.Settings.New\u Assembly\u AccessConnectionString 公共con作为新的SqlConnection(sqlcon) 作为字符串的公共作业Wpf 在dgv加载时显示加载动画,wpf,vb.net,multithreading,datagridview,backgroundworker,Wpf,Vb.net,Multithreading,Datagridview,Backgroundworker,更新代码。我认为它很接近,但dgv没有加载任何数据 公开课密室作业 Public sqlcon As String=My.Settings.New\u Assembly\u AccessConnectionString 公共con作为新的SqlConnection(sqlcon) 作为字符串的公共作业 Public Async Function GetDataAsync(ByVal sql As String, ByVal sqlcon As String) As Task(Of DataTab
Public Async Function GetDataAsync(ByVal sql As String, ByVal sqlcon As String) As Task(Of DataTable)
Dim dt As New DataTable
Dim cmd As New SqlCommand(sql, con)
Using da = New SqlDataAdapter(sql, sqlcon)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("@Job2", job)
Await Task.Run(Function()
da.Fill(dt)
End Function)
End Using
Return dt
End Function
Public Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
Refresh.Visibility = Windows.Visibility.Visible
txtJob.IsEnabled = False
btnEnter.IsEnabled = False
Try
job = txtJob.Text
Dim sqlcon As String = My.Settings.New_Assembly_AccessConnectionString
Dim sql As String
sql = "SELECT isnull(p.[CLASS],j.class) as 'CLASS',isnull([SERIAL_NUMBER],left(year(d.cup_mfg_date),2) + d.cup_serial) as SERIAL, " & _
"isnull([CUP_DATE],d.cup_mfg_date) as CUPDATE, isnull([CUP_PART_NUM],left([Cup#],charindex('-',Cup#)-1)) as PARTNUM, isnull(p.[LATERAL_VALUE], " & _
"d.lateral_value * 10000) as LATVALUE, isnull([LAT_UPPER],0) as LAT_UPPER,isnull([LAT_LOWER],0) as LAT_LOWER, " & _
"isnull(p.[BEFORE_WEIGHT],0) as BEFORE_WEIGHT, isnull(p.[AFTER_WEIGHT],0) as AFTER_WEIGHT,isnull([GREASE_UPPER],0) as GREASE_UPPER, " & _
"isnull([GREASE_LOWER],0) as GREASE_LOWER,isnull(p.[SPACER_MEASURE], d.Spacer_Measure) as SPACER,isnull([QTY_SPACER_CHANGE],0) as 'CHANGES', " & _
"isnull([LATERAL_DATE_TIME],'1999-11-11 11:11:11.111') as LATERAL_DATE_TIME, " & _
"isnull([GREASE_DATE_TIME],'1999-11-11 11:11:11.111') as GREASE_DATE_TIME, isnull([LINE_NUM],d.linenum) as LINE, " & _
"isnull(BAD_PART, 'BAD_INFO') as Result, isnull([AIR_PRESSURE1], 0) as PRESSURE " & _
"FROM [NB_ASSEMBLY].[dbo].[PieceData] p full outer JOIN New_Assembly_Access.dbo.Tbl_Data AS d " & _
"ON substring(d.zbarcode,3,6) = right(p.serial_number,6) and month(d.cup_mfg_date) = month(p.cup_date) and " & _
"right(d.zbarcode,10) = right(p.cup_part_num,10) join new_assembly_Access.dbo.tbl_job j on j.job# = d.job# where d.job# = @Job2"
con.Open()
Dim data = Await GetDataAsync(sql, sqlcon)
dgvJob.DataContext = data
dgvJob.AutoGenerateColumns = True
dgvJob.CanUserAddRows = False
Catch ex As Exception
End Try
Refresh.Visibility = Windows.Visibility.Hidden
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Refresh.Visibility = Windows.Visibility.Hidden
txtJob.Focus()
End Sub
End Class这是您动态修改的代码,从这里开始,因为我不知道是否运行良好,或者是否需要在没有数据库和其他参数的情况下进行更改
Public con As New SqlConnection
Public job As String
Private Sub ReadDatabase()
Dim bgThread As Threading.Thread = New Threading.Thread(Sub()
Dim sqlcon As String = My.Settings.New_Assembly_AccessConnectionString
Dim sql As New SqlCommand
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Using con = New SqlConnection(sqlcon)
con.Open()
sql = New SqlCommand("SELECT isnull(p.[CLASS],j.class) as 'CLASS',isnull([SERIAL_NUMBER],left(year(d.cup_mfg_date),2) + d.cup_serial) as SERIAL, " &
"isnull([CUP_DATE],d.cup_mfg_date) as CUPDATE, isnull([CUP_PART_NUM],left([Cup#],charindex('-',Cup#)-1)) as PARTNUM, isnull(p.[LATERAL_VALUE], " &
"d.lateral_value * 10000) as LATVALUE, isnull([LAT_UPPER],0) as LAT_UPPER,isnull([LAT_LOWER],0) as LAT_LOWER, " &
"isnull(p.[BEFORE_WEIGHT],0) as BEFORE_WEIGHT, isnull(p.[AFTER_WEIGHT],0) as AFTER_WEIGHT,isnull([GREASE_UPPER],0) as GREASE_UPPER, " &
"isnull([GREASE_LOWER],0) as GREASE_LOWER,isnull(p.[SPACER_MEASURE], d.Spacer_Measure) as SPACER,isnull([QTY_SPACER_CHANGE],0) as 'CHANGES', " &
"isnull([LATERAL_DATE_TIME],'1999-11-11 11:11:11.111') as LATERAL_DATE_TIME, " &
"isnull([GREASE_DATE_TIME],'1999-11-11 11:11:11.111') as GREASE_DATE_TIME, isnull([LINE_NUM],d.linenum) as LINE, " &
"isnull(BAD_PART, 'BAD_INFO') as Result, isnull([AIR_PRESSURE1], 0) as PRESSURE " &
"FROM [NB_ASSEMBLY].[dbo].[PieceData] p full outer JOIN New_Assembly_Access.dbo.Tbl_Data AS d " &
"ON substring(d.zbarcode,3,6) = right(p.serial_number,6) and month(d.cup_mfg_date) = month(p.cup_date) and " &
"right(d.zbarcode,10) = right(p.cup_part_num,10) join new_assembly_Access.dbo.tbl_job j on j.job# = d.job# where d.job# = @Job2", con)
sql.Parameters.AddWithValue("@Job2", job)
da.SelectCommand = sql
Dim dt As New DataTable
da.Fill(dt)
Invoke(Sub()
dgvJob.DataContext = dt.DefaultView
dgvJob.AutoGenerateColumns = True
dgvJob.CanUserAddRows = False
End Sub)
End Using
End Sub) With {
.IsBackground = True
}
bgThread.Start()
End Sub
Public Sub Button_Click(sender As Object, e As RoutedEventArgs)
txtJob.IsEnabled = False
btnEnter.IsEnabled = False
job = txtJob.Text
ReadDatabase()
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
txtJob.Focus()
End Sub
我建议你清理一下你的代码,让方法只做一件事——不要有一个ReadDatabase,它也会摆弄控件等等 DataAdapter可以获取一个sql字符串和一个连接字符串,它知道如何建立连接和打开连接等。它会整理好一切,然后将所有这些交给DA 可能会把这个庞大的SQL字符串放到一个资源文件中
Private Async Function GetComponentsDataTable() as Task(Of DataTable)
Dim con As String = My.Settings.New_Assembly_AccessConnectionString
Dim sql as String = "SELECT isnull(p.[CLASS],j.class) as 'CLASS',isnull([SERIAL_NUMBER],left(year(d.cup_mfg_date),2) + d.cup_serial) as SERIAL, " & _
"isnull([CUP_DATE],d.cup_mfg_date) as CUPDATE, isnull([CUP_PART_NUM],left([Cup#],charindex('-',Cup#)-1)) as PARTNUM, isnull(p.[LATERAL_VALUE], " & _
"d.lateral_value * 10000) as LATVALUE, isnull([LAT_UPPER],0) as LAT_UPPER,isnull([LAT_LOWER],0) as LAT_LOWER, " & _
"isnull(p.[BEFORE_WEIGHT],0) as BEFORE_WEIGHT, isnull(p.[AFTER_WEIGHT],0) as AFTER_WEIGHT,isnull([GREASE_UPPER],0) as GREASE_UPPER, " & _
"isnull([GREASE_LOWER],0) as GREASE_LOWER,isnull(p.[SPACER_MEASURE], d.Spacer_Measure) as SPACER,isnull([QTY_SPACER_CHANGE],0) as 'CHANGES', " & _
"isnull([LATERAL_DATE_TIME],'1999-11-11 11:11:11.111') as LATERAL_DATE_TIME, " & _
"isnull([GREASE_DATE_TIME],'1999-11-11 11:11:11.111') as GREASE_DATE_TIME, isnull([LINE_NUM],d.linenum) as LINE, " & _
"isnull(BAD_PART, 'BAD_INFO') as Result, isnull([AIR_PRESSURE1], 0) as PRESSURE " & _
"FROM [NB_ASSEMBLY].[dbo].[PieceData] p full outer JOIN New_Assembly_Access.dbo.Tbl_Data AS d " & _
"ON substring(d.zbarcode,3,6) = right(p.serial_number,6) and month(d.cup_mfg_date) = month(p.cup_date) and " & _
"right(d.zbarcode,10) = right(p.cup_part_num,10) join new_assembly_Access.dbo.tbl_job j on j.job# = d.job# where d.job# = @Job2"
Using da as New SqlDataAdapter(sql, con)
da.SelectCommand.Parameters.AddWithValue("@Job2", job) 'see https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/'
Dim dt As New DataTable
Await Task.Run(Sub() da.Fill(dt)) 'also, see note from AlexB
Return dt
End Using
End Sub
Public Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
txtJob.IsEnabled = False
btnEnter.IsEnabled = False
Dim dt = Await GetComponentsDataTable()
dgvJob.AutoGenerateColumns = True
dgvJob.DataContext = dt.DefaultView
dgvJob.CanUserAddRows = False
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
txtJob.Focus()
End Sub
开始阅读关于。您的
ReadDatabase()
应该是async
读取数据库的值。在UI中,等待对ReadDatabase()的调用。提示:没有人会为您编写完整的代码。如果您有特定的问题或例外,请回来。我为没有发布我的特定问题而道歉。我真的不知道该贴哪一张,甚至连一张都没有。我和Backgroundworker至少试过几次,都被激怒了,并且删除了所有内容。然后发现一些使用异步的例子,我无法开始工作…所以我再次删除了所有内容。我无意冒犯。这只是一个提醒。看来你从Caius那里得到了一个有效的解决方案。我建议您真正深入到async/Wait主题,并尝试了解它是如何工作的。感谢您的回复。您的代码给了我一个dt(datatable)的空引用异常。啊,在GetComponentDataTable的末尾遗漏了一个返回dt
,现在添加了。另外,您应该进入项目属性/设置,并在“编译”选项卡上启用“严格”选项。它可能会导致出现其他错误;这是设计-解决它们,因为它们是等待发生的bug。“Option Strict Off”=“让开发人员轻松自如地处理几乎所有的事情,希望在运行时不会发生任何爆炸”。Option Strict On=“执行更严格的代码准确性检查”。后者总是比前者更好-在设计时会发现这一点。答案很好,但我个人会在这个场景中明确使用ConfigureAwait(true)
,因为您处理的是UI。我知道这是多余的,因为默认情况下它是真实的,但对于学习和文档编制来说,这是一个很好的行为。