如何防止 .NET 中多个方法之间的 WebRequest 冲突?
How to prevent WebRequest Conflicts between Multi Methods in .NET?
我是新来的,所以请多多包涵。很高兴澄清是否有 questions/clarification.
在我所有的进程中,我都有通过 WebRequest 的 LogHandler 方法,它基本上存储了用户调用的进程,并且它作为线程运行,因此现有进程不会减慢速度。
我有另一个名为 StatusCheckX 的方法,它基本上执行另一个 WebRequest 并检查是否存在所需数据并使用数据执行特定任务。
我面临的问题 是来自 StatusCheckX 的 WebRequest 和 LogHandler 似乎在另一个上 conflicting/waiting 并停止响应并最终超时。我该如何克服这个问题。
感谢 help/advice。
阿里.
Public Function Important_Method1() As Boolean
Dim VariableX As Boolean = True
Try
'do something
'do something
Call LogHandler()
If VariableX = True Then
If StatusCheckX() = False Then
Return False
Else
Return True
End If
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function Important_Method2() As Boolean
Dim VariableX As Boolean = True
Try
'do something
'do something
Call LogHandler()
If VariableX = True Then
If StatusCheckX() = False Then
Return False
Else
Return True
End If
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Function LogHandler() As Boolean
Dim T1 As Thread
T1 = New Thread(AddressOf WriteLogX)
T1.Start()
Return True
End Function
Private Function WriteLogX() As Boolean
Dim url As String = "www.xyz.com"
Try
Dim Req As WebRequest = WebRequest.Create(url)
Dim Res As HttpWebResponse = CType(Req.GetResponse(), HttpWebResponse)
If Res.StatusDescription = "ALL GOOD" Then
Return True
Else
Return False
End If
Res.Close()
Res.Dispose()
Catch ex As Exception
Return False
End Try
End Function
Private Function StatusCheckX() As Boolean
Try
'do something
'do something
'do something
Dim Req As WebRequest = WebRequest.Create("www.ijk.com")
Dim Res As HttpWebResponse = CType(Req.GetResponse(), HttpWebResponse)
If Res.StatusDescription = "OK" Then
Dim ResStream As System.IO.Stream = Res.GetResponseStream()
Dim SReader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim i As String = SReader.ReadToEnd()
Dim dict = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(i)
SReader.Close()
SReader.Dispose()
ResStream.Close()
ResStream.Dispose()
If dict.Values(0) = "OK PROCEED" Then
'do something
Return True
Else
'do something
Return False
End If
Else
Return False
End If
Res.Close()
Res.Dispose()
Catch ex As Exception
Return False
End Try
End Function
如果它对任何人有帮助,我最终将它移到了新的 class 并且错误消失了。
我是新来的,所以请多多包涵。很高兴澄清是否有 questions/clarification.
在我所有的进程中,我都有通过 WebRequest 的 LogHandler 方法,它基本上存储了用户调用的进程,并且它作为线程运行,因此现有进程不会减慢速度。
我有另一个名为 StatusCheckX 的方法,它基本上执行另一个 WebRequest 并检查是否存在所需数据并使用数据执行特定任务。
我面临的问题 是来自 StatusCheckX 的 WebRequest 和 LogHandler 似乎在另一个上 conflicting/waiting 并停止响应并最终超时。我该如何克服这个问题。
感谢 help/advice。 阿里.
Public Function Important_Method1() As Boolean
Dim VariableX As Boolean = True
Try
'do something
'do something
Call LogHandler()
If VariableX = True Then
If StatusCheckX() = False Then
Return False
Else
Return True
End If
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function Important_Method2() As Boolean
Dim VariableX As Boolean = True
Try
'do something
'do something
Call LogHandler()
If VariableX = True Then
If StatusCheckX() = False Then
Return False
Else
Return True
End If
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Function LogHandler() As Boolean
Dim T1 As Thread
T1 = New Thread(AddressOf WriteLogX)
T1.Start()
Return True
End Function
Private Function WriteLogX() As Boolean
Dim url As String = "www.xyz.com"
Try
Dim Req As WebRequest = WebRequest.Create(url)
Dim Res As HttpWebResponse = CType(Req.GetResponse(), HttpWebResponse)
If Res.StatusDescription = "ALL GOOD" Then
Return True
Else
Return False
End If
Res.Close()
Res.Dispose()
Catch ex As Exception
Return False
End Try
End Function
Private Function StatusCheckX() As Boolean
Try
'do something
'do something
'do something
Dim Req As WebRequest = WebRequest.Create("www.ijk.com")
Dim Res As HttpWebResponse = CType(Req.GetResponse(), HttpWebResponse)
If Res.StatusDescription = "OK" Then
Dim ResStream As System.IO.Stream = Res.GetResponseStream()
Dim SReader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim i As String = SReader.ReadToEnd()
Dim dict = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(i)
SReader.Close()
SReader.Dispose()
ResStream.Close()
ResStream.Dispose()
If dict.Values(0) = "OK PROCEED" Then
'do something
Return True
Else
'do something
Return False
End If
Else
Return False
End If
Res.Close()
Res.Dispose()
Catch ex As Exception
Return False
End Try
End Function
如果它对任何人有帮助,我最终将它移到了新的 class 并且错误消失了。