Best way to continual loop capture error status and ok to save in VB.net

jana
jana used Ask the Experts™
on
We have a routine then saves a series of data to SQL tables.  However, we want to place some sort of search of status to know if ok to save.  This "search" since it will be looking continual loop until status is reached, need to have some way to exit the loop.

This is in VB.NET
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try rephrasing your question.

>need to have some way to exit the loop.

Depending on loop, you can use

Exit For
Exit While

etc

Author

Commented:
The routine in SAVE button is as follows:

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Do Until RecordBeenUnlocked Is True
            Call CheckIfRecordBeenUnlock() 'here where var 'RecordBeenUnlocked' if set to true or false
        Loop
        'Save if only recoerd has been unlock
        Call SaveAllTransToSQL()
    End Sub

Open in new window


The end result is that we want to run SAVE only if a certain record or row has been unlocked

Also we would like some sort of keypress incase the row is not locked and user can press ESC to continue some other time

Please adive
Most Valuable Expert 2012
Top Expert 2014
Commented:
>The end result is that we want to run SAVE only if a certain record or row has been unlocked

Change code to

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ShouldSave As Boolean
        Do Until RecordBeenUnlocked Is True
            ShouldSave = CheckIfRecordBeenUnlock() 
        Loop
        'Save if only recoerd has been unlock
        If ShouldSave Then
           SaveAllTransToSQL()
        End If
    End Sub

Open in new window




>Also we would like some sort of keypress incase the row is not locked and user can press ESC to continue some other time


Try following

Dim ShouldExit As Boolean
    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim ShouldSave As Boolean
        Do Until RecordBeenUnlocked Is True
            ShouldSave = CheckIfRecordBeenUnlock() 
            If ShouldExit Then Exit Sub
            Application.DoEvents()
        Loop
        'Save if only recoerd has been unlock
        If ShouldSave Then
           SaveAllTransToSQL()
        End If
    End Sub

Open in new window


Now set the ShouldExit to true in keypress or cancel button click etc.

Author

Commented:
Not in the computer to try, but was asked that since there is a call to a routine that performs a series of update to SQL tables, that the Save Button routine should be placed in a thread.

Can you give us your point of view?

Author

Commented:
Also can you give us a brief explanation of Application.DoEvents()?

Author

Commented:
We have the following recommended script (es ID: 37857666) to run thru and exit on 2 condition:

    1. If the user press ESC
    2. If CheckIfRecordBeenUnlock routine (checks if record unlock) is trur

We would like if EE can assist us in these 3 last doubts we have so we can close:

1. How can we place a message form, not MsgBox, that will show while the
    loop "Do Until RecordBeenUnlocked" is running and will not affect the actual loop.

2. We were recommnended to place this script within a Thread since a subroutine
    within the loop doeas actual SQL tables update.  Can EE give us a point of view on the
    following (that is, if its tru or should we consider it)?

3. In the recommended script, there is a "Application.DoEvents()", we assume this
    is so the user when pressing ESC it exits the Loop; however, we were told that it a bad
    way or dangerous way to place Application.DoEvents() in our code.  Can EE give
    us thier opinion?

Thanx in advance
Most Valuable Expert 2012
Top Expert 2014
Commented:
1) You can put a label on a form or a marquee style progressbar and show it while this code is running

2) Yes you can put this code in a thread or backgroundworker

3) Application.DoEvents forces the form to process its message pump so UI remains responsive. You wont need to use it if you use a thread or backgroundworker.

Author

Commented:
But should we consider placing this short script within thread?

Also the "apps...DoEvent", is there a dangour placing it as it is?
Most Valuable Expert 2012
Top Expert 2014
Commented:
DoEvents forces the form to refresh itself. It would not make your PC blow so its not dangerous. If you find yourself in a situation where you are calling Doevents constantly in a loop then its advised to refactor the code and run it on a separate thread.

Script is short but it contains an infinite type loop so it makes sense to run it in separate thread.

Author

Commented:
Unfortunately we haven't yet tested it, but this script while in a loop, if the user presses ESC, it would exit the loop; right?
Most Valuable Expert 2012
Top Expert 2014
Commented:
If you add the code which checks for key presses and sets the ShouldExit to true when ESC is pressed then yes.

Author

Commented:
Thanx!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial