public interface IComponent1 { } public interface IComponent2 { } public interface IComponent3 { } public interface IComponent4 { }
public class HeavyComponent1 : IComponent1 { public void Initialize(int initializationDelaySeconds) { Thread.Sleep(1000 * initializationDelaySeconds); // } } public class HeavyComponent2 : IComponent2 { public void Initialize(int initializationDelaySeconds) { Thread.Sleep(1000 * initializationDelaySeconds); // } } public class HeavyComponent3 : IComponent3 { public void Initialize(int initializationDelaySeconds) { Thread.Sleep(1000 * initializationDelaySeconds); // } } public class HeavyComponent4 : IComponent4 { public HeavyComponent4(IComponent1 componentInstance1, IComponent2 componentInstance2, IComponent3 componentInstance3) { // } public void Initialize(int initializationDelaySeconds) { Thread.Sleep(1000 * initializationDelaySeconds); // } }
public void RegisterComponents() { var heavyComponent1 = new HeavyComponent1(); heavyComponent1.Initialize(1); this.RegisterInstance<IComponent1>(heavyComponent1); var heavyComponent2 = new HeavyComponent2(); heavyComponent2.Initialize(2); this.RegisterInstance<IComponent2>(heavyComponent2); var heavyComponent3 = new HeavyComponent3(); heavyComponent3.Initialize(3); this.RegisterInstance<IComponent3>(heavyComponent3); var heavyComponent4 = new HeavyComponent4(heavyComponent1, heavyComponent2, heavyComponent3); heavyComponent4.Initialize(4); this.RegisterInstance<IComponent1>(heavyComponent1); }
public async Task RegisterAsync() { var syncReg = new Object(); var heavyComponent1Task = Task.Run(() => { var heavyComponent1 = new HeavyComponent1(); heavyComponent1.Initialize(1); lock (syncReg) { this.RegisterInstance<IComponent1>(heavyComponent1); } return heavyComponent1; }); var heavyComponent2Task = Task.Run(() => { var heavyComponent2 = new HeavyComponent2(); heavyComponent2.Initialize(2); lock (syncReg) { this.RegisterInstance<IComponent2>(heavyComponent2); } return heavyComponent2; }); var heavyComponent3Task = Task.Run(() => { var heavyComponent3 = new HeavyComponent3(); heavyComponent3.Initialize(3); lock (syncReg) { this.RegisterInstance<IComponent3>(heavyComponent3); } return heavyComponent3; }); var heavyComponent4Task = Task.Run(async () => { var heavyComponent4 = new HeavyComponent4(await heavyComponent1Task, await heavyComponent2Task, await heavyComponent3Task); heavyComponent4.Initialize(4); lock (syncReg) { this.RegisterInstance<IComponent4>(heavyComponent4); } return heavyComponent4; }); await Task.WhenAll(heavyComponent1Task, heavyComponent2Task, heavyComponent3Task, heavyComponent4Task); }
private Task<TInterface> RegisterInstanceAsync<TInterface>(Func<TInterface> registration) { var result = Task.Run(() => { var instance = registration(); lock (_syncReg) { this.RegisterInstance(instance); } return instance; }); _registrationTasks.Add(result); // return result; } private Task<TInterface> RegisterInstanceAsync<TInterface>(Func<Task<TInterface>> registration) { return RegisterInstanceAsync(() => registration().Result); }
private async Task FinishRegistrationTasks() { await Task.WhenAll(_registrationTasks); }
public async Task RegisterComponentsAsync() { var heavyComponent1Task = RegisterInstanceAsync<IComponent1>(() => { var result = new HeavyComponent1(); result.Initialize(1); return result; }); var heavyComponent2Task = RegisterInstanceAsync<IComponent2>(() => { var result = new HeavyComponent2(); result.Initialize(2); return result; }); var heavyComponent3Task = RegisterInstanceAsync<IComponent3>(() => { var result = new HeavyComponent3(); result.Initialize(3); return result; }); var heavyComponent4Task = RegisterInstanceAsync<IComponent4>(async () => { var result = new HeavyComponent4(await heavyComponent1Task, await heavyComponent2Task, await heavyComponent3Task); result.Initialize(4); return result; }); await FinishRegistrationTasks(); }
Source: https://habr.com/ru/post/247331/
All Articles