Asynchronous method invocation: Difference between revisions

Content deleted Content added
RHaworth (talk | contribs)
RHaworth (talk | contribs)
copyedit
Line 1:
{{orphan}}
TheIn computer software, the '''Asynchronousasynchronous error reporting''' [[design pattern]] decouples exception throwing from theirthe origin of the exception to the use of the result, in such a way that exceptions happen in a "safe" way. Often used in connection with the [[Activeactive Objectobject]] pattern.<ref> [http://msdn.microsoft.com/en-us/library/ms228974(VS.80).aspx "Best Practices for Implementing the Event-based Asynchronous Pattern"] Under Errors and Exceptions</ref>
 
==Notes==
Recently seen implemented as a basic principle in [[Microsoft .NET]].<ref> [http://msdn.microsoft.com/en-us/library/ms228974(VS.80).aspx "Best Practices for Implementing the Event-based Asynchronous Pattern"] Under Errors and Exceptions</ref>
<references/>
 
== C# example ==
 
{{uncat}}
<source lang="csharp">
 
public class SomethingCallbackResult
{
private object _result;
public object Result {
get {
//Exception is not thrown until we ask for result
if (_result is Exception)
throw (Exception)_result;
return _result;
}
set {
_result = value;
}
}
}
 
public class Something
{
public delegate void SomethingCallback(SomethingCallbackResult result);
 
public void BeginSomething(SomethingCallback callback)
{
//Start thread with worker
 
//return
}
 
private void worker(SomethingCallback callback)
{
SomethingCallbackResult result = new SomethingCallbackResult();
try
{
//... Actual work ...
result.Result = ...
}
catch (Exception e)
{
//Exception is not throw here, but just saved for later use
result.Result = e;
}
callback(result);
}
}
</source>
 
 
==Notes==
<references/>