summaryrefslogtreecommitdiff
path: root/lib/BackgroundThreadFactory.cs
blob: 6ebcddc721757117400ad968e01ac0fd5ddcde0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;

namespace jive
{
  public interface IBackgroundThreadFactory
  {
    BackgroundThread create_for<CommandToExecute>() where CommandToExecute : DisposableCommand;
    BackgroundThread create_for(Action action);
  }

  public class BackgroundThreadFactory : IBackgroundThreadFactory
  {
    readonly DependencyRegistry registry;

    public BackgroundThreadFactory(DependencyRegistry registry)
    {
      this.registry = registry;
    }

    public BackgroundThread create_for<CommandToExecute>() where CommandToExecute : DisposableCommand
    {
      return new WorkderBackgroundThread(registry.get_a<CommandToExecute>());
    }

    public BackgroundThread create_for(Action action)
    {
      return new WorkderBackgroundThread(new AnonymousDisposableCommand(action));
    }

    class AnonymousDisposableCommand : DisposableCommand
    {
      readonly Action action;

      public AnonymousDisposableCommand(Action action)
      {
        this.action = action;
      }

      public void run()
      {
        action();
      }

      public void Dispose() {}
    }
  }
}