Results 1 to 12 of 12

Thread: [RESOLVED] Syntax checker makes my program run slow, how to make it faster?

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Malaysia
    Posts
    53

    Resolved [RESOLVED] Syntax checker makes my program run slow, how to make it faster?

    I'm using this syntax checker and it works.
    But this code will make my program run slowly.

    Any suggestion?

    (KeywordList and ObjectTagList are array.)

    VB Code:
    1. Dim iPos As Integer
    2.     'save the current cursor position
    3.     iPos = rtfText.SelStart
    4.     'prevent the window from changing
    5.     LockWindowUpdate rtfText.hwnd
    6.     'clear all highlighted
    7.     rtfText.SelStart = 0
    8.     rtfText.SelLength = Len(rtfText.Text)
    9.     rtfText.SelColor = vbBlack
    10.     rtfText.SelBold = False
    11.     'Search over RTB
    12.     For i = 0 To Len(rtfText.Text) - 1
    13.         For s = 0 To UBound(KeywordList)
    14.             hlstr = KeywordList(s)
    15.             res = rtfText.Find(hlstr, i, Len(rtfText.Text), 14)
    16.                 If res <> -1 Then
    17.                     rtfText.SelStart = res
    18.                     rtfText.SelLength = Len(hlstr)
    19.                     rtfText.SelColor = vbBlue
    20.                 End If
    21.         Next
    22.     Next
    23.    
    24.     For i = 0 To Len(rtfText.Text) - 1
    25.         For s = 0 To UBound(ObjectTagList)
    26.             hlstr = ObjectTagList(s)
    27.             res = rtfText.Find(hlstr, i, Len(rtfText.Text), 14)
    28.                 If res <> -1 Then
    29.                     rtfText.SelStart = res
    30.                     rtfText.SelLength = Len(hlstr)
    31.                     rtfText.SelBold = True
    32.                 End If
    33.         Next
    34.     Next
    35.     'restore the cursor position
    36.     rtfText.SelStart = iPos
    37.     'unlock the window
    38.     LockWindowUpdate 0

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Syntax checker makes my program run slow, how to make it faster?

    One thing...I would store the .SelStart position (iPos) in a Long variable as opposed to integer. It's common for large code to exceed 32KB.

    Also, it looks like you're looping through the entire RTB each time.

    The trick is to only check the current line that is being edited (or the previous line when Enter is pressed).

    You're also looping twice. One to check for keywords, and one to check for tags. You could put that all into one loop.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Malaysia
    Posts
    53

    Re: Syntax checker makes my program run slow, how to make it faster?

    Can you show me how to check only one line?
    I tried to check only one line.But when I paste some paragraphs, it will not check these paragraphs!

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Syntax checker makes my program run slow, how to make it faster?

    I'm curious, what's the text in the RTB? HTML? And what are the data in KeywordList() and ObjectTagList()?

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax checker makes my program run slow, how to make it faster?

    What help do you need?
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Malaysia
    Posts
    53

    Re: Syntax checker makes my program run slow, how to make it faster?

    My problem is the program will run slow when the text in the textbox is more than 10 line.

    The KeywordList() and ObjectTagList() is two array.
    I've made a code to search for keyword list over two file:
    keywords.dat and objects.dat

    Both are keywords list file and this is not my problem.


    Also, how to multiple undo/redo for my rtf textbox?

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax checker makes my program run slow, how to make it faster?

    See this link for multiple redo and undo
    Please mark you thread resolved using the Thread Tools as shown

  8. #8
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax checker makes my program run slow, how to make it faster?

    This will speed up the program
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Const WM_GETTEXTLENGTH As Long = &HE
    3.  
    4. Private Function Getlength(rich As RichTextBox) As Long
    5.   'Function To get the length of the text box
    6.   Getlength = SendMessage(rich.hwnd, WM_GETTEXTLENGTH, 0&, 0&)
    7. End Function
    8.  
    9. Dim iPos As Integer
    10.     'save the current cursor position
    11.     iPos = rtfText.SelStart
    12.     'prevent the window from changing
    13.     LockWindowUpdate rtfText.hwnd
    14.     'clear all highlighted
    15.     rtfText.SelStart = 0
    16.     rtfText.SelLength = Getlength(rtfText)
    17.     rtfText.SelColor = vbBlack
    18.     rtfText.SelBold = False
    19.     'Search over RTB
    20.     For i = 0 To Len(rtfText.Text) - 1
    21.         For s = 0 To UBound(KeywordList)
    22.             hlstr = KeywordList(s)
    23.             res = rtfText.Find(hlstr, i, Getlength(rtfText), 14)
    24.                 If res <> -1 Then
    25.                     rtfText.SelStart = res
    26.                     rtfText.SelLength = Len(hlstr)
    27.                     rtfText.SelColor = vbBlue
    28.                 End If
    29.         Next
    30.     Next
    31.    
    32.     For i = 0 To Len(rtfText.Text) - 1
    33.         For s = 0 To UBound(ObjectTagList)
    34.             hlstr = ObjectTagList(s)
    35.             res = rtfText.Find(hlstr, i, Getlength(rtfText), 14)
    36.                 If res <> -1 Then
    37.                     rtfText.SelStart = res
    38.                     rtfText.SelLength = Len(hlstr)
    39.                     rtfText.SelBold = True
    40.                 End If
    41.         Next
    42.     Next
    43.     'restore the cursor position
    44.     rtfText.SelStart = iPos
    45.     'unlock the window
    46.     LockWindowUpdate 0
    Please mark you thread resolved using the Thread Tools as shown

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Malaysia
    Posts
    53

    Re: Syntax checker makes my program run slow, how to make it faster?

    danasegarane, this code is still slowing my programs!

  10. #10

    Thread Starter
    Member
    Join Date
    Dec 2006
    Location
    Malaysia
    Posts
    53

    Re: Syntax checker makes my program run slow, how to make it faster?

    Quote Originally Posted by ZenDisaster
    VB Code:
    1. RTF1.Visible = False
    2.     For iKey = 0 To UBound(KeyWordList)
    3. TryAgain:
    4.         lFound = RTF1.Find(KeyWordList(iKey), lFound, , 14)
    5.         If lFound <> -1 Then
    6.             RTF1.SelStart = lFound
    7.             RTF1.SelLength = Len(KeyWordList(iKey))
    8.             RTF1.SelColor = vbBlue
    9.             lFound = lFound + Len(KeyWordList(iKey)) + 1
    10.             GoTo TryAgain
    11.         End If
    12.         lFound = 0
    13.     Next iKey
    14. RTF1.Visible = True

    Down from over 2 minutes to 0.140999999999622 seconds to complete.
    My first line is missing, why??

  11. #11
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Syntax checker makes my program run slow, how to make it faster?

    Have you found ,why is your program is slow down.As for as I can tell one thing.If you read the length of line which is more , then it will slow.For Getting the length
    you have to use this function
    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    2. Const WM_GETTEXTLENGTH As Long = &HE
    3.  
    4. Private Function Getlength(rich As RichTextBox) As Long
    5.   'Function To get the length of the text box
    6.   Getlength = SendMessage(rich.hwnd, WM_GETTEXTLENGTH, 0&, 0&)
    7. End Function
    Please mark you thread resolved using the Thread Tools as shown

  12. #12
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Syntax checker makes my program run slow, how to make it faster?

    One of the best speed ups you can make is to stop working dierctly on the richtextbox control itself.
    That is very slow.
    Store it in a String instead then work with the string to find the keywords and their positions.
    Then go back and make the changes.

    You can make it even faster by making the changes to a string variable then putting it back into the richtextbox as TextRTF.
    But to do that you have to parse the documents color table and add the color tags yourself.
    It's a little like learning to write HTML.

    There's a good example of it at planet source code by a guy called Bobo(?)

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