Why does asynchronous delegate method require calling EndInvoke?

The reason you need to call EndInvoke is to avoid memory leaks; . Net will store information about the function's result (or exception) until you call EndInvoke .

Up vote 2 down vote favorite 2 share g+ share fb share tw.

Here is the code im trying to run. Class Program { private delegate void GenerateXmlDelegate(); static void Main(string args) { GenerateXmlDelegate worker = new GenerateXmlDelegate(GenerateMainXml); IAsyncResult result = worker. BeginInvoke(null, null); } private static void GenerateMainXml() { Thread.

Sleep(10000); Console. WriteLine("GenerateMainXml Called by delegate"); } } c# asynchronous delegates link|improve this question edited Nov 11 '09 at 8:54Rashmi Pandit4,88812042 asked Nov 11 '09 at 3:04Anton P.

The reason you need to call EndInvoke is to avoid memory leaks; . Net will store information about the function's result (or exception) until you call EndInvoke. You can call EndInvoke in the completion handler that you give to BeginInvoke and retain the asyncronous nature.

EDIT: For example: class Program { private delegate void GenerateXmlDelegate(); static void Main(string args) { GenerateXmlDelegate worker = new GenerateXmlDelegate(GenerateMainXml); IAsyncResult result = worker. BeginInvoke(delegate { try { worker.EndInvoke(); } catch(...) { ... } }, null); } private static void GenerateMainXml() { Thread. Sleep(10000); Console.

WriteLine("GenerateMainXml Called by delegate"); } } If you want to fire an async call and forget about it, you can use the ThreadPool, like this: ThreadPool. QueueUserWorkItem(delegate { GenerateMainXml(); }).

From all the articles i've read it suggests that by simply calling BeginInvoke will fire the method call. – Anton P Nov 11 '09 at 3:19.

As SLaks said, EndInvoke insures against memory leaks. BeginInvoke is still asynchronous; consider the following code: static void Main() { Func slowCalculator = new Func(PerformSlowCalculation); IAsyncResult slowCalculation = slowCalculator. BeginInvoke(null, null); // lots of stuff to do while slowCalculator is doing its thing Console.

WriteLine("Result is {0}", slowCalculator. EndInvoke(slowCalculation)); } static double PerformSlowCalculation() { double result; // lots and lots of code return result; } If this code were written without the BeginInvoke/EndInvoke calls, PerformSlowCalculation would have to finish before Main could do the rest of its "lots of stuff"; this way, the two can be happening at the same time. Now, in your example using a GenerateXmlDelegate, you still need EndInvoke even though you're not returning anything.

The way to do this is: static void Main(string args) { GenerateXmlDelegate worker = new GenerateXmlDelegate(GenerateMainXml); IAsyncResult result = worker. BeginInvoke(GenerateXmlComplete, null); } private static void GenerateXmlComplete(IAsyncResult result) { AsyncResult realResult = result as AsyncResult; GenerateXmlDelegate worker = result. AsyncDelegate as GenerateXmlDelegate; worker.EndInvoke(); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions