|
-
Feb 13th, 2006, 02:50 PM
#1
Thread Starter
Lively Member
Resolved - Need a cancel button
I need a cancel button for a program which goes into this loop searching the entire hard drive. It can be a lengthy process waiting for it to stop on its own. Where do I put the code and what code is best to use?
VB Code:
Private Function SearchForFiles(FP As FILE_PARAMS) As Double
'local working variables
Dim WFD As WIN32_FIND_DATA
Dim hFile As Long
Dim nSize As Long
Dim sPath As String
Dim sRoot As String
Dim sTmp As String
sRoot = QualifyPath(FP.sFileRoot)
sPath = sRoot & "*.*"
'obtain handle to the first match
hFile = FindFirstFile(sPath, WFD)
'if valid ...
If hFile <> INVALID_HANDLE_VALUE Then
'This is where the method obtains the file
'list and data for the folder passed.
'
'GetFileInformation function returns the size,
'in bytes, of the files found matching the
'filespec in the passed folder, so it is
'assigned to nSize. It is not directly assigned
'to FP.nFileSize because nSize is incremented
'below if a recursive search was specified.
nSize = GetFileInformation(FP)
FP.nFileSize = nSize
Do
'if the returned item is a folder...
If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
'..and the Recurse flag was specified
If FP.bRecurse Then
'remove trailing nulls
sTmp = TrimNull(WFD.cFileName)
'and if the folder is not the default
'self and parent folders...
If sTmp <> "." And sTmp <> ".." Then
'..then the item is a real folder, which
'may contain other sub folders, so assign
'the new folder name to FP.sFileRoot and
'recursively call this function again with
'the ammended information.
'
'Since nSize is a local variable, whose value
'is both set above as well as returned as the
'function call value, the nSize needs to be
'added to previous calls in order to maintain accuracy.
'
'However, because the nFileSize member of
'FILE_PARAMS is passed back and forth through
'the calls, nSize is simply assigned to it
'after the recursive call finishes.
FP.sFileRoot = sRoot & sTmp
nSize = nSize + SearchForFiles(FP)
FP.nFileSize = nSize
End If
End If
End If
'continue looping until FindNextFile returns
'0 (no more matches)
Loop While FindNextFile(hFile, WFD)
'close the find handle
hFile = FindClose(hFile)
End If
'because this routine is recursive, return
'the size of matching files
SearchForFiles = nSize
End Function
Thanks in advance!
Last edited by imperialdata; Feb 15th, 2006 at 07:55 AM.
Reason: Resolved
-
Feb 13th, 2006, 02:56 PM
#2
-
Feb 13th, 2006, 03:16 PM
#3
Thread Starter
Lively Member
Re: Need a cancel button
Excuse my ignorance on this one, I get a 'Variable not defined' error.
If a Function is Private and I Dim a variable in the main form code, then do I have to dim it again in the function?
Thanks for tidying up the code with the tags, I'll do that next time.
-
Feb 13th, 2006, 03:49 PM
#4
Frenzied Member
Re: Need a cancel button
You can use also DoEvents
VB Code:
'Declare on general section
Public bCancel As Boolean
bCancel = False
Do
DoEvents
'....
If bCancel Then Exit Do
Loop While FindNextFile(hFile, WFD)
Private Sub cmdCancel_Click()
bCancel = True
End Sub
oh1mie/Vic

-
Feb 13th, 2006, 03:51 PM
#5
Re: Need a cancel button
Depending on the code happening in the Do...Loop the DoEvents could slow the whole thing down quite a bit, so it might be worth sticking in a counter and only firing DoEvents every nth loop.
-
Feb 13th, 2006, 03:56 PM
#6
Re: Need a cancel button
as oh1mie stated:
bCancel is declared in the General Declarations section of the form...
unless this function is running in a module.. then u will need to put the bCancel as publice in the mod..
Public bCancel as Boolean
then in the click event of the cance button set the bCancel = True
be sure to set bCancel back to false after its done "canceling"
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 13th, 2006, 04:22 PM
#7
Thread Starter
Lively Member
Re: Need a cancel button
The code is not in a module, just a private function but I still get the 'Variable not defined' error despite having Dimmed it in the Form Load area
-
Feb 13th, 2006, 04:26 PM
#8
Re: Need a cancel button
and its highlighting bCancel??
and.. not the form load... u want it in the General Declarations section...
thats ALL the way at the top.. above all other subs..
Left dropdown in the code window will say (General) and the right dropdown will say (Declarations)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 13th, 2006, 04:31 PM
#9
Frenzied Member
Re: Need a cancel button
 Originally Posted by imperialdata
