2012-08-27 63 views
0

以下代码用于登录到今天的文件“Service.27082012.txt”。异步调用Writelog异常错误

Private filePath As String 
Private fileStream As FileStream 
Private streamWriter As StreamWriter 

Public Sub OpenFile() 
    Dim strPath As String 
    strPath = "Service." & Format(Now, "ddMMyyyy") & ".txt" 
    If System.IO.File.Exists(strPath) Then 
     fileStream = New FileStream(strPath, FileMode.Append, FileAccess.Write) 
    Else 
     fileStream = New FileStream(strPath, FileMode.Create, FileAccess.Write) 
    End If 
    streamWriter = New StreamWriter(fileStream) 
End Sub 

Public Sub WriteLog(ByVal strComments As String) 
    OpenFile() 
    streamWriter.WriteLine(strComments) 
    CloseFile() 
End Sub 

Public Sub CloseFile() 
    streamWriter.Close() 
    fileStream.Close() 
End Sub 

但是,当我尝试写通过WRITELOG日志文件异步,我得到的错误

该进程无法访问该文件“d:\ TEMP \ Service.27082012.txt”,因为它是被另一个进程使用。

如何摆脱它。

的异常堆栈跟踪是:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) 
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 
at Calc.Service.OpenFile() in D:\Project\Service.svc.vb:line 784 
at Calc.Service.WriteLog(String strComments) in D:\Project\Service.svc.vb:line 791 
at Calc.Service.GetInfo(DetailsRequest request) in D:\Project\Service.svc.vb:line 759 
at SyncInvokeGetInfo(Object , Object[] , Object[]) 
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) 
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 

日志写入文件异步像下面

Dim multiThreadOptions As ParallelOptions = New ParallelOptions 
multiThreadOptions.MaxDegreeOfParallelism = 16 
Parallel.For(0, 100, multiThreadOptions, Function(i) 
               Dim objServiceCall As New ServiceCall 
               synw.WriteLine("{0} ", objServiceCall.MethodCall(TextBox1.Text, TextBox2.Text)) 
               synw.Flush() 
               Return i 
              End Function) 

的MethodCall包含WRITELOG

的代码
Writelog("Comments") 
+0

请向我们展示您的代码如何尝试异步写入日志文件。 –

+0

您需要添加一个SyncLock块以避免跨线程问题 – Rajesh

回答

1

请使用一个SyncLock以避免上述的交叉线程问题。请在下面找到示例代码:

Dim multiThreadOptions As ParallelOptions = New ParallelOptions 
multiThreadOptions.MaxDegreeOfParallelism = 16 
Parallel.For(0, 100, multiThreadOptions, Function(i) 
               Dim objServiceCall As New ServiceCall 
               SyncLock synw 
               synw.WriteLine("{0} ", objServiceCall.MethodCall(TextBox1.Text, TextBox2.Text)) 
               synw.Flush() 
               End SyncLock 
               Return i 
              End Function) 

而且你正在写一个文件,它是特定日期,因此,如果你在同一个时间点多个电话,那么你会试图访问同一文件,因此你可能会得到这个例外。尝试在您正在执行写入文件的streamWriter对象上使用syncLock,或者使日志文件具有时间特定性以避免此类问题