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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait before opening next Shel 2

Status
Not open for further replies.

DBSSP

Programmer
Apr 1, 2002
327
US
Again, the mystikal magic of VB is eluding my presence (I swear there's some little gremlin in my machine laughing at me). Anyway, I coded applications to open and run, and I was using a halfa$$ counter to make the application wait before executing the next line(I know, "bad programmer, bad!").
This was making sure the first application was open and ready to before giving a set of commands from a prompt.

Below is my code. Does anybody have a way to make the program wait? Also, closing shells seems to be out of my grasp at the moment.

***BEGIN CODE***

Function ScriptCtcl3_()

Dim XL As Excel.Application
Dim Wkb As Workbook
Dim strCmdLine As String
Dim strEmulator As String

'Set Parameters for command lines and shells
'The switch "/K" can be replaced with "/C" to automatically
'close the opened program when the program has executed.

strCmdLine = "cmd.exe /C commandline to run"
strEmulator = "pcsws session.ws /K"


Set XL = CreateObject("Excel.Application")
With XL
.Visible = True
Set Wkb = .Workbooks.Open(Filename:="v:\cms\access\agent\import\l3current.xls", UpdateLinks:=True)
End With

'The following 3 lines allow time for spreadsheet to open
'and update

Make next command wait a few seconds
Do While (x < 5500000)
x = x + 1.5
Loop

Call Shell(strEmulator, vbNormalFocus)

Make next command wait a few seconds so the above shell can open

Do While (x < 5500000)
x = x + 1
Loop


Call Shell(strCmdLine, vbNormalFocus)

I would like to be able to close everything after the emulator script has finished running

End Function Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
Jay,

You can use the following setup to add a delay into your procedure (not dependent on the pc's processor speed as is the For..Next loop):

Code:
Sub ProcessWithDelay()
Dim StartTime as Single

  'First process here; e.g. open Excel file

  StartTime = Timer
  Do Until Timer < StartTime + DelayTime
    DoEvents  'Allows OS to run other processes
  Loop

  'Second process here; e.g. one that depends on Excel file being open

End Sub

DelayTime can be a constant, variable, or literal set to the number of seconds you want to delay. Keep in mind, this is a somewhat kudgy approach as it forces you to guess at how much time is required. It would be better to test for the existence of some attribute of the process you are waiting for to complete.

Re: &quot;Closing Shells&quot; -- Not sure what you mean here. Which shell isn't closing? You can close the Excel instance in your posted code by using
Code:
XL.Quit


Regards,
Mike
 
Hey, thanks! What I mean by closing the shells, is closing the called shell for the emulator after it has completed it's procedure....I think I have an idea there though. Thank you very much!

Also, what do if I use multiple instances of this timer? Do I have to reset and set the delay time? Or do I do something else to reset it in it's next instance? Jay [infinity]
&quot;If the words up and down were reversed, would you trip and fall or trip and fly?&quot;
 
Jay,

I tend to use named constants (rather than literals) so here is an example using your three processes and constants:


Code:
Const Process1_Delay = 3
Const Process2_Delay = 5


Sub ProcessWithDelay()
Dim StartTime as Single

  'First process here

  StartTime = Timer
  Do Until Timer < StartTime + Process1_Delay
    DoEvents
  Loop

  'Second process here

  StartTime = Timer
  Do Until Timer < StartTime + Process2_Delay
    DoEvents
  Loop

  ' Third process here

End Sub


Hope this is what you were looking for.

Regards,
Mike
 
Shell can be fun, given it doesn't return a code

What I'd (ab)normaly do there is write out a batch file, the 1st line of which creates a temporary file, does what its gotta do, and the last action is to delete itself.

Very, rudimentary semaphores.

Have your code wait for the destruction of the temporary file, and when its deleted its safe to proceed, given that your batch file has terminated normally

Just a thought

HTH

PaulTEG
 
Paul, you are a legend [medal]

I love solutions that don't involve 5000 lines of code - have a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top