Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another approach than File.Delete(tcFile)
Message
From
30/05/2012 17:53:23
Al Doman (Online)
M3 Enterprises Inc.
North Vancouver, British Columbia, Canada
 
 
To
30/05/2012 17:09:34
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01544721
Message ID:
01544863
Views:
31
>>WRT the bold above, are you certain that's true? Are you sure you're running processes synchronously, that have to happen one after another? If one or more are running async, you'll start seeing issues like that.
>
>Here is the Process class that executes everything from inside a .NET application:
>
>
>Namespace Framework
>
>    Public Class ProcessFile
>
>        Public cArguments As String = ""
>        Public cDomain As String = ""
>        Public cError As String = ""
>        Public cFileName As String = ""
>        Public cLog As String = ""
>        Public cMessage As String = ""
>        Public cOutput As String = ""
>        Public cPassword As String = ""
>        Public cUsername As String = ""
>        Public cWorkingDirectory As String = ""
>        Public lRedirectStandardError As Boolean = True
>        Public lRedirectStandardInput As Boolean = True
>        Public lRedirectStandardOutput As Boolean = True
>        Public oProcess As Process = New Process
>        Private oApp As Framework.App = Nothing
>        Private oLXProcess As Framework.LXProcess = Nothing
>
>        ' This is when we access the class in a desktop mode
>        Public Sub New(ByVal toApplication As Framework.App)
>            oApp = toApplication
>        End Sub
>
>        ' This is when we access the class in a Web or Web Service mode
>        Public Sub New(ByVal toProcess As Framework.LXProcess)
>            oLXProcess = toProcess
>            oApp = oLXProcess.oApp
>        End Sub
>
>        Public Function Process() As Boolean
>            Dim loSecureString As System.Security.SecureString = New System.Security.SecureString()
>            Dim lnCounter As Integer = 0
>
>            ' Reset the values
>            cError = ""
>            cLog = ""
>            cMessage = ""
>            cOutput = ""
>
>            ' Trim everything
>            cArguments = Trim(cArguments)
>            cDomain = Trim(cDomain)
>            cPassword = Trim(cPassword)
>            cUsername = Trim(cUsername)
>
>            ' If we have a working directory
>            If cWorkingDirectory.Length > 0 Then
>
>                ' If the directory does not exist
>                If Not oApp.DirectoryExist(cWorkingDirectory) Then
>                    cMessage = "The directory " + cWorkingDirectory + " does not exist."
>                    Return False
>                End If
>
>            End If
>
>            ' If the file does not exist
>            If Not oApp.FileExist(cFileName) Then
>                cMessage = "The file " + cFileName + " does not exist."
>                Return False
>            End If
>
>            oProcess.StartInfo.FileName = cFileName
>            oProcess.StartInfo.WorkingDirectory = cWorkingDirectory
>            oProcess.StartInfo.Arguments = cArguments
>            oProcess.StartInfo.RedirectStandardOutput = lRedirectStandardOutput
>            oProcess.StartInfo.RedirectStandardInput = lRedirectStandardInput
>            oProcess.StartInfo.RedirectStandardError = lRedirectStandardError
>            oProcess.StartInfo.UseShellExecute = False
>            oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
>            oProcess.StartInfo.CreateNoWindow = True
>
>            ' If we have a domain
>            If cDomain.Length > 0 Then
>                oProcess.StartInfo.Domain = cDomain
>            End If
>
>            ' If we have a username
>            If cUsername.Length > 0 Then
>                oProcess.StartInfo.LoadUserProfile = True
>                oProcess.StartInfo.UserName = cUsername
>
>                ' For each character
>                For lnCounter = 1 To cPassword.Length
>                    loSecureString.AppendChar(cPassword(lnCounter - 1))
>                Next
>
>                oProcess.StartInfo.Password = loSecureString
>            End If
>
>            Try
>                oProcess.Start()
>            Catch loError As Exception
>                cMessage = loError.Message + " " + cFileName + " " + cArguments
>                Return False
>            End Try
>
>            ' If we have redirected the error
>            If lRedirectStandardError Then
>                cError = oProcess.StandardError.ReadToEnd()
>            End If
>
>            ' If we have redirected the output
>            If lRedirectStandardOutput Then
>                cOutput = oProcess.StandardOutput.ReadToEnd()
>
>                ' If we have an output
>                If cOutput.Length > 0 Then
>
>                    ' If we have at least three characters
>                    If cOutput.Length > 2 Then
>
>                        ' If the last characters is a carriage return
>                        If Mid(cOutput, cOutput.Length - 1, 2) = oApp.cCR Then
>                            cOutput = Mid(cOutput, 1, cOutput.Length - 2)
>                        End If
>
>                    End If
>
>                    ' Some output contains only CHR(13), so we will do some parsing to standardize all this
>                    cOutput = oApp.StrTran(cOutput, oApp.cCR, "LXFramework" + Chr(0) + "LXFramework")
>                    cOutput = oApp.StrTran(cOutput, Chr(13), oApp.cCR)
>                    cOutput = oApp.StrTran(cOutput, "LXFramework" + Chr(0) + "LXFramework", oApp.cCR)
>
>                End If
>
>            End If
>
>            ' If we have an output
>            If cOutput.Length > 0 Then
>                cLog = cLog + "Output" + oApp.cCR + "------" + oApp.cCR + cOutput
>            End If
>
>            ' If we have an error
>            If cError.Length > 0 Then
>                cLog = cLog + "Error" + oApp.cCR + "-----" + oApp.cCR + cError
>            End If
>
>            ' Reset the values
>            cDomain = ""
>            cPassword = ""
>            cUsername = ""
>
>            Return True
>        End Function
>
>    End Class
>
>End Namespace
>
>
>When the Zip class is calling it to execute the WinZip command line utility, I set it like this:
>
>
>            Dim loProcess As Framework.ProcessFile = New Framework.ProcessFile(oApp)
>
>            loProcess.cWorkingDirectory = cWinZipDirectory
>            loProcess.cFileName = loProcess.cWorkingDirectory + "\WZUnzip.exe"
>            loProcess.cArguments = loProcess.cArguments + " """ + cFile + """"
>            loProcess.cArguments = loProcess.cArguments + " """ + lcDirectory + """"
>            loProcess.lRedirectStandardInput = False
>            loProcess.lRedirectStandardError = False
>            loProcess.lRedirectStandardOutput = False
>            If Not loProcess.Process() Then
>               cMessage = loProcess.cMessage
>               Exit Try
>            End If
>
>            ' Add a 7.0 second timeout to leave enough time for the memory to be flush so loFileDirectory.GetFile() can see the files
>            System.Threading.Thread.Sleep(7000)
>
>
>
>Could it be that the fact that I turned OFF the three RedirectStandard that this is causing the process to run in background and the code continues even if it has not finished? If this is not a factor then is there a switch in the .NET Process class that would allow to set it so it would finish before continuing?

I'm not familiar with the .Net Process class so I can't help you with its internals.

Just looking at your code very generally, the 7 second wait at the end jumps out at me. If everything you're doing is truly synchronous, you should not need any wait at all.

Even if a file operation has not yet been committed to disk, the file system still knows the file is there. You can see this even in Windows Explorer. If you copy a large file from one folder to another, in the destination folder you can hit F5 to refresh the view, and the file appears immediately. Its size may start at zero, but if you repeatedly refresh you will see it grow until the process completes.

I don't know what would happen if (in a separate process) you tried to delete a file that was still being copied, my guess would be you'd get some sort of "file in use by another" error. The separate process might need to try to get an exclusive FOPEN( ) or some such to determine when the file has been fully written.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform