visitmeoo
asked on
Errors running program
I have a program that writes to files in a share so it creates/deletes the file and creates/deletes a drive map.
When I run the program step by step (using F8) it works fine. When I run the program all at once with no stops it usualy returns errors regarding the networking commands such as: can't delete map or can't read file. Some times (rairly) the program runs fine.
Have you any idea how to solve it?
When I run the program step by step (using F8) it works fine. When I run the program all at once with no stops it usualy returns errors regarding the networking commands such as: can't delete map or can't read file. Some times (rairly) the program runs fine.
Have you any idea how to solve it?
It sounds like a timing problem. You should check for the existence of a file before you try reading from it, and make sure a drive map exists before trying to delete it.
Try to put doevents between code
ASKER
What are 'doevents' ?
ASKER
Checking the existince of the file doesn't do me any good.
Already now I can see when the file is created and when it is not. When I run it step by step it's created and when I run it all at once it some times creates it and sometimes doesn't.
Already now I can see when the file is created and when it is not. When I run it step by step it's created and when I run it all at once it some times creates it and sometimes doesn't.
DoEvents gives control back to Windows so it can process anything that has been queued while waiting for your program to run.
The syntax is:
'code up to here
'DoEvents inserted to allow windows to catch up with processing
Doevents
'continue code.
You could try inserting doevents immediately following any commands that deal with the network (ie. Mapping a drive). This should allow windows to get the process done before your program tries to access the drive.
Hope this helps.
The syntax is:
'code up to here
'DoEvents inserted to allow windows to catch up with processing
Doevents
'continue code.
You could try inserting doevents immediately following any commands that deal with the network (ie. Mapping a drive). This should allow windows to get the process done before your program tries to access the drive.
Hope this helps.
ASKER
I don't understand exactly how to add the 'doevents' & what they should include. Here is my code, please show me what to add:
Public Sub FindUser(ID As String)
Dim wshShell
Set wshShell = CreateObject("WScript.Shel l")
'map network drive
Dim Connect
Set Connect = CreateObject("WScript.Netw ork")
Connect.MapNetworkDrive "T:", "\\star\SYSVOL"
wshShell.CurrentDirectory = "T:\cs.biu.ac.il\scripts"
'create random file name
Dim fName As String
Dim objFSO1
Set objFSO1 = CreateObject("Scripting.Fi leSystemOb ject")
fName = NewRandomFileName("abcdefg hijklmnopq rstuvwxyzA BCDEFGHIJK LMNOPQRSTU VWXYZ", 11)
Do While objFSO1.FileExists(fName)
fName = NewRandomFileName("abcdefg hijklmnopq rstuvwxyzA BCDEFGHIJK LMNOPQRSTU VWXYZ", 11)
Loop
fName = fName & ".txt"
'execute search
Dim myCommand As String
phoneNo = ID
myCommand = "cscript search.vbs LDAP://DC=ch,DC=cs,DC=biu, DC=ac,DC=i l /C:telephoneNumber=" & phoneNo & " /S:subtree"
wshShell.Run "cmd.exe /C " & myCommand & " > " & fName, 0
'delete drive mapping
wshShell.CurrentDirectory = "C:\"
'MsgBox "stam - doesn't create file without this printing - ask pinchas"
Connect.RemoveNetworkDrive "T:"
'parsing output file
wshShell.CurrentDirectory = "\\star\SYSVOL\cs.biu.ac.i l\scripts"
Dim objFSO, objTextFile
Dim strLinetoParse, begining, arrToken() As String
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile(fName, 1)
Do While objTextFile.AtEndOfStream <> True
strLinetoParse = objTextFile.ReadLine
begining = Mid(strLinetoParse, 1, 7)
'catches only first instance. If all instances are required remove exit do
If begining = "ADsPath" Then
arrToken = Split(strLinetoParse, " ")
num = arrToken(1)
'strDN = arrToken(3)
Results(num - 1) = arrToken(3)
'Exit Do
End If
Loop
'MsgBox "Number of results is: " & num
objTextFile.Close
objFSO.DeleteFile (fName)
Set objFSO = Nothing
Set objFSO1 = Nothing
Set wshShell = Nothing
Set Connect = Nothing
'path = strDN
Dim i As Integer
If num <> "" Then
For i = 0 To num
RResults(i) = Results(i)
Next
End If
End Sub
Public Sub FindUser(ID As String)
Dim wshShell
Set wshShell = CreateObject("WScript.Shel
'map network drive
Dim Connect
Set Connect = CreateObject("WScript.Netw
Connect.MapNetworkDrive "T:", "\\star\SYSVOL"
wshShell.CurrentDirectory = "T:\cs.biu.ac.il\scripts"
'create random file name
Dim fName As String
Dim objFSO1
Set objFSO1 = CreateObject("Scripting.Fi
fName = NewRandomFileName("abcdefg
Do While objFSO1.FileExists(fName)
fName = NewRandomFileName("abcdefg
Loop
fName = fName & ".txt"
'execute search
Dim myCommand As String
phoneNo = ID
myCommand = "cscript search.vbs LDAP://DC=ch,DC=cs,DC=biu,
wshShell.Run "cmd.exe /C " & myCommand & " > " & fName, 0
'delete drive mapping
wshShell.CurrentDirectory = "C:\"
'MsgBox "stam - doesn't create file without this printing - ask pinchas"
Connect.RemoveNetworkDrive
'parsing output file
wshShell.CurrentDirectory = "\\star\SYSVOL\cs.biu.ac.i
Dim objFSO, objTextFile
Dim strLinetoParse, begining, arrToken() As String
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile(fName,
Do While objTextFile.AtEndOfStream <> True
strLinetoParse = objTextFile.ReadLine
begining = Mid(strLinetoParse, 1, 7)
'catches only first instance. If all instances are required remove exit do
If begining = "ADsPath" Then
arrToken = Split(strLinetoParse, " ")
num = arrToken(1)
'strDN = arrToken(3)
Results(num - 1) = arrToken(3)
'Exit Do
End If
Loop
'MsgBox "Number of results is: " & num
objTextFile.Close
objFSO.DeleteFile (fName)
Set objFSO = Nothing
Set objFSO1 = Nothing
Set wshShell = Nothing
Set Connect = Nothing
'path = strDN
Dim i As Integer
If num <> "" Then
For i = 0 To num
RResults(i) = Results(i)
Next
End If
End Sub
'Start of code
Public Sub FindUser(ID As String)
Dim wshShell
Set wshShell = CreateObject("WScript.Shel l")
'map network drive
Dim Connect
Set Connect = CreateObject("WScript.Netw ork")
Connect.MapNetworkDrive "T:", "\\star\SYSVOL"
'DoEvents added to ensure that system completes mapping
DoEvents
'Set current directory
wshShell.CurrentDirectory = "T:\cs.biu.ac.il\scripts"
'create random file name
Dim fName As String
Dim objFSO1
Set objFSO1 = CreateObject("Scripting.Fi leSystemOb ject")
fName = NewRandomFileName("abcdefg hijklmnopq rstuvwxyzA BCDEFGHIJK LMNOPQRSTU VWXYZ", 11)
Do While objFSO1.FileExists(fName)
fName = NewRandomFileName("abcdefg hijklmnopq rstuvwxyzA BCDEFGHIJK LMNOPQRSTU VWXYZ", 11)
Loop
fName = fName & ".txt"
'execute search
Dim myCommand As String
phoneNo = ID
myCommand = "cscript search.vbs LDAP://DC=ch,DC=cs,DC=biu, DC=ac,DC=i l /C:telephoneNumber=" & phoneNo & " /S:subtree"
wshShell.Run "cmd.exe /C " & myCommand & " > " & fName, 0
'DoEvents added here to ensure complete run/search
DoEvents
'delete drive mapping
wshShell.CurrentDirectory = "C:\"
'MsgBox "stam - doesn't create file without this printing - ask pinchas"
Connect.RemoveNetworkDrive "T:"
'DoEvents added.
DoEvents
'parsing output file
wshShell.CurrentDirectory = "\\star\SYSVOL\cs.biu.ac.i l\scripts"
Dim objFSO, objTextFile
Dim strLinetoParse, begining, arrToken() As String
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile(fName, 1)
Do While objTextFile.AtEndOfStream <> True
strLinetoParse = objTextFile.ReadLine
begining = Mid(strLinetoParse, 1, 7)
'catches only first instance. If all instances are required remove exit do
If begining = "ADsPath" Then
arrToken = Split(strLinetoParse, " ")
num = arrToken(1)
'strDN = arrToken(3)
Results(num - 1) = arrToken(3)
'Exit Do
End If
Loop
'MsgBox "Number of results is: " & num
objTextFile.Close
objFSO.DeleteFile (fName)
Set objFSO = Nothing
Set objFSO1 = Nothing
Set wshShell = Nothing
Set Connect = Nothing
'path = strDN
Dim i As Integer
If num <> "" Then
For i = 0 To num
RResults(i) = Results(i)
Next
End If
End Sub
/*End of code
The number of DoEvents commands might overkill but at least your system will be keeping up with all the mapping changes.
Hope this helps.
*/
Public Sub FindUser(ID As String)
Dim wshShell
Set wshShell = CreateObject("WScript.Shel
'map network drive
Dim Connect
Set Connect = CreateObject("WScript.Netw
Connect.MapNetworkDrive "T:", "\\star\SYSVOL"
'DoEvents added to ensure that system completes mapping
DoEvents
'Set current directory
wshShell.CurrentDirectory = "T:\cs.biu.ac.il\scripts"
'create random file name
Dim fName As String
Dim objFSO1
Set objFSO1 = CreateObject("Scripting.Fi
fName = NewRandomFileName("abcdefg
Do While objFSO1.FileExists(fName)
fName = NewRandomFileName("abcdefg
Loop
fName = fName & ".txt"
'execute search
Dim myCommand As String
phoneNo = ID
myCommand = "cscript search.vbs LDAP://DC=ch,DC=cs,DC=biu,
wshShell.Run "cmd.exe /C " & myCommand & " > " & fName, 0
'DoEvents added here to ensure complete run/search
DoEvents
'delete drive mapping
wshShell.CurrentDirectory = "C:\"
'MsgBox "stam - doesn't create file without this printing - ask pinchas"
Connect.RemoveNetworkDrive
'DoEvents added.
DoEvents
'parsing output file
wshShell.CurrentDirectory = "\\star\SYSVOL\cs.biu.ac.i
Dim objFSO, objTextFile
Dim strLinetoParse, begining, arrToken() As String
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile(fName,
Do While objTextFile.AtEndOfStream <> True
strLinetoParse = objTextFile.ReadLine
begining = Mid(strLinetoParse, 1, 7)
'catches only first instance. If all instances are required remove exit do
If begining = "ADsPath" Then
arrToken = Split(strLinetoParse, " ")
num = arrToken(1)
'strDN = arrToken(3)
Results(num - 1) = arrToken(3)
'Exit Do
End If
Loop
'MsgBox "Number of results is: " & num
objTextFile.Close
objFSO.DeleteFile (fName)
Set objFSO = Nothing
Set objFSO1 = Nothing
Set wshShell = Nothing
Set Connect = Nothing
'path = strDN
Dim i As Integer
If num <> "" Then
For i = 0 To num
RResults(i) = Results(i)
Next
End If
End Sub
/*End of code
The number of DoEvents commands might overkill but at least your system will be keeping up with all the mapping changes.
Hope this helps.
*/
ASKER
Thank you Greenback.
No I understand where the doevents need to come, but I still don't understand what they should contain (how to write them). Can you give me a sample code of what you would put?
Thanks.
No I understand where the doevents need to come, but I still don't understand what they should contain (how to write them). Can you give me a sample code of what you would put?
Thanks.
The DoEvents is just a single word command - DoEvents.
Just type that in and VB will recognize it and go off on it's merry way.
Sorry for not being more clear.
Just type that in and VB will recognize it and go off on it's merry way.
Sorry for not being more clear.
ASKER
Thanks for the explanation, but it still gets stuck at the line:
Connect.RemoveNetworkDrive "T:"
although I added DoEvents after it.
It gives the error:
Run-time error '-2147022492 (80070964)':
The device is in use by an active process and cannot be disconnected.
Connect.RemoveNetworkDrive
although I added DoEvents after it.
It gives the error:
Run-time error '-2147022492 (80070964)':
The device is in use by an active process and cannot be disconnected.
Try setting a timer to loop just after the connect.removenetworkdrive "T:" statement.
Please disregard my last post.
Move the block of code where you set the objects to nothing to before the removenetworkdrive statement. It looks like the object is still pointing to the t: when you try to remove the mapping.
Move the block of code where you set the objects to nothing to before the removenetworkdrive statement. It looks like the object is still pointing to the t: when you try to remove the mapping.
ASKER
If I set the object to be nothing, I can't delete it (I get an error), probably because the object doesn't exist anymore.
If I set it to nothing, then recreate it and try to delete mapping I'm back to the original problem.
If I set it to nothing, then recreate it and try to delete mapping I'm back to the original problem.
ASKER
Here is an update:
I found that the problems occurs because the VB program startes running new applications and doesn't wait for them to complete before it continues.
It's a timing issue like some one said.
I found the 'Shell' command which I can use instead of 'wshShell.Run'
This command returns the task ID of the process I'm running and so I can check if the process completed before my application moves on.
My question is: When I have the task ID, how do I check if the process is still alive?
Thanks.
I found that the problems occurs because the VB program startes running new applications and doesn't wait for them to complete before it continues.
It's a timing issue like some one said.
I found the 'Shell' command which I can use instead of 'wshShell.Run'
This command returns the task ID of the process I'm running and so I can check if the process completed before my application moves on.
My question is: When I have the task ID, how do I check if the process is still alive?
Thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Question is PAQed and points refunded.
YensidMod
Experts Exchange Moderator
YensidMod
Experts Exchange Moderator
ASKER