Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I refresh a form when it loose's focus 2

Status
Not open for further replies.

AccessGuruCarl

Programmer
Jul 3, 2004
471
US
Hello All,

I have a form the uploads files using FTP...

The file sizes average about 300MB!

If I minimize the form, or the screensaver kicks in, when I restore the form or return from the screensaver, the form is completely white!

If tried setting a booloean value to test if an upload is in progress, and if so on form got focus I used Me.Refresh...

I didn't work.... What do I need to refresh the form view after it looses focus and regains focus!


Thanks,
Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Forgot to mention....

When this form gets focus back...

If I use the Task Manager...
It tells me the app is not responding.... But if I wait... The upload does complete, then I get the form view back and task manager reports the app as running...

Just FYI.... If it matters.....

Thanks again,
Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Er..."this behavior is by design."

What's happening is that stuff that has to be done by the form won't get done while the thread is running other stuff. So, when you download, and then something else paints over your form, the form won't repaint itself until you're done downloading.

If you want to be able to handle events in the middle of what you're doing, investigate the GetChunk method. This will allow you to pull your file in chunks, and do things like repaint forms, update progress bars, and the like between chunks.

HTH

Bob
 
Hi Bob,

I was trying to use a GetChunk method for what I thought looked like fairly good code... But it kept failing...
Would return error codes of 12002 and 12030
InternetWriteFileErrorCode --> Connection was terminated abnormally...

Of course when I test it on smaller files, and step through the code it never fails.... Anything over 100MB and things start to go bad...

Heres what I was working with, maybe you can spot something wrong...

Class Module - cFTP
Code:
Public Function FTPUploadFile(sLocal As String, sRemote As String) As Boolean
    Dim Data(BUFFERSIZE - 1) As Byte
    Dim Written As Long
    Dim Size As Long
    Dim Sum As Long
    Dim lBlock As Long
    
    Sum = 0
    lBlock = 0
    sLocal = Trim(sLocal)
    sRemote = Trim(sRemote)
    
    If sLocal <> "" And sRemote <> "" Then
      hFile = FtpOpenFile(hConnection, sRemote, GENERIC_WRITE, dwType, 0)
      If hFile = 0 Then
          ErrorOut Err.LastDllError, "FtpOpenFile:PutFile"
          FTPUploadFile = False
          Exit Function
      End If
      
      Open sLocal For Binary Access Read As #1
      Size = LOF(1)
      For lBlock = 1 To Size \ BUFFERSIZE
          Get #1, , Data
          If (InternetWriteFile(hFile, Data(0), BUFFERSIZE, Written) = 0) Then
              FTPUploadFile = False
              ErrorOut Err.LastDllError, "InternetWriteFile"
              Exit Function
          End If
          DoEvents
          Sum = Sum + BUFFERSIZE
          RaiseEvent FileTransferProgress(Sum, Size)
      Next lBlock
      
      Get #1, , Data
      If (InternetWriteFile(hFile, Data(0), Size Mod BUFFERSIZE, Written) = 0) Then
          FTPUploadFile = False
          ErrorOut Err.LastDllError, "InternetWriteFile2"
          Exit Function
      End If
      
      Sum = Sum + (Size Mod BUFFERSIZE)
      Size = Sum
      RaiseEvent FileTransferProgress(Sum, Size)

      Close #1
      InternetCloseHandle (hFile)
      FTPUploadFile = True
   End If

End Function

Form -- Progress
Code:
Public Sub mFTP_FileTransferProgress(lCurrentBytes As Long, lTotalBytes As Long)
On Error Resume Next
Dim j As Long
Dim j2 As Long
TransferRate = Format(Int(lCurrentBytes / (Timer - BeginTransfer)) / 1000, "####.00")
    prgStatus.Max = lTotalBytes
    prgStatus.Min = 0
  j = prgStatus.Value
  j2 = prgStatus.Value \ 1024
  DoEvents
        prgStatus.Value = lCurrentBytes
        DoEvents
        prgStatus.ToolTipText = prgStatus.Value & " Bytes of " & prgStatus.Max & " Bytes Transfered"
        DoEvents
        Label7.Caption = prgStatus.Value \ 1024 & " KB of " & prgStatus.Max \ 1024 & " KB Transfered"
        DoEvents
        Label9.Caption = Format$(CLng((j / prgStatus.Max) * 100)) + "%"
        DoEvents
        Label10.Caption = Format(TransferRate, "##.#0#") & " Kbps"
        Label11.Caption = "Estimated Time Left: " & ConvertTime(Int(((prgStatus.Max - prgStatus.Value) / 1024) / TransferRate))
        If prgStatus.Value = prgStatus.Max Then
        Label9.Caption = "100%"
        End If
