Consider the following use case for fire-and-forget:
The caller is requesting some data from my method. My method checks the cache to see if there is data. If not, it extracts it from the source and caches it. The caller does not need to wait until the caching occurs before receiving its results, and the method should not prevent the caller from receiving the result if the cache process fails. What I have today is as follows:
public Foo GetFoo(string fooKey)
{
// look for Foo with fooKey in cache
// if Foo is not found, get Foo with fooKey from source
// and assign it to local variable myFoo
Task cacheTask
= Task.Run
(
() => CacheFoo(myFoo)// fire-and-forget the caching of myFoo
);
return myFoo;
}
CacheFoo , , ( .Net 4.5), . , , . ?
try
{
...
cacheTask.ContinueWith
(
(e) => {
if (cacheTask.IsFaulted)
{
;
}
}
, TaskContinuationOptions.OnlyOnFaulted
);
}
? if if IsFaulted , "OnlyOnFaulted"?
/ .