'How to prevent WebRequest Conflicts between Multi Methods in .NET?
'm new here so please bear with me. Happy to clarify if any questions/clarification.
In all my processes I have LogHandler method via WebRequest which basically store what process was called by user and this runs as Thread so existing processes are not slowed down.
I have another method called StatusCheckX which basically does another WebRequest and check if data needed exist and perform certain task with the data.
Problem I'm facing is that of WebRequest from StatusCheckX and LogHandler seems to be conflicting/waiting on another and stops responding and eventually times out. How can I overcome this.
Thanks for help/advice. Ari.
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
Solution 1:[1]
if it helps anyone, I finally moved it to new class and error went away.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ari Kurita |