CSharpAnalytics.Network.BackgroundHttpRequester.Start C# (CSharp) Method

Start() public method

Start the BackgroundHttpRequester with a given upload interval and a list of previously unrequested URIs.
public Start ( System.TimeSpan uploadInterval, IEnumerable previouslyUnrequested = null ) : void
uploadInterval System.TimeSpan How often to send the contents of the queue.
previouslyUnrequested IEnumerable List of previously unrequested URIs obtained last time the requester was stopped.
return void
        public void Start(TimeSpan uploadInterval, IEnumerable<Uri> previouslyUnrequested = null)
        {
            if (IsStarted)
                throw new InvalidOperationException(String.Format("Cannot start a {0} when already started", GetType().Name));

            if (previouslyUnrequested != null)
                priorRequests = new ConcurrentQueue<Uri>(previouslyUnrequested);

            cancellationTokenSource = new CancellationTokenSource();
            currentUploadInterval = uploadInterval;

            backgroundSender = Task.Factory.StartNew(RequestLoop, cancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
        }

Usage Example

 /// <summary>
 /// Start the requester with any unsent URIs from the last application run.
 /// </summary>
 /// <param name="uploadInterval">How often to send URIs to analytics.</param>
 /// <returns>Task that completes when the requester is ready.</returns>
 private static async Task StartRequesterAsync(TimeSpan uploadInterval)
 {
     requester = new BackgroundHttpRequester(PreprocessHttpRequest);
     var previousRequests = await LocalFolderContractSerializer<List<Uri>>.RestoreAsync(RequestQueueFileName);
     requester.Start(uploadInterval, previousRequests);
 }