Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Sub WriteToFile(ByVal FilePath As String, ByVal FileName As String, ByVal Data() As Byte)
- Dim FileOpen As Boolean
- Dim File As System.IO.FileStream = Nothing
- Dim StartTime As DateTime
- Dim MaxWaitSeconds As Integer = 120
- StartTime = DateTime.Now
- FileOpen = False
- Do
- Try
- File = New System.IO.FileStream(FilePath & FileName, IO.FileMode.Append)
- FileOpen = True
- Catch ex As Exception
- If DateTime.Now.Subtract(StartTime).TotalSeconds > MaxWaitSeconds Then
- Throw New Exception("Waited more than " & MaxWaitSeconds & " To Open File.")
- Else
- Dim FileWatch As System.IO.FileSystemWatcher
- FileWatch = New System.IO.FileSystemWatcher(FilePath, FileName)
- FileWatch.WaitForChanged(IO.WatcherChangeTypes.Changed,5000)
- End If
- FileOpen = False
- End Try
- Loop While Not FileOpen
- If FileOpen Then
- File.Write(Data, 0, Data.Length)
- File.Close()
- End If
- End Sub
- using (var watcher = new FileSystemWatcher(MatlabPath, fileName))
- {
- var wait = new EventWaitHandle(false, EventResetMode.AutoReset);
- watcher.EnableRaisingEvents = true;
- watcher.Changed += delegate(object sender, FileSystemEventArgs e)
- {
- wait.Set();
- };
- if (!wait.WaitOne(MillissecondsTimeout))
- {
- throw new TimeoutException();
- }
- }
Add Comment
Please, Sign In to add comment