dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] VB6: Software freezes while downloading a file from web!

  1. #1

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

    Resolved [RESOLVED] VB6: Software freezes while downloading a file from web!

    Hi.
    I have a big software already running in many companies.
    As a measure of security, I want to add to it a module that checks in a website (ftp with username & password) if I want the program to block it self, or send messages to user, or update it self, etc.

    It download the file perfectly! The problem is it takes a little while to do it (like 3-5 seconds, and if it's offline, like a minute) and during it, the whole software freezes until it ends. I tried moving the routine to another form, but it's the same. I also thought of moving it to an independent exe that triggers when the main one runs, but nowadays Windows is so paranoic it would be very caotic (sandbox, firewall...) it has to do it quietly.

    Any suggestions? I'm thinking of doing it when the program closes, or when it's inactive for 1 minute, but still, it's awkward and it just can't freeze like that, my clients are going to report it to me. TO SUM UP, I need it to do it quietly, silently, in background, no freezing (or at least that if it's offline, doesn't freeze for 1 minute).


    The code I use is:

    Code:
    Option Explicit
    Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
    Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
    
    Function InternetGetFile(sURLFileName As String, sSaveToFile As String, Optional bOverwriteExisting As Boolean = False) As Boolean
        Dim lRet As Long
        Const S_OK As Long = 0, E_OUTOFMEMORY = &H8007000E
        Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000
        Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3
        Const INTERNET_FLAG_RELOAD = &H80000000
        
        On Error Resume Next
        'Create an internet connection
        lRet = InternetOpen("", INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)   '---> in this line it freezes and I can't do anything
        
        If bOverwriteExisting Then
            If Len(Dir$(sSaveToFile)) Then
                VBA.Kill sSaveToFile
            End If
        End If
        'Check file doesn't already exist
        If Len(Dir$(sSaveToFile)) = 0 Then
            'Download file
            lRet = URLDownloadToFile(0&, sURLFileName, sSaveToFile, 0&, 0)
            If Len(Dir$(sSaveToFile)) Then
                'File successfully downloaded
                InternetGetFile = True
            Else
                'Failed to download file
                If lRet = E_OUTOFMEMORY Then
                    EscribeLog "Error serv: The buffer length is invalid or there was insufficient memory to complete the operation."
                Else
                    EscribeLog "Error serv: (this is probably a proxy server error)." 'Escribelog is a function on my own, it's "WriteLog" in spanish
                End If
                InternetGetFile = False
            End If
        End If
        On Error GoTo 0
        
    End Function
    and I call it this way:
    Code:
    Public Sub RevWeb()
        On Error Resume Next
        Dim ppArch As String, pp As String, sn As String, accion As String, param As String, pplocal As String
        ppArch = App.Path + "\" + Format(Date + Time, "ddmmyyhhmmss")
        If InternetGetFile("adress/file.txt", ppArch, True) Then  '---> in this line it freezes!
            'it opens and processes the content of the file, now in hard disk
        End If
    End Sub
    THANK YOU VERY MUCH.
    PS: English is not my native language, please excuse my grammar or spelling errors.
    Last edited by IHappenToBe'; Dec 13th, 2012 at 12:08 PM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,513

    Re: VB6: Software freezes while downloading a file from web!

    if you can use a shell object for downloading it runs asyncronoulsy

    also there is an example in code bank that uses an activex exe (.ocx) to do this
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

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

    Re: VB6: Software freezes while downloading a file from web!

    Quote Originally Posted by westconn1 View Post
    if you can use a shell object for downloading it runs asyncronoulsy

    also there is an example in code bank that uses an activex exe (.ocx) to do this
    First of all, thank you for your answer.
    ...I looked for it but I can't find that example, sorry.
    Also I don't understand what you mean by "shell object".

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: VB6: Software freezes while downloading a file from web!

    Are you sure the InternetOpen function is necessary? The URLDownloadToFile function was able to successfully download a small zip attachment from this forum all by itself. However, it did freeze the Form and the IDE while downloading. That's probably due to not passing parameters to pCaller and lpfnCB. If a callback and an IUnknown interface was provided, the UI might not have frozen.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: VB6: Software freezes while downloading a file from web!

    See Download files from the Web (No Winsock) for a UserControl approach to downloading files.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,513

    Re: VB6: Software freezes while downloading a file from web!

    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

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

    Re: VB6: Software freezes while downloading a file from web!

    Quote Originally Posted by westconn1 View Post
    Waw, problem SUPER-solved!
    That was exactly what I needed. Thank you so much!

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
  •  



Featured


Click Here to Expand Forum to Full Width