Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 18433 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > .NET Framework Interview Questions > How to call methods in remoting Asynchro ...

How to call methods in remoting Asynchronously ?

Interview question and answer by: Bharathi Cherukuri | Posted on: 4/26/2012 | Category: .NET Framework Interview questions | Views: 903 | | Points: 40
Ads
Ads


Answer:

By using delegates, we can make Asynchronous method.

Example:

Below is a class named DataCache, which may periodically need to write its cache to a file. The class contains a method named FlushToDisk to simulate this operation.

class DataCache

{
public int FlushToDisk(string fileName)
{
// simulate a long operation Thread.Sleep(4000);
return 0;
}
}


Since writing to disk is an expensive operation, we would like execute the FlushToDisk method asynchronously. The next example defines the CacheFlusher delegate and then uses it to call the DataCache object’s FlushToDisk method asynchronously.

static void Main(string[] args)

{
// create the object
DataCache cache = new DataCache();
// create the delegate
CacheFlusher flusher = new CacheFlusher(cache.FlushToDisk);
// call the delegate asynchronously
flusher.BeginInvoke("data.dat", new AsyncCallback(CallbackMethod), flusher);
// wait to exit
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}


static void CallbackMethod(IAsyncResult result)

{
// get the delegate that was used to call that
// method
CacheFlusher flusher = (CacheFlusher) result.AsyncState;
// get the return value from that method call
int returnValue = flusher.EndInvoke(result);
Console.WriteLine("The result was " + returnValue);
}


If you run this example you will see that the line “Press Enter to Exit” will be displayed in the console window, followed by the output from CallbackMethod. This is because the Main method calls BeginInvoke and continues processing, displaying its own output without waiting for the CacheFlusher delegate to finish. Later, when the asynchronous operation is complete, the delegate calls the CallbackMethod, which writes its output to the screen.

Asked In: Many Interviews | Alert Moderator 
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

Even more ... | Submit Interview Questions and win prizes!

More Interview Questions from Bharathi Cherukuri

Even more ... | Submit Interview Questions and win prizes!


About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 1:07:03 AM