Exemplo n.º 1
0
        /// <summary>异步注册一个初始化后的机器信息实例</summary>
        /// <param name="msRefresh">定时刷新实时数据,默认0ms不刷新</param>
        /// <returns></returns>
        public static Task <MachineInfo> RegisterAsync(Int32 msRefresh = 0)
        {
            return(Task.Factory.StartNew(() =>
            {
                // 文件缓存,加快机器信息获取
                var file = XTrace.TempPath.CombinePath("machine.info").GetFullPath();
                if (Current == null && File.Exists(file))
                {
                    try
                    {
                        Current = File.ReadAllText(file).ToJsonEntity <MachineInfo>();
                    }
                    finally { }
                }

                var mi = Current ?? new MachineInfo();

                mi.Init();
                File.WriteAllText(file.EnsureDirectory(true), mi.ToJson(true));

                // 定时刷新
                if (msRefresh > 0)
                {
                    mi._timer = new TimerX(s => mi.GetRuntime(), null, msRefresh, msRefresh)
                    {
                        Async = true
                    }
                }
                ;

                Current = mi;

                // 注册到对象容器
                ObjectContainer.Current.Register <MachineInfo>(mi);

                return mi;
            }));
        }