Thread Locking

O

OpticTygre

I have a class which represents an HTTPListener (call this
"aMessage"). The class accepts http requests, and responds to those
requests accordingly using simple code copied from the MSDN. What I
need to do however, is something that I can't seem to find in the
documentation.

My ProcessRequest subroutine needs to raise an event to its parent
application, then wait until the parent application returns a response
(through a public subroutine in aMessage), then unblock the
ProcessRequest subroutine and send the response.

I have accomplished this through shared variables (one representing a
response string, and the other representing a boolean that signals a
respose has been returned and the thread can continue processing) and
a very archaic polling routine, and I'm curious if there are better
ways to handle this type of situation. I'm sure there are, I just
don't know where to look.

If anyone could suggest some documentation, or - even better - sample
code that could accomplish this, it would be very helpful. Below is
some pseudo-code to show what I'm trying to do.

-----Schnipp-----

Private Shared m_IsResponseReady As Boolean = False
Private Shared m_ResponseString As String = String.Empty

Protected Overrides Sub ProcessRequest(ByVal Context As
System.Net.HttpListenerContext)

'Here goes some code to get the request from the Context object

'Raise an event passing the request string
RaiseEvent OnRequestReceived(strRequest)

'Here is where I want to lock the thread until a response is
received. Right now, I just use a
'polling mechanism checking the m_IsResponseReady boolean

Do While Not m_IsResponseReady
Thread.Sleep(500)
Loop

'Now that I have a request, I want to continue processing -
returning the response to the Context object.

End Sub

Public Sub SetResponse(ByVal Response as String)

m_ResponseString = Response
m_IsResponseReady = True

End Sub

-----Schnapp-----

Thanks, and happy new year!

-Jason
 
T

Tom Shelton

I have a class which represents an HTTPListener (call this
"aMessage"). The class accepts http requests, and responds to those
requests accordingly using simple code copied from the MSDN. What I
need to do however, is something that I can't seem to find in the
documentation.

My ProcessRequest subroutine needs to raise an event to its parent
application, then wait until the parent application returns a response
(through a public subroutine in aMessage), then unblock the
ProcessRequest subroutine and send the response.

Not understanding the complete problem, I'm going to make a
suggestion... Would it work to retunr the response via the event?
Basically, what I'm proposing is something like the Form.Closing
event... So, essentially, you could do something like:

Public ResponseEventArgs
Inherits System.EventArgs

Private _request As String
Private _response As String

Public Sub New (ByVal request As string)
_request = request
End Sub

Public ReadOnly Property Request As String
Get
Return _request
End Get
End Property

Public Property Response As String
Get
Return _response
End Get
Set (Value As String)
_response = Value
End Set
End Property
End Class

' this for communication
Public Delegate Sub ResponseEventHandler (ByVal sender As Object, ByVal
e As ResponseEventArgs)


Then in your code to call the event:

Sub ProcessRequest (blah, blah)
' get the request

RequestEventArgs e = new ResponseRequestArgs (strRequest)
RaiseEvent OnRequestReceived (Me, e)

' process response
response = e.Response

End Sub

Anyway, just a thought.
 
O

OpticTygre

Not understanding the complete problem, I'm going to make a
suggestion...  Would it work to retunr the response via the event?
Basically, what I'm proposing is something like the Form.Closing
event...  So, essentially, you could do something like:

Public ResponseEventArgs
        Inherits System.EventArgs

        Private _request As String
        Private _response As String

        Public Sub New (ByVal request As string)
                _request = request
        End Sub

        Public ReadOnly Property Request As String
                Get
                        Return _request
                End Get
        End Property

        Public Property Response As String
          Get
            Return _response
          End Get
          Set (Value As String)
             _response = Value
          End Set
        End Property
End Class

' this for communication
Public Delegate Sub ResponseEventHandler (ByVal sender As Object, ByVal
e As ResponseEventArgs)

Then in your code to call the event:

Sub ProcessRequest (blah, blah)
        ' get the request

        RequestEventArgs e = new ResponseRequestArgs (strRequest)
        RaiseEvent OnRequestReceived (Me, e)

        ' process response
        response = e.Response

End Sub

Anyway, just a thought.

No, that won't work. You don't get a reply directly back from an
event like you would a function. Events are raised and forgotten
about. In the above code, the event would be raised to the event
handling application (which could take any number of seconds to take
the request and create a proper response), but the ProcessRequest
subroutine would continue to run asynchronously from whatever was
handling the event and creating a response. In other words, the
ProcessRequest sub would error, because no response would be present,
as the event handling application would still be processing the
request string and creating a response.
 
G

Guest

I have a class which represents an HTTPListener (call this
"aMessage"). The class accepts http requests, and responds to those
requests accordingly using simple code copied from the MSDN. What I
need to do however, is something that I can't seem to find in the
documentation.

My ProcessRequest subroutine needs to raise an event to its parent
application, then wait until the parent application returns a response
(through a public subroutine in aMessage), then unblock the
ProcessRequest subroutine and send the response.

I have accomplished this through shared variables (one representing a
response string, and the other representing a boolean that signals a
respose has been returned and the thread can continue processing) and
a very archaic polling routine, and I'm curious if there are better
ways to handle this type of situation. I'm sure there are, I just
don't know where to look.

If anyone could suggest some documentation, or - even better - sample
code that could accomplish this, it would be very helpful. Below is
some pseudo-code to show what I'm trying to do.


Take a look at ManualResetEvent and AutoResetEvent waithandles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top