| View previous topic :: View next topic |
| Author |
Message |
derSturm Newbie
Joined: 13 Jan 2004 Posts: 4
|
Posted: Jan 13th, 2004 02:26 AM Post subject: Sort/Shell *beep*...p! |
|
|
I'm new to Visual Basic. I bought a book that came with 6.0 Working Model Edition, which is 6.0 minus executable compilation and- worse- the Help file. Any help will be greatly appreciated.
I'm working on a program that requires the user-inspired sorting of 'fielded' file data. To accomplish this, I wrote a one-line QBasic "SHELL "SORT " + Command$" program and compiled it into an executable. I call upon this program via Visual Basic's "Shell" command. (The same result could be accomplished by creating a batch file, and calling upon it in the same manner.) The problem? Visual Basic executes the lines following the Shell command before the Shell'ed program is finished executing. The big problem? The line following the Shell command calls for the opening of the object (sorted) file. In other words, Visual Basic is reading from the sorted file before the file is finished being sorted. It is consistently finding only five lines when it should be finding (currently) twenty.
I have tried "Do: Loop Until Len(Dir$("FILE"))" to no avail. Obviously, the file shows up in the directory before it is finished being written. Is there any way to instruct Visual Basic to wait until the Shell'ed program is finished before continuing with its own execution (without calling upon the user to manually exit the Shell'ed program's window [I'm happy using "vbHide"])? A loop delay is one solution, but it causes way too long an unnecessary delay. Another, and better, solution comprises the following code:
Do
tempInteger = 0
Open "FILE" For Input As #1
While Not Eof(1)
Line Input #1, tempString
tempInteger = tempInteger + 1
Wend
Close 1
Loop Until tempInteger = Total
For present and future reference, is there a better way to do it than this? I would really like to understand Visual Basic's "Shell" command better. A more conducive means of sorting file data would be appreciated information, as well.
I'm running at 1 GHz under 98SE.
:rolleyes: |
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Jan 13th, 2004 08:28 AM Post subject: |
|
|
Try this
| Code: | Option Explicit
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400
Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0) As Boolean
Dim lInst As Long
Dim lStart As Long
Dim lTimeToQuit As Long
Dim sExeName As String
Dim lProcessId As Long
Dim lExitCode As Long
Dim bPastMidnight As Boolean
On Error GoTo ErrorHandler
lStart = CLng(Timer)
sExeName = ExeFullPath
'Deal with timeout being reset at Midnight
If TimeOutValue > 0 Then
If lStart + TimeOutValue < 86400 Then
lTimeToQuit = lStart + TimeOutValue
Else
lTimeToQuit = (lStart - 86400) + TimeOutValue
bPastMidnight = True
End If
End If
lInst = Shell(sExeName, vbMinimizedNoFocus)
lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
Do
Call GetExitCodeProcess(lProcessId, lExitCode)
DoEvents
If TimeOutValue And Timer > lTimeToQuit Then
If bPastMidnight Then
If Timer < lStart Then Exit Do
Else
Exit Do
End If
End If
Loop While lExitCode = STATUS_PENDING
ShellandWait = True
ErrorHandler:
ShellandWait = False
Exit Function
End Function
Private Sub Form_Load()
ShellandWait "notepad.exe"
MsgBox "finished"
End Sub |
_________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
derSturm Newbie
Joined: 13 Jan 2004 Posts: 4
|
Posted: Jan 13th, 2004 09:24 PM Post subject: |
|
|
Wow! That's way over my head! Calling upon "kernel32" and all. I tried it, and it works very nicely. It makes me feel really ignorant, though, not knowing what the heck it's doing!
Do you have any suggestions as to what reference material I should attain? I'd like to know more about Windows' built-in libraries, as well as Visual Basic specifically.
First, of course, I've got to get a "real" version of VB... one that comes with a Help file! |
|
| Back to top |
|
derSturm Newbie
Joined: 13 Jan 2004 Posts: 4
|
Posted: Jan 13th, 2004 09:26 PM Post subject: |
|
|
| Oh, and Thanks! |
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Jan 14th, 2004 03:12 AM Post subject: |
|
|
You're welcome As for API reference I suggest you visit http://www.mentalis.org/index2.shtml where there is the API-List and you can also download the API-Guide and the APIViewer  _________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Jan 14th, 2004 10:31 AM Post subject: |
|
|
Or look at it here : http://msdn.microsoft.com  _________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
derSturm Newbie
Joined: 13 Jan 2004 Posts: 4
|
Posted: Jan 15th, 2004 12:15 AM Post subject: |
|
|
Cool!
Some really great information! I'll be coming here in the future.
Thanks to both of you. |
|
| Back to top |
|
P.T.A.M. Administrator

Joined: 08 Oct 2003 Posts: 752 Location: Greece
|
Posted: Jan 16th, 2004 06:06 PM Post subject: |
|
|
I'm glad to hear you'll be coming back Looking forward to seeing you again  _________________ No one is completely useless. They can at least be an example of what to avoid. |
|
| Back to top |
|
|