CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2001
    Posts
    165

    Updating Form Data Synchronously

    I have a program which gathers a lot of data from a remote database and then processes the information. On my main form I have a series of labels that are updated when each new process is running to let the user know what is going on.

    However, it is possible for the user to request such a large amount of data to process that the first function alone may take 5 minutes. I decided to add a timer that goes off every second to update an elapsed time display in the corner of the form. This timer is enabled at the start of the function and the begin time is set globally. Then in the timer event function it gets the minutes and seconds elapsed and updates a label caption. The label is not being updated on the screen however. I tested the code to make sure it is working properly and it is. Here is the timer event function:

    private Sub Timer1_Timer()
    Dim nMinutes as Integer, nSeconds as Integer
    Dim sTime as string

    nMinutes = Minute(time - TimeBegin)
    nSeconds = Second(time - TimeBegin)

    sTime = nMinutes & ":"
    If nSeconds < 10 then sTime = sTime & "0"
    sTime = sTime & nSeconds

    lblTime.Caption = sTime
    DoEvents
    End Sub



    TimeBegin is obviously a TimeDate object containing the start time.

    Is there any reason why the label should not be updated every second like it is set to do?

    The majority of the time is spent waiting for an SQL query to return from the server. This could be sped up by setting up better indexes on the Table but that is not up to me to do and this is only a management reporting tool that is not time-critical.

    -K


  2. #2
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Updating Form Data Synchronously

    When you execute your SQL statements, pass the adAsyncExecute flag and then place a loop after it, which also does a Doevents...


    sql = "Your SQL Statement here..."

    m_objConnection.Execute sql, , adAsyncExecute

    While m_objConnection.State And adStateExecuting
    DoEvents
    Wend




    that might be a little resource intensive by itself, you'd want to check some sort of counter that only calls doevents every 20 times or something. but even when you are checking the timer every second, that code may not be running because the app is effectivly "hung" because it's waiting for a response from SQL Server. If you tell ADO to do that action asyncronously, then your app will maintain control - but you still have to wait while SQL Server is doin it's thing - hence the loop directly after the execute call.

    hope this helps,

    john

    John Pirkey
    MCSD
    http://www.ShallowWaterSystems.com
    http://www.stlvbug.org
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

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

Featured