zoukankan      html  css  js  c++  java
  • vb.net 写入文件同步锁

     1  <SoapHeader("oHeader")> _
     2     <WebMethod()> _
     3     <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
     4     Public Function Test_SyncLock() As String
     5         Dim threads(10) As Threading.Thread
     6         For i As Integer = 0 To 10
     7             threads(i) = New Threading.Thread(AddressOf PrintNumbers)
     8             threads(i).Name = String.Format("Worker thread #{0}", i)
     9         Next
    10         ' Now start each one.
    11         For Each t As Threading.Thread In threads
    12             t.Start()
    13         Next
    14         Return ""
    15     End Function
    16 
    17     Private Sub PrintNumbers()
    18         writeLog("", "a", Threading.Thread.CurrentThread.Name)
    19     End Sub
    20 
    21 #Region "Write Log File"
    22 
    23     Private threadLock As Object = New Object()
    24     Public Sub writeLog(ByVal dir As String, ByVal fileName As String, ByVal log As String)
    25 
    26         Dim filePath As String = System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/"),
    27                                                        "Log/" & dir & "/" & fileName & ".log")
    28         SyncLock threadLock
    29             '创建目录
    30             Dim dirPath As String = filePath.Substring(0, filePath.LastIndexOf("/"))
    31             If (Directory.Exists(dirPath) = False) Then
    32                 Directory.CreateDirectory(dirPath)
    33             End If
    34             Dim aa As System.IO.StreamWriter = New System.IO.StreamWriter(filePath, True, System.Text.Encoding.UTF8)
    35             aa.WriteLine(log)
    36             aa.Close()
    37             aa.Dispose()
    38         End SyncLock
    39     End Sub
    40 #End Region
    View Code
  • 相关阅读:
    Unreal Engine 4 Based Materials
    PhysX Clothing for UE4
    UE4中使用URL图片
    开始创作自己的VR作品——VR故事叙述终极指南
    UE4里的自定义深度功能
    Mybatis27题
    java 备用待迁移
    几个算法题目
    数据结构算法题目
    Mybatis 面试题
  • 原文地址:https://www.cnblogs.com/xiaobuild/p/5006955.html
Copyright © 2011-2022 走看看