Reputation: 1182
this is for writing data to same file multithreading
Dim _readWriteLock As ReaderWriterLockSlim = New ReaderWriterLockSlim
Private Sub writedatatofile(ByVal s As String)
' Set Status to Locked
_readWriteLock.EnterWriteLock()
Try
' Append text to the file
Dim sw As StreamWriter = File.AppendText("textfile.txt")
sw.WriteLine(s)
sw.Close()
Finally
' Release lock
_readWriteLock.ExitWriteLock()
End Try
End Sub
this is for getting asynchronous response
Private Shared Sub GetResponseCallback(ByVal asynchronousResult As IAsyncResult)
Dim response As HttpWebResponse = CType(request.EndGetResponse(asynchronousResult), _
HttpWebResponse)
writedatatofile(Response.Headers.Tostring)
End Sub
whene i call writedatatofile from GetresponseCallback i get this error : Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class
how can i write safety multithreads to same text file from asynchronous response ?
Upvotes: 0
Views: 1759
Reputation: 141638
You cannot call an Instance method from a Shared (static) method. You are basically saying, "I want to call a method that operates on a particular instance of an object from a method that doesn't care about object instances" - so how would the Shared method be able to do that?
You need to change GetResponseCallback
to instance (removed the Shared modifier).
Upvotes: 2