本文整理汇总了C#中System.Threading.Tasks.Task.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# Task.Execute方法的具体用法?C# Task.Execute怎么用?C# Task.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.Execute方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SimpleExecutionTest
public void SimpleExecutionTest ()
{
bool executed = false;
Task t = new Task (() => executed = true);
t.Execute (delegate {});
Assert.IsTrue (executed);
}
示例2: ExecutionWithChildCreationTest
public void ExecutionWithChildCreationTest ()
{
bool executed = false;
bool childRetrieved = false;
Task t = new Task (() => { Task.Factory.StartNew (() => Console.WriteLine ("execution")); executed = true; });
t.Execute ((child) => childRetrieved = child != null);
Assert.IsTrue (executed);
Assert.IsTrue (childRetrieved);
}
示例3: TryExecuteTaskInline
protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
{
if (task.IsCompleted)
return false;
if (task.Status == TaskStatus.WaitingToRun) {
task.Execute (null);
return true;
}
return false;
}
示例4: TryExecuteTaskInline
protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
{
task.Execute (null);
return true;
}
示例5: ParticipativeWorkerMethod
// Almost same as above but with an added predicate and treating one item at a time.
// It's used by Scheduler Participate(...) method for special waiting case like
// Task.WaitAll(someTasks) or Task.WaitAny(someTasks)
// Predicate should be really fast and not blocking as it is called a good deal of time
// Also, the method skip tasks that are LongRunning to avoid blocking (Task are not LongRunning by default)
public static void ParticipativeWorkerMethod (Task self,
ManualResetEventSlim predicateEvt,
int millisecondsTimeout,
IProducerConsumerCollection<Task> sharedWorkQueue,
ThreadWorker[] others,
ManualResetEvent evt,
Func<Task, Task, bool> checkTaskFitness)
{
const int stage1 = 5, stage2 = 0;
int tries = 50;
WaitHandle[] handles = null;
Watch watch = Watch.StartNew ();
if (millisecondsTimeout == -1)
millisecondsTimeout = int.MaxValue;
bool aggressive = false;
bool hasAutoReference = autoReference != null;
Action<Task> adder = null;
while (!predicateEvt.IsSet && watch.ElapsedMilliseconds < millisecondsTimeout && !self.IsCompleted) {
// We try to execute the self task as it may be the simplest way to unlock
// the situation
if (self.Status == TaskStatus.WaitingToRun) {
self.Execute (hasAutoReference ? autoReference.adder : (Action<Task>)null);
if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
return;
}
Task value;
// If we are in fact a normal ThreadWorker, use our own deque
if (hasAutoReference) {
var enumerable = autoReference.dDeque.GetEnumerable ();
if (adder == null)
adder = hasAutoReference ? autoReference.adder : (Action<Task>)null;
if (enumerable != null) {
foreach (var t in enumerable) {
if (t == null)
continue;
if (checkTaskFitness (self, t))
t.Execute (adder);
if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
return;
}
}
}
int count = sharedWorkQueue.Count;
// Dequeue only one item as we have restriction
while (--count >= 0 && sharedWorkQueue.TryTake (out value) && value != null) {
evt.Set ();
if (checkTaskFitness (self, value) || aggressive)
value.Execute (null);
else {
if (autoReference == null)
sharedWorkQueue.TryAdd (value);
else
autoReference.dDeque.PushBottom (value);
evt.Set ();
}
if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
return;
}
// First check to see if we comply to predicate
if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
return;
// Try to complete other work by stealing since our desired tasks may be in other worker
ThreadWorker other;
for (int i = 0; i < others.Length; i++) {
if ((other = others [i]) == autoReference || other == null)
continue;
if (other.dDeque.PopTop (out value) == PopResult.Succeed && value != null) {
evt.Set ();
if (checkTaskFitness (self, value) || aggressive)
value.Execute (null);
else {
if (autoReference == null)
sharedWorkQueue.TryAdd (value);
else
autoReference.dDeque.PushBottom (value);
evt.Set ();
}
}
if (predicateEvt.IsSet || watch.ElapsedMilliseconds > millisecondsTimeout)
return;
}
//.........这里部分代码省略.........
示例6: ExecuteTask
void ExecuteTask (Task value, ref bool result)
{
if (value == null)
return;
var saveCurrent = currentTask;
currentTask = value;
value.Execute (adder);
result = true;
currentTask = saveCurrent;
}
示例7: TryExecuteTask
internal protected bool TryExecuteTask (Task task)
{
if (task.IsCompleted)
return false;
if (task.Status == TaskStatus.WaitingToRun) {
task.Execute (null);
return true;
}
return false;
}
示例8: TryExecuteTask
protected internal bool TryExecuteTask(Task task)
{
if (task.IsCompleted)
return false;
if (task.Status == TaskStatus.WaitingToRun) {
task.Execute ();
if (task.WaitOnChildren ())
task.Wait ();
return true;
}
return false;
}
示例9: AddWork
public void AddWork (Task t)
{
ThreadPool.QueueUserWorkItem (delegate {
t.Execute (AddWork);
});
}