Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

Task Factories

A task factory is represented by the System.Threading.Tasks.TaskFactory class, which creates Task objects, or the System.Threading.Tasks.TaskFactory<TResult> class, which creates Task<TResult> objects. Both classes contain methods that you can use to:

The Task class has a static property that represents the default TaskFactory. Typically, TaskFactory methods are invoked by using the Factory property, as shown in the following example.

[Visual Basic]

Dim taskA as Task = Task.Factory.StartNew(Sub( ...))

[C#]

Task taskA = Task.Factory.StartNew( () => ...);

In most scenarios, you do not have to derive a new class from TaskFactory. However, it is sometimes useful to configure a new TaskFactory and then use it to specify certain options or to associate tasks with a custom scheduler. The following example shows how to configure a new TaskFactory that creates tasks that all use the specified TaskScheduler and have the specified TaskCreationOptions options.

class Program
{
   static CancellationTokenSource cts = new CancellationTokenSource();

   static TaskFactory _factory = new TaskFactory(
      cts.Token,
      TaskCreationOptions.PreferFairness,
      TaskContinuationOptions.ExecuteSynchronously,
      new MyScheduler());

   static void Main(string[] args)
   {
      var t2 = _factory.StartNew(() => DoWork());
   }

   static void DoWork() {/*...*/ }
}
Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.