當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。