Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » General

Post new topic   Reply to topic
Sort/Shell *beep*...p!
View previous topic :: View next topic  
Author Message
derSturm
Newbie


Joined: 13 Jan 2004
Posts: 4

PostPosted: Jan 13th, 2004 02:26 AM    Post subject: Sort/Shell *beep*...p! Reply with quote

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
View user's profile Send private message Send e-mail
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 13th, 2004 08:28 AM    Post subject: Reply with quote

Try this Wink

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
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
derSturm
Newbie


Joined: 13 Jan 2004
Posts: 4

PostPosted: Jan 13th, 2004 09:24 PM    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
derSturm
Newbie


Joined: 13 Jan 2004
Posts: 4

PostPosted: Jan 13th, 2004 09:26 PM    Post subject: Reply with quote

Oh, and Thanks!
Back to top
View user's profile Send private message Send e-mail
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 14th, 2004 03:12 AM    Post subject: Reply with quote

You're welcome Smile 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 Wink
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Jan 14th, 2004 07:23 AM    Post subject: Reply with quote

Hi!

There's no real help file for visual basic. Instead of help file microsoft sell's MSDN..which contains all the necessary help required for Visual Basic and their other languages.

So you just need to get the MSDN Library.

Thanks!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 14th, 2004 10:31 AM    Post subject: Reply with quote

Or look at it here : http://msdn.microsoft.com Smile
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
derSturm
Newbie


Joined: 13 Jan 2004
Posts: 4

PostPosted: Jan 15th, 2004 12:15 AM    Post subject: Reply with quote

Cool!

Some really great information! I'll be coming here in the future.

Thanks to both of you.
Back to top
View user's profile Send private message Send e-mail
P.T.A.M.
Administrator


Joined: 08 Oct 2003
Posts: 752
Location: Greece

PostPosted: Jan 16th, 2004 06:06 PM    Post subject: Reply with quote

I'm glad to hear you'll be coming back Smile Looking forward to seeing you again Smile
_________________
No one is completely useless. They can at least be an example of what to avoid.
Back to top
View user's profile Send private message Send e-mail Visit poster's website ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » General All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic