wrap asynchronous calls in synchronous calls

Hi all,

I have a general question on how to hide asynchronous calls from a user:
My user invokes a method like this
public Customer GetCustomer(int id);

This method calls an asynchronous service which has the following method+event
public void GetCustomer(int id)
public event EventHandler CustomerReceived

The event is fired when the GetCustomer call has finished.

What I want to do is as follows:

I want to pack the asynchronous call into the method which is being called by my users.

Can anyone of you give me a hint

Regards,
David


Answer this question

wrap asynchronous calls in synchronous calls

  • Alcide

    Markku, this isn't a great solution. You should use a ManualResetEvent for this instead. Here is the you code modified:


    private ManualResetEvent _customerReceived = new ManualResetEvent( false );
    private void button1_Click(object sender, EventArgs e)
    {
    _async = false;
    _customerReceived.Reset();
    _customer.GetCustomer(1);
    _customerReceived.WaitOne();
    Message( "Customer received!" );
    }
    void customer_CustomerReseived(object sender, EventArgs e)
    {
    // Make something here
    _customerReceived.Set();
    }



  • Biceps

    Thanks

    Every day I learn somting new.


  • al_puff

    Hi David

    I test this kind of solution and I this it work OK.

    private void button1_Click(object sender, EventArgs e)

    {

    _async = false;

    _customer.GetCustomer(1);

    while (!_async) ;

    MessageBox.Show("Customer reseived");

    }

    void customer_CustomerReseived(object sender, EventArgs e)

    {

    // Make something here

    _async = true;

    }

    Yours

    Markku


  • JLarkin

    Hi PJ,

    thanks for this solution, this looks really awesome!
    I will test it now...

    Thanks,
    David

  • wrap asynchronous calls in synchronous calls