End Sub

Form - Call to upload
Code:
fFTP = False
Set mFTP = New cFTP
   mFTP.SetModeActive
   mFTP.SetTransferBinary

Dim lTimer As Long
Dim strRemote As String
Dim strLocal As String
Dim strAVICopy As String
'Update the status label and Open the db to run the update
    lblStatus.Caption = "Status:" & Chr(10) & "    Opening FTP connection..."
    Me.Refresh
    strAVICopy = App.Path & "\avis\upload.avi"        picLogo.Visible = False
    Animation1.Visible = True
    Animation1.Open strAVICopy
    Animation1.Play
    
BeginTransfer = Timer
lTimer = Timer
strRemote = "2MB.ftp"  'Text4.Text & "/" & Text5.Text
strLocal = App.Path & "\2MB.ftp"    ' & Text5.Text

'Open FTP connection, set directory to default
If mFTP.OpenConnection(gblServer, gblServerLogin, gblServerPassword) Then
  mFTP.SetFTPDirectory "/"
'Change the status label
  lblStatus.Caption = "Status: " & Chr(10) & Chr(10) _
    & "Uploading file..." & Chr(10) & strLocal
    
'Show the progress bar, and set it to the left edge of the form
prgStatus.Left = 150
prgStatus.Visible = True
'Upload the Client Update File
If Not mFTP.FTPUploadFile(strLocal, strRemote) Then
lblStatus.Caption = "Status:    Error"
MsgBox mFTP.GetLastErrorMessage
Exit Function
Else
DoEvents
End If
'Upload the Setup File
strRemote = "/" & ReadINIValue(gblINIPath, gblCIPakNumber, "ExeName") & ".exe"
strLocal = gblCIOutput

lblStatus.Caption = "Status: " & Chr(10) & Chr(10) _
        & "Uploading file..." & Chr(10) & strLocal


        If Not mFTP.FTPUploadFile(strLocal, strRemote) Then
          lblStatus.Caption = "Status:     Error"
          MsgBox mFTP.GetLastErrorMessage
          Exit Function
        Else
          DoEvents
        End If

    DoEvents

    Animation1.Stop
    Animation1.Visible = False
    picLogo.Visible = True
    Label7.Caption = ""     'KB transferred
    Label9.Caption = ""     'Percentage
    Label10.Caption = ""    'Transfer Rate
    Label11.Caption = ""    'Estimated Time Reamining

    lblStatus.Caption = "Status: " & Chr(10) & "    Upload complete..." _
        & Chr(10) & "    Closing connection..."

    mFTP.CloseConnection
    prgStatus.Visible = False

    fFTP = True
End If

End Function

It seems weird that everything works fine for smaller files, but not large! Yet if I use the Simple PutFile without any progress it works on the large files!

Thanks for the post, I thought that was going to be the answer I got back, I was hoping for another solution!

Let me know if you see anything wrong with this...
Thanks,
Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Check out the "DoEvents" statement. It might slow your program a bit though.

 
Your cFTP class is something I've never heard of. Maybe there's something going on with that? You might want to take a look at thread708-970319 too.

HTH

Bob
 
>Check out the "DoEvents" statement

The code is already littered with DoEvents. Rather too many in fact in the mFTP_FileTransferProgress function
 
I thought there was quite a few of them...(DoEvents)

Not sure really how DoEvents works...
Help file doesn't give you much on this event!
Other than warn you about it's usage...

Should it be before the event I want or after it?

I'll remove all but the first and last DoEvents in the mFTP_FileTransferProgress function and try one at a time to see if I can get it to fail...

Thanks for the input....
Carl




AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
I'd get rid of all of the current ones in that faunction, and add one new one just before End Sub
 
Thanks for the help....

I removed all and placed at end of sub, as suggested...

I also removed the DoEvents in the call to the FTP at the end of the sub just before the resetting of the textboxes and stopping the animation...

It successfully uploaded 1 230MB file and another 320MB...

I'll test it a few more times, and hopefully it doesn't crash out when I bring it to the end-user!

Thanks again....
Carl

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
To clarify (a bit) what DoEvents does:

Event handlers sometimes execute sequentially, and sometimes an event handler triggers another event handler. When this happens, the latter code can get placed in a queue and not be executed until the first event handler is done. DoEvents basically says to flush the event handler queue.

I don't believe it's a good idea to use DoEvents unless you are well aware of what events DoEvents will be doing. The idea of "at this point in the code, use DoEvents to execute any event code that might be there" is an unstable one, and it appears that the above code takes this approach.

Bob
 
Thanks for the clarification...

I gave a star to both you and strongm....

AccessGuruCarl
Programmers helping programmers
you can't find a better site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top