Benutzer:MovGP0/Parallel/Tasks
| MovGP0 | Über mich | Hilfen | Artikel | Weblinks | Literatur | Zitate | Notizen | MSCert | Physik |
Parallel Tasks
Parallel InvokeParallel.Invoke(Method1, Method2, /*...*/);
equals to Task task1 = Task.Factory.StartNew(Method1);
Task task2 = TaskFactory.StartNew(Method2);
// ...
Task.WaitAll(task1, task2, /*...*/);
Task Cancellation Tokenusing(var cancellationTokenSource = new CancellationTokenSource())
{
var cancellationToken = cancellationTokenSource.Token;
var task = Task.Factory.StartNew(() =>
{
for(/*...*/)
{
cancellationToken.ThrowIfCancellationRequested();
// ...
}
}, cancellationToken);
cancellationTokenSource.Cancel();
}
Exception Handling
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
Speculative Executionpublic 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
// 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] AntipatternsVariables Captured by Closures
Disposing a Resource Needed by a TaskTask<string> task;
using(var file = new StringReader("text"))
{
task = Task<string>.Factory.StartNew(() => file.ReadLine());
}
Console.WriteLine(task.Result);
Thread Abort
Task DesignThreads
Life Cycle
Thread Pool
Performance considerations
Long Running Task
Referenzen
|
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.