Click here to Skip to main content
15,898,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a problem and didn't find any solution at the moment :

I have a property getX() who call's a function askX().

My askX() function call's a request on the server which returns an event.

The x value is received in a communication_X_RCVD()

So my question :doh: , how can I do to return the value received in my communication_X_RCVD() from my getX() property.

Thank's

ps: sorry for my poor English :sigh:
Posted

Presumably, the askX method is asynchronous because it may take some time to execute and the author didn't want you to block while waiting for it. Blocking the UI thread is bad design because it stops your application from interacting with the user.

So, the first question is: do you really want getX to block while the request is executing on the server?

Nick
 
Share this answer
 
Hi Nick,
It's not a problem in this case if my application it's locked.
So yes, I want to wait for the response.

Thank's

Pablo
 
Share this answer
 
If blocking is ok, you will need some synchronization.

You haven't posted enough details for me to give you code, but here is a start:

In getX(), start the asynchronous operation by calling askX(). Then wait on an EventWaitHandle ( ManualResetEvent or AutoResetEvent ). In communication_X_RCVD(), store the result in a visible field and then signal the EventWaitHandle. When getX() resumes, the result will be available.

If you post the signature of askX() and communication_X_RCVD(), I could be more specific.

Nick
 
Share this answer
 
Hi
My signatures method are

getX() as MyCustomObject<br />
<br />
askX(){<br />
server.AskX()<br />
}<br />
<br />
communication_X_RCVD(eventhandler) handles server.Event<br />
{<br />
}<br />


So to be a little more clear.
I request the server via server.askX() to create a file with some content.
the server event notify me that the file has been created.
I parse the file and build MyCustomObject.

I never worked with EventWaitHandle ( ManualResetEvent or AutoResetEvent ).

Thank's for helping Nick

Regards
Pablo
 
Share this answer
 
Hi Pablo,

This code will work as long as you only call getX from your UI thread:

VB
Imports System.Threading

Public Class MyCustomObject
    Public Property I As Integer
End Class

Public Class Server
    Public Sub AskX()
    End Sub
    Public Event ReceivedX As EventHandler
End Class

Public Class TestAsyncEvent
    Private WithEvents server As New Server
    Private mre As New ManualResetEvent(False)
    Private tmpX As MyCustomObject

    Public Function getX() As MyCustomObject

        tmpX = Nothing
        mre.Reset()

        askX()

        mre.WaitOne(TimeSpan.FromSeconds(30)) 'Specify some reasonable timeout

        Return tmpX

    End Function

    Private Sub askX()
        server.AskX()
    End Sub

    Private Sub communication_X_RCVD(ByVal sender As Object, ByVal args As EventArgs) Handles server.ReceivedX
        Dim x As New MyCustomObject
        x.I = 42
        tmpX = x
        mre.Set()
    End Sub
End Class


I suggest you read up on concurrent software. There is an excellent free primer here:

http://www.albahari.com/threading/[^]

Nick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900