If you want to execute 3 tasks synchronously (one after the other), but you don’t want these tasks to block the UI thread. You can use the following code:
Note: Thread.Sleep(…) is a blocking operation and is used to mimic workload.
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTests { [TestClass] public class RliTests { public void DoWork1() { Console.WriteLine( string.Format("DoWork1 started at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); Thread.Sleep(1000); Console.WriteLine( string.Format("DoWork1 completed at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); } public void DoWork2() { Console.WriteLine( string.Format("DoWork2 started at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); Thread.Sleep(2000); Console.WriteLine( string.Format("DoWork2 completed at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); } public void DoWork3() { Console.WriteLine( string.Format("DoWork3 started at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); Thread.Sleep(3000); Console.WriteLine( string.Format("DoWork3 completed at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); } public void DoWorkOnUiTrhead() { for (int i = 0; i < 10; i++) { Console.WriteLine( string.Format("DoWorkOnUiTrhead started at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); Thread.Sleep(1000); Console.WriteLine( string.Format("DoWorkOnUiTrhead completed at [{0}] on Thread [{1}].", DateTime.Now.ToString("yyy-MM-dd HH:mm:ss.fff"), Thread.CurrentThread.ManagedThreadId)); } } /// <summary> /// Executing a list of tasks synchronously, without blocking the UI. /// </summary> [TestMethod] public void Test() { Console.WriteLine( string.Format("UI ThreadId [{0}]", Thread.CurrentThread.ManagedThreadId)); // Run task synchronously, but on an other thread, // so the UI thread is not blocked. var taskAsync = new Task ( RunTaskSyncrhonously, System.Threading.Tasks.TaskCreationOptions.LongRunning ); taskAsync.Start(); // Execute work in the UI thread, to prove the UI is not blocked. DoWorkOnUiTrhead(); } public void RunTaskSyncrhonously() { var tasks = new List<Task> { new Task(DoWork1, System.Threading.Tasks.TaskCreationOptions.LongRunning), new Task(DoWork2, System.Threading.Tasks.TaskCreationOptions.LongRunning), new Task(DoWork3, System.Threading.Tasks.TaskCreationOptions.LongRunning) }; foreach (Task task in tasks) { task.RunSynchronously(TaskScheduler.Default); } } } }
Output
As you can see in the output, the tasks are run on an other thread, so they don’t block the UI thread, but they are run synchronously (one after the other).
UI ThreadId [17]
DoWorkOnUiTrhead started at [2012-11-16 11:55:59.116] on Thread [17].
DoWork1 started at [2012-11-16 11:55:59.116] on Thread [18].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:00.116] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:00.116] on Thread [17].
DoWork1 completed at [2012-11-16 11:56:00.117] on Thread [18].
DoWork2 started at [2012-11-16 11:56:00.117] on Thread [18].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:01.117] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:01.117] on Thread [17].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:02.117] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:02.117] on Thread [17].
DoWork2 completed at [2012-11-16 11:56:02.118] on Thread [18].
DoWork3 started at [2012-11-16 11:56:02.118] on Thread [18].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:03.118] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:03.118] on Thread [17].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:04.118] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:04.118] on Thread [17].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:05.118] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:05.118] on Thread [17].
DoWork3 completed at [2012-11-16 11:56:05.119] on Thread [18].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:06.119] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:06.119] on Thread [17].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:07.120] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:07.120] on Thread [17].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:08.120] on Thread [17].
DoWorkOnUiTrhead started at [2012-11-16 11:56:08.120] on Thread [17].
DoWorkOnUiTrhead completed at [2012-11-16 11:56:09.120] on Thread [17].
Im unable to use Thread.sleep without blocking UI. Thread.sleep dont work. the execution cont. without having delay mentioned in Thread.Sleep(delay).