如何使用进度显示插入表中的记录的进度 [英] How do I use a progress to show progress of records being inserted into a table

查看:68
本文介绍了如何使用进度显示插入表中的记录的进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至目前,我已经从表单中创建了一个消息框,该表单允许我从Excel工作表中将新记录输入到我的表中或将其转换为全新表。



这里是代码:



As of right now, I have made a messagebox from a form that gives me the option to input new records into my table from an excel sheet or convert it over to a brand new table.

here is the code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       ProgressBar1.Maximum = Form1.totalrecords
       ProgressBar1.Increment(1)

       If ProgressBar1.Value = ProgressBar1.Maximum Then
           Timer1.Stop()
       End If
   End Sub







Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Form1.import_new_table()
       Form1.divide_temp_table_into_other_tables()

       Timer1.Start()
   End Sub







the

Form1.totalrecords

基于在excel表中被复制到数据库的总行数。



我遇到的问题是屏幕冻结,直到完成该过程并进行桌子似乎是在导入桌子后开始的。



任何建议tions?



我尝试了什么:



我试过设置最小值并进入最大值,但这个问题似乎是持久的。




is based upon the total rows in the excel sheet that is being copied to the database.

The issue that I am having is the screen freezes until the process is done and the progress bar seems to start after the table has been imported.

Any suggestions?

What I have tried:

I have tried to set the minimum value and step into the maximum value but this problem seems to be persistent.


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   BackgroundWorker1.RunWorkerAsync()
   Form1.import_new_table()
   Form1.divide_temp_table_into_other_tables()

End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
   For x As Integer = 0 To 100
      System.Threading.Thread.Sleep(100)
      BackgroundWorker1.ReportProgress(CInt(x / Form1.totalrecords) * 100)
   Next
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
   ProgressBar1.Value = e.ProgressPercentage
   '  If ProgressBar1.Value = ProgressBar1.Maximum Then

   ' End If
End Sub

Private Sub Import_excel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
   ProgressBar1.Maximum = Form1.totalrecords
End Sub





然而,进度条没有移动,现在内存很多它越大大约是152 MB,现在它最高可达9 - 10 gb。



however, The progress bar is not moving, and now the memory much larger it was around 152 mb and now it shoots up to 9 - 10 gb.

推荐答案

您需要使用后台工作线程来导入记录。在GUI线程中运行导入可防止系统更新屏幕。 codeproject:后台工作人员 - Google搜索 [ ^ ]解释问题及其解决方案。
You need to use a background worker thread to do the importing of the records. Running the import in the GUI thread prevents the system from updating the screen. Plenty of articles listed at codeproject: background worker - Google Search[^] that explain the problem and its solution.


private Maximum_rec as integer

Sub Importdata 
     ' you have to count records before importing
     Maximum_rec = Form1.totalrecords
     Form1.import_new_table()
     Form1.divide_temp_table_into_other_tables()
End Sub 

Sub Importdata_withprogress

       Dim nth As New Threading.Thread(AddressOf Importdata )
       nth.Start()
       While nth.IsAlive

            IF Maximum_rec <> 0 THEN

                ProgressBar1.Maximum = Maximum_rec
                'x=the current record in loop
                IF x <= Maximum_rec Then
                    ProgressBar1.Value = x
                END IF

            END IF

            Application.DoEvents
            'Threading.Thread.Sleep(100)

       End While

End Sub


这篇关于如何使用进度显示插入表中的记录的进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