Results 1 to 16 of 16

Thread: [2008] Speeding Up this Code - Urgent!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    [2008] Speeding Up this Code - Urgent!

    Alright, I have this code which - loops through a text file (very big text file) to find the Course ID, then it adds the item to the List Box and while its doing that, it creates the .mdb file & adds some data from the text file to the database, and while all that is going, a Progress Bar ( which I have yet to figure out how to get it to increment with my Adding of Data ).

    The Code is very slow and the application gets hung up with Not Responding if I click on it, I want the user to be able to click on the App to cancel the current import but at the moment it just freezes up.

    Code:

    vb Code:
    1. Option Strict Off
    2. Imports System.Data.OleDb
    3. Imports System.IO
    4. Module modImport
    5.     Public Sub ImportCourse()
    6.         With frmCourses
    7.             If .ofdOpen.ShowDialog = DialogResult.OK Then
    8.                 Dim FileCopyName As String = Application.StartupPath & "\Data\Courses\" & Path.GetFileName(.ofdOpen.FileName.Remove(.ofdOpen.FileName.LastIndexOf("."))) & ".CSV"
    9.                 If File.Exists(FileCopyName) = True Then
    10.                     File.Delete(FileCopyName) 'Temp
    11.                 Else
    12.                     File.Copy(.ofdOpen.FileName, FileCopyName)
    13.                     SQL = "SELECT [Classes(Import If #1)] FROM " & .ofdOpen.FileName
    14.                     Dim Path As String = IO.Path.GetFullPath(.ofdOpen.FileName.Remove(.ofdOpen.FileName.LastIndexOf("\")))
    15.                     Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Path & ";Extended Properties=""text;HDR=Yes;FMT=Delimited"";")
    16.                     Command = New OleDbCommand(SQL, Connection)
    17.                     Command.Connection.Open()
    18.                     Adapter.SelectCommand = Command
    19.                     DS = New DataSet
    20.                     Adapter.Fill(DS)
    21.                     Command.Connection.Close()
    22.                     frmImport.pbImport.Maximum = DS.Tables(0).Rows.Count
    23.                     frmImport.pbImport.Minimum = 0
    24.                     For Each DRow As DataRow In DS.Tables(0).Rows
    25.                         If .lstCourses.Items.Contains(DRow("Classes(Import If #1)")) = False Then
    26.                             Connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Data\Courses\" & DRow("Classes(Import If #1)") & ".mdb")
    27.                             CreateCourse(Connection.ConnectionString)
    28.                             SQL = "INSERT INTO [Information] (CourseCode) VALUES (?); "
    29.                             Command = New OleDbCommand(SQL, Connection)
    30.                             Command.Connection.Open()
    31.                             Command.Parameters.AddWithValue("CourseCode", DRow("Classes(Import If #1)"))
    32.                             Command.ExecuteNonQuery()
    33.                             Connection.Close()
    34.                             frmImport.Show()
    35.                             If frmImport.pbImport.Value < frmImport.pbImport.Maximum Then
    36.                                 frmImport.pbImport.Value += 5
    37.                             End If
    38.                         End If
    39.                     Next
    40.                     frmImport.Close()
    41.                     File.Delete(FileCopyName)
    42.                 End If
    43.  
    44.             End If
    45.         End With
    46.     End Sub
    47. End Module

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Speeding Up this Code - Urgent!

    If you don't want to hang the UI, call the sub using

    Code:
    ThreadPool.QueueUserWorkItem(AddressOf ImportCourse)
    To cancel you'll have to regularly check a boolean value. Or you can kill the thread (not recommended).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Speeding Up this Code - Urgent!

    Hedy, I did that but I get this error and poiints to .ofdOpen.ShowDialog

    Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Speeding Up this Code - Urgent!

    adding

    application.doevents

    in the for next loop will make the ui more responsive
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Speeding Up this Code - Urgent!

    So how do I fix the above problem & how do Ig et the progress bar to go with increase with the adding of the listbox items. ?

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Speeding Up this Code - Urgent!

    Why is Option Strict OFF? That alone will cost you in speed.

    I don't see a loop, so I assume you didn't post that part, which doesn't really matter. DoEvents will interupt execution to allow the app to process any pending messages. Adding that line to the loop will remove the not responding issue and make the UI responsive. You would want to change the loop, though. If you have a Cancel button, just have it set a boolean to true, and in the loop, quit the loop if the boolean is true. Therefore, the DoEvents will allow the Cancel button click event to fire if the user clicks the button, which will set the boolean to true, which will stop the loop when execution comes back to the loop after the DoEvents.

    As for the progress bar, can you figure out how much the total progress is such that you can determine how far along you are each time through the loop? If you can, then you have more options, otherwise you need a progress bar that is more like a spinning clock, or some such. You've seen those in plenty of places. They are just used to indicate that progress is happening, but they don't give the user any information as to how far along they are and how long they have to go.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Speeding Up this Code - Urgent!

    Well what I'm doing atm is setting the maxium to the DS.tables(0).Rows.Count and then adding +5 to the values of the progress bar each time it loops but then near the end it says 'Number HEre' must be smaller than the max and bigger than min . I guess because its going over. So how would I stop that? I tried doing Value = 0 on the Progress Bar but it just restarted the whole PB.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Speeding Up this Code - Urgent!

    Right, check whether the value+5 would be greater than the max value, and if it is, set the value to the max value.
    My usual boring signature: Nothing

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] Speeding Up this Code - Urgent!

    i agree with shaggy about option strict, turn it on.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Speeding Up this Code - Urgent!

    Value of '8860' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'. Parameter name: Value

    with this code:

    vb Code:
    1. frmImport.Show()
    2.                             frmImport.pbImport.Maximum = DS.Tables(0).Rows.Count
    3.                             If frmImport.pbImport.Value >= DS.Tables(0).Rows.Count Then
    4.                                 frmImport.pbImport.Value = frmImport.pbImport.Maximum
    5.                             ElseIf frmImport.pbImport.Value < DS.Tables(0).Rows.Count Then
    6.                                 frmImport.pbImport.Value += 5
    7.                             End If

  11. #11
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    Re: [2008] Speeding Up this Code - Urgent!

    why increment by 5 each time? For each row you are going to add 5 but the max is N. If N is 600 rows then you will go over the max value by the time you hit row 120.

    Either you can change your loop to not use For ... Each or you need to initialize a counter, and increment it 1 for each row processed (basically a good ole for...next loop)
    If someone has helped you, please make sure to rate them.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Speeding Up this Code - Urgent!

    I, too, don't see why you are incrementing by 5, but no matter. Change the code as such:

    vb Code:
    1. If frmImport.pbImport.Value > DS.Tables(0).Rows.Count+5 Then
    2.  frmImport.pbImport.Value = frmImport.pbImport.Maximum
    3. else
    4.  frmImport.pbImport.Value += 5                          
    5. End If
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Speeding Up this Code - Urgent!

    Yea, I changed around the code to do pbImport.increment(5) and it works like a charm. I was just following a tut I found through Google. I guess it wasn't very effictive.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Speeding Up this Code - Urgent!

    So, did you get rid of that Option Strict OFF?
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    Re: [2008] Speeding Up this Code - Urgent!

    Yes, I changed it to Option Strict On, I was reading another post of yours where the OO said that 1 teacher said don't use the other said used and then you went into explaining and thats where I decided I would turn it on - but I turned it off becuse I as doing something like this

    dim i as integer
    i = i + 1

    but then it said + was not a Option Stric thingy. so I turned it off, but forgot to turn it back on.

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Speeding Up this Code - Urgent!

    Good, one of my programs showed a 50% speed boost by turning Option Strict ON. You seem like a good candidate for that benefit.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width