0

I don't know how to create background task in WindowsPhone 8.1. Here is a solution I found. But when I rewrited the could, it throw an internal exception at line

this.taskRegistration = BackgroundTaskRegistration.AllTasks.Values.First();"

Who can tell me why?

The exception as follow

System.InvalidOperationException
HResult=-2146233079
Message=Sequence contains no elements
Source=System.Core
StackTrace:
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
cck
  • 703
  • 6
  • 11
  • 1
    It's hard to tell what exacly causes exception without more code. Maybe you will find more information: [MSDN](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj553413.aspx), [SO answer](http://stackoverflow.com/a/24076237/2681948), [blog post](http://www.romasz.net/how-to-add-a-backgroundtask/). – Romasz Nov 24 '14 at 11:53
  • The exception is being thrown, because `Values` doesn't have any element in sequence. Check [MSDN](http://msdn.microsoft.com/en-us/library/vstudio/bb535050(v=vs.100).aspx), in particular `Exceptions` section. Instead of `First()`, use `FirstOrDefault()` which won't throw an exception if sequence is empty, but rather will return `null`. – Michael Nov 24 '14 at 12:02

0 Answers0