S
Stewart Berman
I am working my way through various parts of the VB.NET diagnostics and error handling and ran
across a small problem. It concerns a program that sits in the tray and runs continuously.
A runtime error was thrown at the first Trace.WriteLine statement -- see details below -- because
the ApplicationName_Trace.log file was locked (although I cannot find anything that might have
locked it at the time the error was thrown).
In the VB6 world I would have written my own logging class and it would always contain an "On Error
Resume Next" statement as Tracing/Error logging should never throw an error that stops the program.
Is there a setting or another way to do the equivalent in the TraceListener class -- preferably via
the app.config files? I really don't want to have to wrap every Trace block in a Try configuration.
Details:
The TraceListener is defined as:
<trace useGlobalLock="false" autoflush="true" indentsize="3">
<listeners>
<add name="traceListener" type="System.Diagnostics.TextWriterTraceListener"
initializeData="SABBackgroundSlideShow_Trace.log" />
<!-- <remove name="Default" /> -->
</listeners>
</trace>
The trace file ended at the CBackgroundWorker_ProgressChanged Tracing Ended statement below before I
hit the Continue button. The first line to be added after that was the Picturebox loading started
line. There is one line missing in the trace because of the error.
20090713 14:12:07.453: CBackgroundWorker_ProgressChanged Tracing Started
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Started
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Started
20090713 14:12:07.453: Percent of display time left: 10
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Ended
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Ended
20090713 14:12:07.453: CBackgroundWorker_ProgressChanged Tracing Ended
< *** Missing trace line **>
20090713 14:12:13.453: Picturebox loading started: C:\Common\SlideShow\IMG_2496.JPG
20090713 14:12:13.750: Picturebox loading completed: C:\Common\SlideShow\IMG_2496.JPG
If (cvarTraceSwitch.TraceInfo) Then
Trace.WriteLine(Format$(Now(), "yyyyMMdd HH:mm:ss.fff: ") &
System.Reflection.MethodBase.GetCurrentMethod.Name & " Tracing Started")
Trace.Indent()
End If
CBackgroundWorker = CType(sender, BackgroundWorker)
lMemoryInUseStarting = MemoryInUse()
dataSet = LoadData(CType(e.Argument, DataSet))
CWallpaper = New Wallpaper()
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal
Do While (Not CBackgroundWorker.CancellationPending)
nValid = 0
nRows = dataSet.Tables("Images").Rows.Count
If (0 < nRows) Then
For nRow = 1 To nRows
If ((CBackgroundWorker.CancellationPending) OrElse (0 <>
Interlocked.CompareExchange(cvarReload, -1, -1))) Then
Exit For
End If
dataRow = dataSet.Tables("Images").Rows(nRow - 1)
If (CType(dataRow.Item("Valid"), Boolean)) Then
sFilePathName = CType(dataRow("FileFolder"), String) & "\" &
CType(dataRow("FileName"), String)
pictureBox = New PictureBox()
Try
Trace.WriteLineIf(cvarTraceSwitch.TraceInfo, Format$(Now(), "yyyyMMdd
HH:mm:ss.fff: ") & "Picturebox loading started: " & sFilePathName)
pictureBox.Load(sFilePathName)
Trace.WriteLineIf(cvarTraceSwitch.TraceInfo, Format$(Now(), "yyyyMMdd
HH:mm:ss.fff: ") & "Picturebox loading completed: " & sFilePathName)
Catch ex As Exception
dataRow.Item("Valid") = False
Trace.WriteLineIf(cvarTraceSwitch.TraceWarning, Format$(Now(), "yyyyMMdd
HH:mm:ss.fff: ") & "Error in loading picturebox: " & ex.ToString())
Finally
pictureBox.Dispose()
pictureBox = Nothing
End Try
End If
The error thrown was:
System.IO.IOException: The process cannot access the file because another process has locked a
portion of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
at System.IO.FileStream.Flush()
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Flush()
at System.Diagnostics.TextWriterTraceListener.Flush()
at System.Diagnostics.TraceInternal.WriteLine(String message)
at System.Diagnostics.Trace.WriteLine(String message)
at SABBackgroundSlideShow.SlideShow.CBackgroundWorker_ProgressChanged(Object sender,
ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)
across a small problem. It concerns a program that sits in the tray and runs continuously.
A runtime error was thrown at the first Trace.WriteLine statement -- see details below -- because
the ApplicationName_Trace.log file was locked (although I cannot find anything that might have
locked it at the time the error was thrown).
In the VB6 world I would have written my own logging class and it would always contain an "On Error
Resume Next" statement as Tracing/Error logging should never throw an error that stops the program.
Is there a setting or another way to do the equivalent in the TraceListener class -- preferably via
the app.config files? I really don't want to have to wrap every Trace block in a Try configuration.
Details:
The TraceListener is defined as:
<trace useGlobalLock="false" autoflush="true" indentsize="3">
<listeners>
<add name="traceListener" type="System.Diagnostics.TextWriterTraceListener"
initializeData="SABBackgroundSlideShow_Trace.log" />
<!-- <remove name="Default" /> -->
</listeners>
</trace>
The trace file ended at the CBackgroundWorker_ProgressChanged Tracing Ended statement below before I
hit the Continue button. The first line to be added after that was the Picturebox loading started
line. There is one line missing in the trace because of the error.
20090713 14:12:07.453: CBackgroundWorker_ProgressChanged Tracing Started
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Started
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Started
20090713 14:12:07.453: Percent of display time left: 10
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Ended
20090713 14:12:07.453: OnSlideShowProgressChanged Tracing Ended
20090713 14:12:07.453: CBackgroundWorker_ProgressChanged Tracing Ended
< *** Missing trace line **>
20090713 14:12:13.453: Picturebox loading started: C:\Common\SlideShow\IMG_2496.JPG
20090713 14:12:13.750: Picturebox loading completed: C:\Common\SlideShow\IMG_2496.JPG
If (cvarTraceSwitch.TraceInfo) Then
Trace.WriteLine(Format$(Now(), "yyyyMMdd HH:mm:ss.fff: ") &
System.Reflection.MethodBase.GetCurrentMethod.Name & " Tracing Started")
Trace.Indent()
End If
CBackgroundWorker = CType(sender, BackgroundWorker)
lMemoryInUseStarting = MemoryInUse()
dataSet = LoadData(CType(e.Argument, DataSet))
CWallpaper = New Wallpaper()
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal
Do While (Not CBackgroundWorker.CancellationPending)
nValid = 0
nRows = dataSet.Tables("Images").Rows.Count
If (0 < nRows) Then
For nRow = 1 To nRows
If ((CBackgroundWorker.CancellationPending) OrElse (0 <>
Interlocked.CompareExchange(cvarReload, -1, -1))) Then
Exit For
End If
dataRow = dataSet.Tables("Images").Rows(nRow - 1)
If (CType(dataRow.Item("Valid"), Boolean)) Then
sFilePathName = CType(dataRow("FileFolder"), String) & "\" &
CType(dataRow("FileName"), String)
pictureBox = New PictureBox()
Try
Trace.WriteLineIf(cvarTraceSwitch.TraceInfo, Format$(Now(), "yyyyMMdd
HH:mm:ss.fff: ") & "Picturebox loading started: " & sFilePathName)
pictureBox.Load(sFilePathName)
Trace.WriteLineIf(cvarTraceSwitch.TraceInfo, Format$(Now(), "yyyyMMdd
HH:mm:ss.fff: ") & "Picturebox loading completed: " & sFilePathName)
Catch ex As Exception
dataRow.Item("Valid") = False
Trace.WriteLineIf(cvarTraceSwitch.TraceWarning, Format$(Now(), "yyyyMMdd
HH:mm:ss.fff: ") & "Error in loading picturebox: " & ex.ToString())
Finally
pictureBox.Dispose()
pictureBox = Nothing
End Try
End If
The error thrown was:
System.IO.IOException: The process cannot access the file because another process has locked a
portion of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
at System.IO.FileStream.Flush()
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Flush()
at System.Diagnostics.TextWriterTraceListener.Flush()
at System.Diagnostics.TraceInternal.WriteLine(String message)
at System.Diagnostics.Trace.WriteLine(String message)
at SABBackgroundSlideShow.SlideShow.CBackgroundWorker_ProgressChanged(Object sender,
ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)
at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)