예제 #1
0
        private Task Upload(UploadTask uploadTask, IProgress <IUploadState> progress = null, CancellationToken cancellationToken = default(CancellationToken), PauseToken pauseToken = default(PauseToken))
        {
            var tcs = new TaskCompletionSource <bool>();

            uploadTask.AddOnCompleteListener(new OnCompleteHandlerListener(task =>
            {
                if (task.IsSuccessful)
                {
                    tcs.SetResult(true);
                }
                else
                {
                    tcs.SetException(ExceptionMapper.Map(task.Exception));
                }
            }));

            if (progress != null)
            {
                uploadTask.AddOnProgressListener(new OnProgressHandlerListener(snapshot =>
                {
                    var uploadTaskSnapshot = snapshot.JavaCast <UploadTask.TaskSnapshot>();
                    progress.Report(new UploadTaskSnapshotWrapper(uploadTaskSnapshot));
                }));
            }

            if (cancellationToken != default(CancellationToken))
            {
                cancellationToken.Register(() => uploadTask.Cancel());
            }

            if (pauseToken != default(PauseToken))
            {
                pauseToken.SetStorageTask(new StorageTaskWrapper(uploadTask));
            }

            return(tcs.Task);
        }