vb.net - vb.net 线程和加载面板
问题描述
我尝试使用 vb.net 线程以避免表单冻结并分别加载 datagridview 数据
我有 Main form Called home,当我单击它时有一个按钮会召唤另一个名为Formone的表单,其中包含图表和 datagridview。
Dim trr As System.Threading.Thread
Private Sub Formone_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressPanel1.Show() '// this is loading GIF
trr = New Thread(AddressOf load_dgv)
trr.Priority = ThreadPriority.AboveNormal
trr.Start()
stats() '/ this is chartcontrol
End Sub
Private Sub load_dgv()
'here sql server query and load it to a table
end sub
Sub assignit(ByVal x As DataTable)
If Me.InvokeRequired Then
Me.Invoke(New Action(Of DataTable)(AddressOf assignit), x)
Else
GridControl1.DataSource = x
ProgressPanel1.Hide()
End If
数据正常加载到 datagridview 和图表,但表单在加载数据时冻结,加载 gif 不显示,按钮也冻结。
我想在加载数据时显示加载 gif 并避免冻结。
解决方案
这是发生这种情况的原因:
动画只能在 UI 线程中处理。
我的应用程序遇到了这个问题。
继承人你做什么:
在您的 UI 线程中:
SplashScreen.Show
'里面有你的动画/标志/gif
当你的工作线程加载数据时,你需要在它之后放置一个等待函数。
转到您在 vb 中的工具箱并选择 Background WORKER。双击您的后台工作对象,它将带您到 BackgrounderWOrker_DoWork() 部分
您可以在此处进行所有加载。
回到你的 UI 线程:
Splash.Show
BackgroundWorker.RunWorkerAsync 'execute the background worker
While BackGroundWorker.IsBusy = True 'wait for the background worker
Application.DoEvents()
End While
这样,您的 gif 将在 UI 线程上运行时显示平滑,并且您的后台工作人员会加载您的数据。.
推荐阅读
- java - 连接池连接不足。在 Jetty 嵌入式服务器中使用 DBCP2 BasicDataSource
- vb.net - 我在 Visual Basic -2015 中更新单个记录的完整代码,显示错误?
- docker - 如何为构建以运行 Spring Boot 应用程序的 Docker 映像配置 HTTPS 代理服务器?
- r - 使用 XML 包“Null”错误在 R 中进行 Webscraping
- reactjs - 在 React 和 Gatsby 中按 className 选择 dom 元素的正确方法
- javascript - 在 Github 页面上部署 Angular Universal?
- docusignapi - 如何在具有不同路由顺序的docusign API中添加相同的收件人?
- python - 为什么总是显示最稀有的输出?(Python 3)
- r - ggplot2 - 手动将箱线图分面标签指定为符号/数学表达式
- shell - 在我的 shell (ZSH) 中更改目录和文件的颜色