Ejemplo n.º 1
0
        /// <summary>
        /// The platform calls this method when an instance of your service is placed and ready to execute.
        /// </summary>
        /// <param name="cancellationToken">
        /// The system uses a cancellation token to signal your service when it's time to stop running.
        /// </param>
        /// <returns></returns>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            IReliableDictionary <int, CustomObject> dictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <int, CustomObject> >("dictionary1");

            int i = 1;

            while (!cancellationToken.IsCancellationRequested)
            {
                using (ITransaction tx = this.StateManager.CreateTransaction())
                {
                    await dictionary.AddOrUpdateAsync(tx, i, new CustomObject()
                    {
                        Data = i
                    }, (k, v) => new CustomObject()
                    {
                        Data = v.Data + 1
                    });

                    await tx.CommitAsync();
                }

                ServiceEventSource.Current.ServiceMessage(
                    this,
                    "Total Custom Objects: {0}. Data Average: {1}",
                    await dictionary.GetCountAsync(),
                    dictionary.Average(item => item.Value.Data));

                i = i % 10 == 0 ? 1 : i + 1;

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
            }
        }