[C#]LevelUp.Lazy
namespace LazyDemo { class Program { static void Main(string[] args) { var count = 10; LevelUpLazy.Lazy<string>[] lazys = new LevelUpLazy.Lazy<string>[count]; for (int i = 0; i < count; i++) { lazys[i] = new LevelUpLazy.Lazy<string>(DoWork); }
lazys[3].ValueInited += new EventHandler(laz3_ValueInited); lazys[3].BeginInit();
lazys[2].BeginInit();
Stopwatch sw = Stopwatch.StartNew(); Console.WriteLine(“lazy1 result = {0}”, lazys[1].Value); Console.WriteLine(“lazy1 Elapsed Time = {0} ms”, sw.ElapsedMilliseconds);
sw.Restart(); Console.WriteLine(“lazy1 result = {0}”, lazys[1].Value); Console.WriteLine(“lazy1 Elapsed Time = {0} ms”, sw.ElapsedMilliseconds);
//Refresh Init sw.Restart(); lazys[1].ClearValue(); Console.WriteLine(“lazy1 result = {0}”, lazys[1].Value); Console.WriteLine(“lazy1 Elapsed Time = {0} ms”, sw.ElapsedMilliseconds);
sw.Restart(); Console.WriteLine(“lazy2 result = {0}”, lazys[2].Value); Console.WriteLine(“lazy2 Elapsed Time = {0} ms”, sw.ElapsedMilliseconds);
//Proxy lazys[4].InitialValue =“Temp Value”; for (int i = 0; i < 10; ++i) { Console.WriteLine(“lazy4 result = {0}”, lazys[4].Value); System.Threading.Thread.Sleep(200); }
//Background Init LevelUpLazy.Lazy<string>.EnableBackgroundInit = true; System.Threading.Thread.Sleep(5000); for (int i = 0; i < count; ++i) { if (lazys[i].IsValueCreated) Console.WriteLine(“lazy{0} result = {1}”, i, lazys[i].Value); } }
static void laz3_ValueInited(object sender, EventArgs e) { LevelUpLazy.Lazy<string> lazy3 = sender as LevelUpLazy.Lazy<string>; Console.WriteLine(“lazy3 result = {0}”, lazy3.Value); }
static string DoWork() { System.Threading.Thread.Sleep(1000); return Guid.NewGuid().ToString (); } } }