Results 1 to 11 of 11

Thread: [RESOLVED] How to prevent VB6 from hanging while refreshing remote database?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Location
    La Plata, Buenos Aires, Argentina
    Posts
    52

    Resolved [RESOLVED] How to prevent VB6 from hanging while refreshing remote database?

    Hi everyone.

    I have this big all-right program, and I want to add to it an ADODC that reads from a remote database (throught a DSN with ODBC) every 5 seconds for read for messages from me or other users, like a chat.
    It's all fine, but every time I do Adodc1.Refresh, the programs hangs for 1 second or so, and it can be annoying if you're typing or something.

    So, how can avoid that little delay, so the control can work in background without freezing everything else?
    I'm using an Adodc1, not by code but design-created, whose only ConnectionString is DSN = MyDsn.
    Every time I do:
    Code:
    AdoDc1.RecordSource = "SELECT * FROM messages where read = 0"
    AdoDc1.Refresh
    if freezes for 1 second or so. (Works perfectly, the only problem is the one I mentioned.)

    Thank you.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: How to prevent VB6 from hanging while refreshing remote database?

    I assume its causing the UI to stutter ? If so, then multi-threading is what you need but unfortunately this cannot be done in VB6 without some headache. However, I believe that ActiveX EXEs were provided as a way to work multi-threading into VB6 apps so I'd look that up. If you can pawn that query off on another thread through an ActiveX EXE, that might suffice.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Wait a second.....Are you seriously using a database to create a chat client ? Is that the primary purpose of your database ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4

    Thread Starter
    Member
    Join Date
    May 2012
    Location
    La Plata, Buenos Aires, Argentina
    Posts
    52

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Quote Originally Posted by Niya View Post
    If you can pawn that query off on another thread through an ActiveX EXE, that might suffice.
    Ok, I'll try. I've never created an Active EXE, but I'll try.

    How many records is that query returning anyway ?
    It doesn't matter. 1 or 100, it's the same delay. About 1 second. I've try using code, it's the same thing... well, more delay, actually.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2012
    Location
    La Plata, Buenos Aires, Argentina
    Posts
    52

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Quote Originally Posted by Niya View Post
    Wait a second.....Are you seriously using a database to create a chat client ? Is that the primary purpose of your database ?
    Well, I'm not proud of it, but it's not a teenagers chat, it's like a really slow chat, more like messages, so it's ok.
    And I searched and tried a lot to make a proper chat using me as Server, the problems are:
    1) I -Server- have to be always logged on
    2) I don't have a static IP and I'm behind a router, so those chats only works on LAN connections

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Using ADO you can create an Asynchronous Connection to the Database - see here: http://support.microsoft.com/kb/194960

  7. #7

    Thread Starter
    Member
    Join Date
    May 2012
    Location
    La Plata, Buenos Aires, Argentina
    Posts
    52

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Quote Originally Posted by Doogle View Post
    Using ADO you can create an Asynchronous Connection to the Database - see here: http://support.microsoft.com/kb/194960
    I'm using VB6, that's not VB6.

  8. #8

    Thread Starter
    Member
    Join Date
    May 2012
    Location
    La Plata, Buenos Aires, Argentina
    Posts
    52

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Well, I've found this: http://www.codeproject.com/Articles/...-walkthrough-w
    about making asynchronous processes in VB6. I'll explore that.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to prevent VB6 from hanging while refreshing remote database?

    It's not too difficult.

    Define the Connection Object WithEvents, create the connection string and Open the connection with the adAsyncConnect Option e.g.
    Code:
    Dim WithEvents db As Connection
    Code:
      Set db = New Connection
      db.CursorLocation = adUseClient
      db.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Public\HairDesign.mdb;", , , adAsyncConnect
    When the connection has been established the db_ConnectComplete event triggers. In this routine you can set a global variable to indicate that the connection has been made.
    You can then execute some sort of query. When the query has completed the db_ExecuteComplete event triggers and in that routine you can process the results of the query (e.g. Update some control or other with the results)

  10. #10

    Thread Starter
    Member
    Join Date
    May 2012
    Location
    La Plata, Buenos Aires, Argentina
    Posts
    52

    Re: How to prevent VB6 from hanging while refreshing remote database?

    Quote Originally Posted by Doogle View Post
    It's not too difficult.

    Define the Connection Object WithEvents, create the connection string and Open the connection with the adAsyncConnect Option e.g.
    Code:
    Dim WithEvents db As Connection
    Code:
      Set db = New Connection
      db.CursorLocation = adUseClient
      db.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Public\HairDesign.mdb;", , , adAsyncConnect
    When the connection has been established the db_ConnectComplete event triggers. In this routine you can set a global variable to indicate that the connection has been made.
    You can then execute some sort of query. When the query has completed the db_ExecuteComplete event triggers and in that routine you can process the results of the query (e.g. Update some control or other with the results)
    Thank you, Doogle! That was EXACTLY what I needed. It doesn't hang anymore.
    Thank you very very much

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] How to prevent VB6 from hanging while refreshing remote database?

    This is how it might hang together
    Code:
    Private WithEvents db As ADODB.Connection
    Private rs As ADODB.Recordset
    
    Private Sub db_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
    Timer1.Enabled = True
    End Sub
    
    Private Sub db_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
    Dim intI As Integer
    If Not (rs.EOF And rs.BOF) Then
        Do
            For intI = 0 To rs.Fields.Count - 1
                Debug.Print rs.Fields(intI).Name, rs.Fields(intI).Value
            Next intI
            rs.MoveNext
        Loop Until rs.EOF
    Else
        Debug.Print "No records returned from Query"
    End If
    rs.Close
    Set rs = Nothing
    End Sub
    
    Private Sub Form_Load()
    Timer1.Enabled = False
    Timer1.Interval = 5000
    Set db = New ADODB.Connection
    db.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Public\HairDesign.mdb;", , , adAsyncConnect
    End Sub
    
    Private Sub Timer1_Timer()
    Dim strSQL As String
    strSQL = "SELECT * FROM Appointments"
    Set rs = New ADODB.Recordset
    rs.Open strSQL, db, adOpenStatic, adLockOptimistic
    End Sub
    Change the connection string to whatever you need and keep the adAsyncConnect option
    Change the Table name to whatever you need

    When you run, the timer will be enabled once the connection is established and every 5 seconds it will query the Table and when the results are available will display them in the Immediate Window. This all happens asynchronously whilst your program may be doing other things.

    EDIT: I see you beat me to it.
    Last edited by Doogle; Aug 30th, 2013 at 01:03 AM. Reason: Missed off the Until rs.EOF

Tags for this Thread

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