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