当前位置: 首页>>代码示例>>C#>>正文


C# Task.Execute方法代码示例

本文整理汇总了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);
		}
开发者ID:carrie901,项目名称:mono,代码行数:8,代码来源:MonoTaskExtensionsTests.cs

示例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);
		}
开发者ID:carrie901,项目名称:mono,代码行数:11,代码来源:MonoTaskExtensionsTests.cs

示例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;
		}
开发者ID:ngraziano,项目名称:mono,代码行数:12,代码来源:SynchronizationContextScheduler.cs

示例4: TryExecuteTaskInline

		protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
		{
			task.Execute (null);
			return true;
		}
开发者ID:koush,项目名称:mono,代码行数:5,代码来源:Scheduler.cs

示例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;
				}

//.........这里部分代码省略.........
开发者ID:carrie901,项目名称:mono,代码行数:101,代码来源:ThreadWorker.cs

示例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;
		}
开发者ID:carrie901,项目名称:mono,代码行数:11,代码来源:ThreadWorker.cs

示例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;
		}
开发者ID:markrendle,项目名称:mono,代码行数:12,代码来源:TaskScheduler.cs

示例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;
        }
开发者ID:RainsSoft,项目名称:dotnet-tpl35,代码行数:15,代码来源:TaskScheduler.cs

示例9: AddWork

		public void AddWork (Task t)
		{
			ThreadPool.QueueUserWorkItem (delegate {
				t.Execute (AddWork);
			});
		}
开发者ID:TheRealDuckboy,项目名称:mono-soc-2008,代码行数:6,代码来源:ThreadPoolScheduler.cs


注:本文中的System.Threading.Tasks.Task.Execute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。