Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DynamicBuilder.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Threading;
5 
6 using SiliconStudio.Core.Diagnostics;
7 
8 namespace SiliconStudio.BuildEngine
9 {
10  /// <summary>
11  /// This class allow to run a given <see cref="Builder"/> in a new thread. It will run a single <see cref="DynamicBuildStep"/>
12  /// that can be fed with a given <see cref="IBuildStepProvider"/>.
13  /// </summary>
14  public class DynamicBuilder : IDisposable
15  {
16  /// <summary>
17  /// The thread that will run an instance of <see cref="Builder"/> to build provided steps.
18  /// </summary>
19  private readonly Thread builderThread;
20  private readonly Builder builder;
21  private readonly DynamicBuildStep dynamicBuildStep;
22 
23  /// <summary>
24  /// Initializes a new instance of the <see cref="DynamicBuilder"/> class.
25  /// </summary>
26  /// <param name="name">The name of this instance. Used to name the created thread.</param>
27  /// <param name="builder">The builder to use.</param>
28  /// <param name="buildStepProvider">The build step provider to use.</param>
29  public DynamicBuilder(Builder builder, IBuildStepProvider buildStepProvider, string name = null)
30  {
31  this.builder = builder;
32  dynamicBuildStep = new DynamicBuildStep(buildStepProvider);
33  builderThread = new Thread(SafeAction.Wrap(BuilderThread)) { IsBackground = true };
34  if (!string.IsNullOrEmpty(name))
35  {
36  builderThread.Name = name;
37  }
38  }
39 
40  /// <summary>
41  /// Starts the thread an run the builder.
42  /// </summary>
43  public void Start()
44  {
45  builderThread.Start();
46  }
47 
48  /// <summary>
49  /// Cancels any build in progress and wait for the thread to exit.
50  /// </summary>
51  public void Dispose()
52  {
53  builder.CancelBuild();
54  dynamicBuildStep.NotifyNewWorkAvailable();
55  builderThread.Join();
56  }
57 
58  /// <summary>
59  /// Notify the <see cref="DynamicBuildStep"/> that a new build step is available.
60  /// </summary>
62  {
63  dynamicBuildStep.NotifyNewWorkAvailable();
64  }
65 
66  private void BuilderThread()
67  {
68  builder.Reset();
69  builder.Root.Add(dynamicBuildStep);
70  builder.Run(Builder.Mode.Build, true, false);
71  }
72  }
73 }
void Dispose()
Cancels any build in progress and wait for the thread to exit.
static ThreadStart Wrap(ThreadStart action, [CallerFilePath] string sourceFilePath="", [CallerMemberName] string memberName="", [CallerLineNumber] int sourceLineNumber=0)
Definition: SafeAction.cs:13
This class allow to run a given Builder in a new thread. It will run a single DynamicBuildStep that c...
void NotifyBuildStepAvailable()
Notify the DynamicBuildStep that a new build step is available.
DynamicBuilder(Builder builder, IBuildStepProvider buildStepProvider, string name=null)
Initializes a new instance of the DynamicBuilder class.
void Start()
Starts the thread an run the builder.
This interface describes a class that is capable of providing build steps to a DynamicBuildStep.