summaryrefslogtreecommitdiff
path: root/lib/infrastructure/threading/BackgroundThreadFactory.cs
blob: a42e4d95ac785bd64239c70a7922bce2713f236f (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
48
49
using System;
using jive.infrastructure.container;
using jive.utility;

namespace jive.infrastructure.threading
{
  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() {}
    }
  }
}