本文整理汇总了C#中TaskCreationOptions类的典型用法代码示例。如果您正苦于以下问题:C# TaskCreationOptions类的具体用法?C# TaskCreationOptions怎么用?C# TaskCreationOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TaskCreationOptions类属于命名空间,在下文中一共展示了TaskCreationOptions类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
/// <summary>
/// Starts the periodic task.
/// </summary>
/// <param name="action">The action.</param>
/// <param name="intervalInMilliseconds">The interval in milliseconds.</param>
/// <param name="delayInMilliseconds">The delay in milliseconds, i.e. how long it waits to kick off the timer.</param>
/// <param name="duration">The duration.
/// <example>If the duration is set to 10 seconds, the maximum time this task is allowed to run is 10 seconds.</example></param>
/// <param name="maxIterations">The max iterations.</param>
/// <param name="synchronous">if set to <c>true</c> executes each period in a blocking fashion and each periodic execution of the task
/// is included in the total duration of the Task.</param>
/// <param name="cancelToken">The cancel token.</param>
/// <param name="periodicTaskCreationOptions"><see cref="TaskCreationOptions"/> used to create the task for executing the <see cref="Action"/>.</param>
/// <returns>A <see cref="Task"/></returns>
/// <remarks>
/// Exceptions that occur in the <paramref name="action"/> need to be handled in the action itself. These exceptions will not be
/// bubbled up to the periodic task.
/// </remarks>
public static Task Start(Action action,
int intervalInMilliseconds = Timeout.Infinite,
int delayInMilliseconds = 0,
int duration = Timeout.Infinite,
int maxIterations = -1,
bool synchronous = false,
CancellationToken cancelToken = new CancellationToken(),
TaskCreationOptions periodicTaskCreationOptions = TaskCreationOptions.None)
{
Stopwatch stopWatch = new Stopwatch();
Action wrapperAction = () =>
{
CheckIfCancelled(cancelToken);
action();
};
Action mainAction = () =>
{
try
{
MainPeriodicTaskAction(intervalInMilliseconds, delayInMilliseconds, duration, maxIterations,
cancelToken, stopWatch, synchronous, wrapperAction,
periodicTaskCreationOptions);
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
throw;
}
};
return Task.Factory.StartNew(mainAction, cancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}
示例2: Iterate
/// <summary>Asynchronously iterates through an enumerable of tasks.</summary>
/// <param name="factory">The target factory.</param>
/// <param name="source">The enumerable containing the tasks to be iterated through.</param>
/// <param name="cancellationToken">The cancellation token used to cancel the iteration.</param>
/// <param name="creationOptions">Options that control the task's behavior.</param>
/// <param name="scheduler">The scheduler to which tasks will be scheduled.</param>
/// <returns>A Task that represents the complete asynchronous operation.</returns>
public static Task Iterate(
this TaskFactory factory,
IEnumerable<object> source,
CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler)
{
return Iterate(factory, source, null, cancellationToken, creationOptions, scheduler);
}
示例3: ContinuationTaskFromTask
public ContinuationTaskFromTask(Task antecedent, Delegate action, object state, TaskCreationOptions creationOptions, InternalTaskOptions internalOptions)
: base(action, state, InternalCurrentIfAttached(creationOptions), default(CancellationToken), creationOptions, internalOptions, antecedent.Scheduler)
{
Contract.Requires(action is Action<Task> || action is Action<Task, object>, "Invalid delegate type in ContinuationTaskFromTask");
_antecedent = antecedent;
CapturedContext = ExecutionContext.Capture();
}
示例4: CreateAndExecuteTaskContinueWith
public static Task CreateAndExecuteTaskContinueWith(Action BodyMethod, Action ContinueMethod, TaskCreationOptions t_Options = TaskCreationOptions.None)
{
Task t_task = new Task(BodyMethod, t_Options);
t_task.ContinueWith(_ => ContinueMethod);
t_task.Start();
return t_task;
}
示例5: Run
/// <summary>
/// Runs the specified task.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="taskOption">The task option.</param>
/// <param name="exceptionHandler">The exception handler.</param>
/// <returns></returns>
public static Task Run(Action task, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
{
return Task.Factory.StartNew(task, taskOption).ContinueWith(t =>
{
exceptionHandler(t.Exception.InnerException);
}, TaskContinuationOptions.OnlyOnFaulted);
}
示例6: Iterate
/// <summary>Asynchronously iterates through an enumerable of tasks.</summary>
/// <param name="factory">The target factory.</param>
/// <param name="source">The enumerable containing the tasks to be iterated through.</param>
/// <param name="creationOptions">Options that control the task's behavior.</param>
/// <returns>A Task that represents the complete asynchronous operation.</returns>
public static Task Iterate(
this TaskFactory factory,
IEnumerable<object> source,
TaskCreationOptions creationOptions)
{
if (factory == null) throw new ArgumentNullException("factory");
return Iterate(factory, source, null, factory.CancellationToken, creationOptions, factory.GetTargetScheduler());
}
示例7: AddDenyChildAttach
public static TaskCreationOptions AddDenyChildAttach(TaskCreationOptions options)
{
#if NET4
options &= ~TaskCreationOptions.AttachedToParent;
return options;
#else
return options | TaskCreationOptions.DenyChildAttach;
#endif
}
示例8: EsuTimerBase
protected EsuTimerBase(int hour, int minute, int second, int interval, TaskCreationOptions creationOptions)
{
this.hour = hour;
this.minute = minute;
this.second = second;
this.interval = interval;
this.creationOptions = creationOptions;
displayFormat = "mm:ss";
}
示例9: Run
public static Task Run(Action<object> task, object state, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
{
return Task.Factory.StartNew(task, state, taskOption).ContinueWith(t =>
{
if (exceptionHandler != null)
exceptionHandler(t.Exception);
else
LogUtil.LogError(t.Exception);
}, TaskContinuationOptions.OnlyOnFaulted);
}
示例10: Run
/// <summary>
/// Runs the specified task.
/// </summary>
/// <param name="task">The task.</param>
/// <param name="taskOption">The task option.</param>
/// <param name="exceptionHandler">The exception handler.</param>
/// <returns></returns>
public static Task Run(Action task, TaskCreationOptions taskOption, Action<Exception> exceptionHandler)
{
return Task.Factory.StartNew(task, taskOption).ContinueWith(t =>
{
if (exceptionHandler != null)
exceptionHandler(t.Exception.InnerException);
else
LogFactoryProvider.GlobalLog.Error(t.Exception.InnerException);
}, TaskContinuationOptions.OnlyOnFaulted);
}
示例11: ProducerConsumerQueue
public ProducerConsumerQueue(int workerCount, TaskCreationOptions taskCreationOptions = TaskCreationOptions.None)
{
workerCount = Math.Max(1, workerCount);
m_Workers = new Task[workerCount];
for (int i = 0; i < workerCount; i++)
{
m_Workers[i] = Task.Factory.StartNew(Consume, taskCreationOptions);
}
}
示例12: StartNewDelayed
public static Task StartNewDelayed(this TaskFactory factory, int millisecondsDelay, Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions, TaskScheduler scheduler) {
if (factory == null)
throw new ArgumentNullException(nameof(factory));
if (millisecondsDelay < 0)
throw new ArgumentOutOfRangeException(nameof(millisecondsDelay));
if (action == null)
throw new ArgumentNullException(nameof(action));
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));
return factory.StartNewDelayed(millisecondsDelay, cancellationToken).ContinueWith(_ => action(), cancellationToken, TaskContinuationOptions.OnlyOnRanToCompletion, scheduler);
}
示例13: OnInterval
public static Task OnInterval(TimeSpan pollInterval, Action action, CancellationToken token,
TaskCreationOptions taskCreationOptions, TaskScheduler taskScheduler)
{
return Task.Factory.StartNew(() =>
{
for (;;)
{
if (token.WaitCancellationRequested(pollInterval))
break;
action();
}
}, token, taskCreationOptions, taskScheduler);
}
示例14: Run
public static Task Run(Action a, TaskCreationOptions creationOptions = TaskCreationOptions.None)
{
var task = new Task(a, creationOptions);
task.ContinueWith(t => { lock (m_runningTasks) m_runningTasks.Remove(t); });
lock (m_runningTasks)
{
m_runningTasks.Add(task);
}
task.Start();
return task;
}
示例15: Run
public static Task Run(Action<ILifetimeScope> action, CancellationToken cancellationToken, TaskCreationOptions options, TaskScheduler scheduler)
{
Guard.ArgumentNotNull(() => action);
Guard.ArgumentNotNull(() => scheduler);
var t = Task.Factory.StartNew(() => {
using (var container = EngineContext.Current.ContainerManager.Container.BeginLifetimeScope(AutofacLifetimeScopeProvider.HttpRequestTag))
{
action(container);
}
}, cancellationToken, options, scheduler);
return t;
}