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

How to stop a double vbscript from executing. 1

Status
Not open for further replies.

JonMa

Programmer
Jul 16, 2003
69
NO
Hello.
I use WScript.Sleep(iant) to execute a vbscript endlessly.
However if the same vbscript is started twice, then everything becomes destroyed.
Is there a good way to throw out the second vbscript ?
Hope anyone can help.
 
The easiest way I can think of is to create a flag file (empty text file) in the same folder as the script when the script starts and delete it when the script ends. Then at the beginning of the script, check if the file exists and exit if it does.

Hope this helps.


Glen Appleton

VB.Net student.
 
Hello again.
There has to be a better way, because the script ends only when you cancel it and then you have to manually delete the file before you can start all over.
Hope somebody can help.
 
Hello JonMa,

[1] Why do you execute "endlessly" a vbs with .sleep?
[2] What do you mean by "everything becomes destroyed"?

regards - tsuji
 
Hello tsuji.
1. Endlessly because it checks a web site for ftp-downloads.
2. Because I download files messing up with each other.

I think there should be possible with som kind of event handling and synchronizing them.

Hope you can help.
 
Hello JonMa,

Not that I am not willing to help or anything being cynical, but I am not at all understood what is behind your question. Is it the .sleep being used legitimately for checking periodically whether the download has finished? Is yes, why two scripts instances being processed would "break" the proper functionality at all?

The idea comes straight to me is to enumerate the process and kill the unwelcome wscript process. (But, in passing, why would you think the same vbs would be fired twice or more time at all?) You see I am hopelessly at a lost as to the motivation and cause and observed behavior. Maybe some other members can step in in good time.

regards - tsuji
 
Hello again.
Do you know how to enumerate the process and kill the unwelcome wscript process ?
Regards Jon.
 
JonMa,

This is how you terminate/kill a process of notepad.
Code:
sApp="notepad.exe"
set svc=getobject("winmgmts:root\cimv2")
set cProcess=svc.execQuery ("select * from win32_process")
for each oProcess in cProcess
	if Instr(1,oProcess.caption,sApp,1)>0 then
		wscript.echo "you find one"
		lRet=oProcess.terminate()
		if lRet<>0 then
			wscript.echo &quot;Failed to terminate the process.&quot;
		else
			wscript.echo &quot;Process successfully terminated.&quot;			
		end if
	end if
next
set cProcess=nothing
Now, if you put sApp=&quot;wscript.exe&quot; you could terminate (if permitted, but this is just a detail) the wscript process. But, it might terminate the good one as well.

So you have to discern good one from the bad. But that would be a problem. And a good understanding of the exact problem would help.

You can expose the processID from the oProcess object, ie, oProcess.processID. But as you had not controlled it when you create it, so that property does not help a lot.

To have a control of the exact process identifier, processID, you may use create method from win32_process. This leads us too far afield from your original posting.

regards - tsuji
 
Hello tsuji.
This is brilliant.
I will definite use your code.
Thanks a lot.
Regards Jon.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top