internal RemoteTask(Task task, RemoteCancellationTokenSource tokenSource) : this(task) { // We only want to attach once the task callback has run, otherwise we cause a memory leak since the below Dispose will never run, causing an eternal sponsorship tokenSource.Attach(); // We're responsible for cleaning up the cancellatin token source upon completion of the task task.ContinueWith((innerTask, state) => ((RemoteCancellationTokenSource)state).Dispose(), tokenSource, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); }
public static RemoteTask <TResult> Start <TResult>(Func <CancellationToken, Task <TResult> > callback, RemoteCancellationToken remoteToken) { if (!RemotingServices.IsObjectOutOfAppDomain(remoteToken)) { return(new RemoteTask <TResult>(callback(remoteToken.Token))); } var MyTokenSource = new RemoteCancellationTokenSource(remoteToken); return(new RemoteTask <TResult>(callback(MyTokenSource.Token), MyTokenSource)); }
public static RemoteTask Start(Func <CancellationToken, Task> callback, RemoteCancellationToken remoteToken) { if (!RemotingServices.IsObjectOutOfAppDomain(remoteToken)) { return(new RemoteTask(callback(remoteToken.Token))); } // Creates a cancellation token source in this AppDomain, and passes responsibility for cleaning it up to RemoteTask var MyTokenSource = new RemoteCancellationTokenSource(remoteToken); return(new RemoteTask(callback(MyTokenSource.Token), MyTokenSource)); }
internal void Attach(RemoteCancellationTokenSource tokenSource) { Debug.Assert(RemotingServices.IsObjectOutOfAppDomain(tokenSource), "Attempt to unwrap remote token source inside the owning AppDomain"); ImmutableInterlockedEx.Add(ref _TokenSources, tokenSource); // Cancel the remote token source if we're already cancelled // This will end up calling Detach, so do it at the end to ensure bookkeeping is consistent if (Token.IsCancellationRequested) { tokenSource.Cancel(); } }
internal void Detach(RemoteCancellationTokenSource tokenSource) { ImmutableInterlockedEx.Remove(ref _TokenSources, tokenSource); }