... Am i putting the user want statement in right place.If i wana stop the scanning(i mean do correction) until EOF in the middle of the scanning process and resume back?How.Is that the userwantout function to abort the program in the middle of the one running process and continue back?
You have it very nearly correct. Apart from the extra
End If at the end (which may relate to an earlier
If that you didn't include in the message) I think this will work. (Note, I added some comments in your code.)
However, there is one change I would make to improve the responsiveness. Your check for whether the user wants to exit is only being done when you make a change. You should do the check each time around the
For loop. Oh, also I spotted a bug. You never clear the
changeMade flag, so once you do change a section, it will keep reporting every section changed after that.
Here's my modified version...
- ' Scan the byte array and correct the constant parts.
-
For I = 49 To mFileSize - 6 Step 6
-
-
' Clear our "change made in this section" flag.
-
changemade = False
-
-
' Display original version of each line.
-
rich1.SelStart = Len(rich1.Text)
-
rich1.SelText = "Before : " & FormattedBytes(I, 6) & vbNewLine
-
-
' Check and correct 1st two bytes of each section.
-
If arrByte(I) <> a Then
-
arrByte(I) = a
-
changemade = True
-
End If
-
If arrByte(I + 1) <> b Then
-
arrByte(I + 1) = b
-
changemade = True
-
End If
-
-
' If section modified, show the corrected version.
-
If changemade Then
-
AnyChanged = True
-
rich1.SelText = "After : " & FormattedBytes(I, 6) & " <--- Corrected" & vbNewLine
-
End If
-
-
' Allow user to stop process by clicking Abort button.
-
DoEvents
-
If UserWantsOut Then
-
Exit For ' Drop out of the loop.
-
End If
-
-
Next
-
-
' Report whether we finished or were interrupted.
-
If UserWantsOut Then
-
MsgBox "Aborted by user!"
-
Else
-
MsgBox "Finished!"
-
End If
I think this will do the job. However, there is one enhancement I would suggest. Only report the "before" version of lines which are changed, rather than
all of them. To do this you would need to move lines 7-9 after the
If changeMade test, remove the string " <--- Corrected" (no longer required) and do one of the following...
- Make another copy of the array before you start the corrections, and just report "before" and "after" from the two arrays. Or
- Make a copy of the first two bytes just before you change them, and use those in place of the array elements when reporting the "before" version.
Oh, one last thing. It seems to me, doing the correction within the byte array is likely to be so fast that I doubt the user will have time to click the Abort button. Unless writing all that output to the RichTextBox slows it down, I suppose.