locked
Is it correct to use Task.WaitAll inside OnInvoke function of ScheduledAgent? RRS feed

  • Question

  • I just want to know if what I'm doing is correct.

    I'm trying update my live tile with a value, but the value is calculated based on three sources - two http request plus the user location.

    So my OnInvoke() function looks something like this

            protected override void OnInvoke(ScheduledTask task)
            {
              
                //grab app's pinned live tile
                ShellTile mainTile = ShellTile.ActiveTiles.First();
    
                //retreive data
                #region
                tasks[0] = HttpExtensions.GetMyData("http://www.datasource1.txt");
                tasks[1] = HttpExtensions.GetMyData("http://www.datasource2.txt");
    
                if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] != true)
                {
                    // The user has opted out of Location.
                }
                else
                    tasks[2] = GeoLocate.OneShotGeoLocate();
    
                try
                {
                    //wait for all tasks to complete before processing
    				Task.WaitAll(tasks,10000);
    				
                    var geoLocation = ((Task<Geoposition>)tasks[2]).Result;
    
                    var value1 = ((Task<Stream>)tasks[1]).Result;
                    var value2 = ((Task<Stream>)tasks[0]).Result;
    
                    //calculate some other stuff based on values and location
    				//...
    				//...
    
                }
                catch (Exception ae)
                {
                }
    
                #endregion
    
                //create new FlipTileObject
                FlipTileData tileData = new FlipTileData
                {
                    BackgroundImage = new Uri("CustomBackground.png", UriKind.RelativeOrAbsolute),
                    Title = "My App",
                    Count = (int)number,
                };
    
                //pass FlipTileObject to the pinned tile
                if (mainTile != null)
                {
                    mainTile.Update(tileData);
                }
    
                //done
                NotifyComplete();
            }

    Thursday, March 20, 2014 4:06 AM

All replies

  • Is this a Periodic or Resource Intensive task?

    If it's a Periodic task I would be concerned about the timeout from a web request exceeding the allowable execution time of the task.  It looks like you thought of that but you don't appear to be checking the return value to know if the wait timed-out...


    -Eric.

    Thursday, March 20, 2014 6:58 PM
  • Is this a Periodic or Resource Intensive task?

    If it's a Periodic task I would be concerned about the timeout from a web request exceeding the allowable execution time of the task.  It looks like you thought of that but you don't appear to be checking the return value to know if the wait timed-out...


    -Eric.

    It is a Periodic Task.

    Do I do something like this instead? (not sure f correct)

               try
                {
                    //wait for all tasks to complete before processing
    		Task.WaitAll(tasks,10000);
    			
                }
                catch (Exception ae)
                {
                }
    
    
    // Check to see how many tasks were unfinished
    int unfinished = tasks.Where(t => !t.IsCompleted).Count();
    
    if(unfinished  > 0){
    
                    var geoLocation = ((Task<Geoposition>)tasks[2]).Result;
    
                    var value1 = ((Task<Stream>)tasks[1]).Result;
                    var value2 = ((Task<Stream>)tasks[0]).Result;
    
                  //calculate some other stuff based on values and location
    //...				
    //...
    
    
    }else{
    
    //do other stuff to handle incomplete data
    
    }




    • Edited by Sal_S Thursday, March 20, 2014 7:22 PM
    Thursday, March 20, 2014 7:20 PM