首页 > 解决方案 > 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 并避免冻结。

标签: vb.netmultithreadingloading

解决方案


这是发生这种情况的原因:

动画只能在 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 线程上运行时显示平滑,并且您的后台工作人员会加载您的数据。.


推荐阅读