Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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.

[EDIT AFTER SOLUTION 1]
VB
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


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.
Posted
Updated 24-Aug-18 13:03pm
v2

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.
 
Share this answer
 
Comments
Member 11856456 24-Aug-18 13:30pm    
content moved to the question in [EDIT After solution 1]
Nelek 24-Aug-18 16:50pm    
Please use the "improve question" widget to add code so that it can get the proper formatting.
VB
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900