Benutzer:MovGP0/Parallel/Tasks
Zur Navigation springen
Zur Suche springen
MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | Programmierung | MSCert | Physik |
Parallel Tasks[Bearbeiten | Quelltext bearbeiten]
Parallel Invoke[Bearbeiten | Quelltext bearbeiten]Parallel.Invoke(Method1, Method2, /*...*/);
equals to Task task1 = Task.Factory.StartNew(Method1);
Task task2 = TaskFactory.StartNew(Method2);
// ...
Task.WaitAll(task1, task2, /*...*/);
Task Cancellation Token[Bearbeiten | Quelltext bearbeiten]using(var cancellationTokenSource = new CancellationTokenSource())
{
var cancellationToken = cancellationTokenSource.Token;
var task = Task.Factory.StartNew(() =>
{
for(/*...*/)
{
cancellationToken.ThrowIfCancellationRequested();
// ...
}
}, cancellationToken);
cancellationTokenSource.Cancel();
}
Exception Handling[Bearbeiten | Quelltext bearbeiten]
try
{
var task = Task.Factory.StartNew( () => /*...*/ );
// ...
task.Wait(); // join
}
catch(AggregateException aggregateException)
{
aggregateException.Handle(exception =>
{
if(exception is /* ... */)
{
// handle exception
return true; // exception was handled
}
return false; // exception was not handled
});
}
Wait's[Bearbeiten | Quelltext bearbeiten]
Speculative Execution[Bearbeiten | Quelltext bearbeiten]public static void SpeculativeInvoke(params Action<CancellationToken>[] actions)
{
var cancellationTokenSource = new CancellationTokenSource();
var token = cancellationTokenSource.Token();
var tasks = actions.Select(action => Task.Factory.StartNew(() => action(token), token));
Task.WaitAny(tasks);
cancellationTokenSource.Cancel();
try
{
Task.WaitAll(tasks);
}
catch (AggregateException aggregateException)
{
// remove OperationCanceledException
aggregateException.Flatten().Handle(e => e is OperationCanceledException);
}
finally
{
if(cancellationTokenSource != null)
{
cancellationTokenSource.Dispose();
}
}
}
Custom Thread Scheduling[Bearbeiten | Quelltext bearbeiten]
// current scheduler
TaskScheduler.Current
// default scheduler
TaskScheduler.Default
// scheduler for syncronizing with synchronization context of the current thread
// does not work with main thread
TaskScheduler.FromCurrentSyncronizationContext
Example is part of the ParallelExtensionExtras[1] Antipatterns[Bearbeiten | Quelltext bearbeiten]Variables Captured by Closures[Bearbeiten | Quelltext bearbeiten]
Disposing a Resource Needed by a Task[Bearbeiten | Quelltext bearbeiten]Task<string> task;
using(var file = new StringReader("text"))
{
task = Task<string>.Factory.StartNew(() => file.ReadLine());
}
Console.WriteLine(task.Result);
Thread Abort[Bearbeiten | Quelltext bearbeiten]
Task Design[Bearbeiten | Quelltext bearbeiten]Threads[Bearbeiten | Quelltext bearbeiten]
Life Cycle[Bearbeiten | Quelltext bearbeiten]
Thread Pool[Bearbeiten | Quelltext bearbeiten]
Performance considerations[Bearbeiten | Quelltext bearbeiten]
Long Running Task[Bearbeiten | Quelltext bearbeiten]
Referenzen[Bearbeiten | Quelltext bearbeiten]
|