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