?
Solved

How to execute/hold execution of a vbscript program until the same vbscript program ends its previous execution?

Posted on 2008-02-07
13
Medium Priority
?
1,669 Views
Last Modified: 2010-08-05
Hi,
   I am using vbscript to read a txt file and display it using MSComm. I need to hold the execution of vbscript until the previous execution of the same vbscript program ends. First time vbscript program will execute with a set of txt values, Mean while if the same vbscript program is executed with a second set of txt values, I want to hold/put the execution in a queue until the previous execution ends.
Please help me in this case how to proceed.

Thanks

Jerald Silvester
0
Comment
Question by:jeraldsilvester_la
  • 6
  • 4
  • 3
13 Comments
 
LVL 17

Expert Comment

by:ExcelGuide
ID: 20840691
ok, but why dont you create a for loop for 2 executions?

So first he will execute the first one and then after the first one finishes it will start the second one.

If it is not the exact the same code, then you can refer to the second code in the first code...right?

I hope this will help you.

Good luck
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20844462
Hi, Psychotec's option is a good one, try to merge the two scripts together, however, if you don't want to do that, you have a couple of options....

1)  Don't run the second script "explicitly" on it's own.  Have the first script run the second script, at the completion of the first script, by using this:
   Set objShell = CreateObect("WScript.Shell")
   strSecondVBSPath = "C:\Temp\Script2.vbs"
   objShell.Run "wscript """ & strSecondVBSPath & """", 1, True

2)  Use a third script to call the two in sequence, using:
   Set objShell = CreateObect("WScript.Shell")
   strFirstVBSPath = "C:\Temp\Script1.vbs"
   strSecondVBSPath = "C:\Temp\Script2.vbs"
   objShell.Run "wscript """ & strFirstVBSPath & """", 1, True
   objShell.Run "wscript """ & strSecondVBSPath & """", 1, True

It is the True on the end of the Run statement that will make the script wait for that command to complete.

Regards,

Rob.
0
 

Author Comment

by:jeraldsilvester_la
ID: 20848190
Hi,
    No, I am using only one script(disp.vbs) which takes a text file(a1.txt). This displays the content in digital display board. Say this takes about 2 minutes. Meanwhile If user tries to execute the same script(disp.vbs) with the second set of values in another text file(a2.txt), I want to hold the second call of this script or put this in queue until the first gets executed. Currently I get PortOpen error (Err=8005).
Hope I've delivered my issue in a better way now.. :-)

Thanks & Regards,
Jerald
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 17

Expert Comment

by:ExcelGuide
ID: 20848397
you should work with error handlers, something like

If Err.Number = 8005 then
Msgbox "Another script is working, waiting for 2 minutes..."
Application.Wait Now + TimeValue("00:02:00")
'Refer to your macro here, like
Run "YourMacro"
end if

Good luck
0
 
LVL 17

Expert Comment

by:ExcelGuide
ID: 20848403
owww btw look which line give the error and put this code above:

On Error GoTo Err1:
...your code...

If Err.Number = 8005 then
Msgbox "Another script is working, waiting for 2 minutes..."
Application.Wait Now + TimeValue("00:02:00")
'Refer to your macro here, like
Run "YourMacro"
end if

Good luck
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20848618
Psychotec is right.  When you run the script a second time, you should get a line number where the error occurs.....
Let's say, for example, the error is on line 23, which might be
objPorts.Open 2254

So, you can put
On Error Resume Next
objPorts.Open 2254
If Err.Number <> 0 Then
   MsgBox "There was an error getting the port. Try the script again later."
   wscript.Quit
End If
On Error GoTo 0
' rest of your code goes here.


Regards,'

Rob.
0
 

Author Comment

by:jeraldsilvester_la
ID: 20856924
Yes Friends..You both gave the soln which I've arrived to.
But, In my case, I cannot show msg to the user and make them wait until the previous display ends.
I want the second execution to wait till the first gets executed and then start executing the second one by itself. Any thoughts?????

Thanks & Regards,
Jerald Silvester
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20871924
Hmmm, for this you'd need to find out if any wscript.exe processes are running, and wait for them to finish.  I will try to find out if we can see which file a process has open.

Regards,

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20880281
Actually, thinking about it overnight (not losing sleep over it though), all you need to do is wait for there to only be one wscript.exe process running, because that will the current script, waiting for others to finish....this should do that....

'=================
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery ("Select Name from Win32_Process WHERE Name='wscript.exe'")

If colProcess.Count > 1 Then MsgBox "A script is already running. Waiting for it to finish..."

While colProcess.Count > 1
      WScript.Sleep 2000
      Set colProcess = objWMIService.ExecQuery ("Select Name from Win32_Process WHERE Name='wscript.exe'")
Wend

MsgBox "This script will now run."

' Other stuff

MsgBox "Finished."
'=================

Regards,

Rob.
0
 

Accepted Solution

by:
jeraldsilvester_la earned 0 total points
ID: 21085670
Hi friends,
 I've solved this by checking the file existancy. as i was reading from a file for the contents to be displayed. so, I check for file existancy and proceed with the next call.
Thank you all for your post/suggestion.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21091641
Fair enough. Would you be able to post a request to have this question PAQed or deleted?

Thanks,

Rob.
0
 

Author Comment

by:jeraldsilvester_la
ID: 21149687
Yes I request you to do so.

Thanks,
Jerald
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 21156853
Jerald, you should be able to accept your own comment as the answer.  Is there a button for you to accept your comment?

Rob.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Progress

592 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question