The code is not in a module, just a private function but I still get the 'Variable not defined' error despite having Dimmed it in the Form Load area
If you dim variable on form load section, it works only on the form load prosedure.
Move it to the general section to works in all prosedures on the that form
oh1mie/Vic

-
Feb 13th, 2006, 05:10 PM
#10
Thread Starter
Lively Member
Re: Need a cancel button
Thanks for that guys. As someone who comes back to VB after a long time not doing anything, the basics can get a bit rusty.
Tried what you suggested and got 'Invalid outside procedure' error on this code (where I put it in the General Declarations area as you said). The word false was highlighted:
VB Code:
Option Explicit
'Cancel button
Dim bCancel As Boolean
'Set cancel button to false
bCancel = False
-
Feb 13th, 2006, 05:14 PM
#11
Re: Need a cancel button
if the variable is in the general declarations section then you should declare it 'Private bCancel As Boolean'.
It looks like you probably have bCancel = False outside of any procedures, but it is unnecessary to set it to false because it defaults to that anyway.
-
Feb 13th, 2006, 06:22 PM
#12
Thread Starter
Lively Member
Re: Need a cancel button
Removing bCancel = false allowed me to get past the error but cancel button has no effect. It seems to have no focus.
Last edited by imperialdata; Feb 13th, 2006 at 06:26 PM.
-
Feb 14th, 2006, 01:53 AM
#13
Frenzied Member
Re: Need a cancel button
 Originally Posted by imperialdata
Removing bCancel = false allowed me to get past the error but cancel button has no effect. It seems to have no focus.
Did you forget DoEvents?
oh1mie/Vic

-
Feb 14th, 2006, 06:16 PM
#14
Thread Starter
Lively Member
Re: Need a cancel button
Yup, wasn't sure where it went, had it in the wrong part of the code. Cancelling seems fine now but as Bushmobile said, it has slowed the whole thing down. I'm checking for files across large hard drives and searching is very slow.
How would I fire the Do Events every, say, 10th loop?
-
Feb 15th, 2006, 01:52 AM
#15
Frenzied Member
Re: Need a cancel button
 Originally Posted by imperialdata
Yup, wasn't sure where it went, had it in the wrong part of the code. Cancelling seems fine now but as Bushmobile said, it has slowed the whole thing down. I'm checking for files across large hard drives and searching is very slow.
How would I fire the Do Events every, say, 10th loop?
VB Code:
Dim iCount As Integer
Do
iCount = iCount + 1
If iCount = 10 Then
DoEvents
iCount = 0
End If
Loop While FindNextFile(hFile, WFD)
oh1mie/Vic

-
Feb 15th, 2006, 03:42 AM
#16
Re: Need a cancel button
if you have a loop like this with doevents in it, you need to be careful that someone
doesn't close your program while you are running the loop. It will continue to run even though the main form is closed. Since doevents has a return value equal to the number of visible forms, you should change the code to something like this:
'this code will call doevents once for every 10 counts
DIM Cnt as Integer 'place at beginning of sub
'then in your loop (in fact, put this right before the Loop keyword)
Cnt = Cnt + 1
if Cnt = 10 then
cnt = 1
if doevents() = 0 then exit Do ' or exit Loop, depending if you do cleanup routines
' in this case, you still need to close a file, so exit do will do...
end if
I hope this helps...
-
Feb 15th, 2006, 07:54 AM
#17
Thread Starter
Lively Member
Re: Need a cancel button
Thanks guys, excellent help.
-
Feb 15th, 2006, 07:55 AM
#18
Re: Resolved - Need a cancel button
If this is resolved, please pull down the Thread Tools menu and click the Mark Thread Resolved button. That will let everyone know that you have your answer.
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|