main
 1using System;
 2
 3namespace jive
 4{
 5  public interface IBackgroundThreadFactory
 6  {
 7    BackgroundThread create_for<CommandToExecute>() where CommandToExecute : DisposableCommand;
 8    BackgroundThread create_for(Action action);
 9  }
10
11  public class BackgroundThreadFactory : IBackgroundThreadFactory
12  {
13    readonly DependencyRegistry registry;
14
15    public BackgroundThreadFactory(DependencyRegistry registry)
16    {
17      this.registry = registry;
18    }
19
20    public BackgroundThread create_for<CommandToExecute>() where CommandToExecute : DisposableCommand
21    {
22      return new WorkderBackgroundThread(registry.get_a<CommandToExecute>());
23    }
24
25    public BackgroundThread create_for(Action action)
26    {
27      return new WorkderBackgroundThread(new AnonymousDisposableCommand(action));
28    }
29
30    class AnonymousDisposableCommand : DisposableCommand
31    {
32      readonly Action action;
33
34      public AnonymousDisposableCommand(Action action)
35      {
36        this.action = action;
37      }
38
39      public void run()
40      {
41        action();
42      }
43
44      public void Dispose() {}
45    }
46  }
47}