【问题标题】:vb.net Displaying an animated Gif while waiting a processvb.net 在等待进程时显示动画 Gif
【发布时间】:2021-06-18 10:12:23
【问题描述】:

我正在运行代码以将数据表导出到 Excel。由于这需要一段时间,我不想混淆用户并显示图像

Dim _excel As New Microsoft.Office.Interop.Excel.Application
        Dim wBook As Microsoft.Office.Interop.Excel.Workbook
        Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet

        PictureLoading.Visible = True

        wBook = _excel.Workbooks.Add()
        wSheet = wBook.ActiveSheet()

        Dim dt As System.Data.DataTable = GetTableForCompleteArchive()
        Dim dc As System.Data.DataColumn
        Dim dr As System.Data.DataRow
        Dim colIndex As Integer = 0
        Dim rowIndex As Integer = 0
        PictureLoading.Refresh()
        For Each dc In dt.Columns
            colIndex += 1
            _excel.Cells(1, colIndex) = dc.ColumnName
        Next
        PictureLoading.Refresh()
        For Each dr In dt.Rows
            rowIndex += 1
            colIndex = 0
            For Each dc In dt.Columns
                colIndex += 1
                _excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
            Next
        Next
        PictureLoading.Refresh()
        wSheet.Columns.AutoFit()
        Dim strFileName As String = Application.StartupPath & "\Export.xlsx"  '"C:\datatable.xlsx"
        If System.IO.File.Exists(strFileName) Then
            System.IO.File.Delete(strFileName)
        End If
        PictureLoading.Refresh()
        wBook.SaveAs(strFileName)
        wBook.Close()
        _excel.Quit()

        PictureLoading.Visible = False

问题是图像冻结并且没有动画。有没有办法在等待过程时让图像动画?

【问题讨论】:

  • 一种解决方案是将您的 excel 处理移动到后台线程,以便您在 UI 线程上显示动画。
  • @Hursey 你能告诉我怎么做吗?
  • @Hursey 我和后台工作人员一起工作

标签: excel vb.net winforms datatable animated-gif


【解决方案1】:
Private Sub TotalArchiveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EXCELToolStripMenuItem.Click

        BackgroundWorker1.WorkerSupportsCancellation = True
        BackgroundWorker1.WorkerReportsProgress = True
        'call this method to start your asynchronous Task.
        BackgroundWorker1.RunWorkerAsync()
        'display the loading image
        PictureLoading.Visible = True

    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        'call the function to export to Excel
        Export2Excel()
        If BackgroundWorker1.CancellationPending Then
            e.Cancel = True
        End If

    End Sub
    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        '' This event is fired when your BackgroundWorker exits.
        '' It may have exitted Normally after completing its task, 
        '' or because of Cancellation, or due to any Error.
        PictureLoading.Visible = False

    End Sub

我的函数是从Backgroundworker_DoWork 内部运行的。当我点击TotalArchiveToolStripMenuItem 时图像设置为可见,当BackgroundWorker 完成时设置为不可见。

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 2013-10-19
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多