private bool FilterByPrefix(ServerModel pc, IEnumerable prefixes) { foreach (string s in prefixes) { if (pc.Name.IndexOf(s, StringComparison.OrdinalIgnoreCase) == 0) { return(true); } } return(false); }
public MainWindow() { var myoption = new MyUserSettings(); InitializeComponent(); Mainwin.Title = "TimeGather is collecting data..."; Servermodelsdatacontext = new ServerModels(); Servermodelsdatacontext.ServerModelList = new ObservableCollection <ServerModel>(); Enableprefixfilter.Value = myoption.Isprefixfilterenabled; //StringCollection prefixes = myoption.Filters; //if (prefixes == null || prefixes.Count == 0) //{ // prefixes = new StringCollection() { "prefix1", "prefix2" }; // Properties.Settings.Default["prefixes"] = prefixes; // Properties.Settings.Default.Save(); //} //Loading = new ServerModel() { Name = "Loading..." }; ServerModelsDG.DataContext = Servermodelsdatacontext.ServerModelList; //_servermodelsdatacontext.ServerModelList.Add(Loading); var computers = GetComputer.GetComputers(); TimeQueryActionBlock = new ActionBlock <ServerModel>(sm => { ServerModel temp = ServerModel.Updatethesecondsoffset(sm); var tq = new TimeQuery(); temp.Source = tq.GetTimeSource(temp.Name); Application.Current.Dispatcher.BeginInvoke(new Action(() => this.Servermodelsdatacontext.ServerModelList.Add(temp))); }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = 128 } ); if (Enableprefixfilter.Value) { var f = JsonSerializer.Deserialize <string[]>(myoption.Filters); computers = (from pc in computers where FilterByPrefix(pc, f) == true select pc).ToList(); } foreach (var s in computers) { TimeQueryActionBlock.Post(s); } TimeQueryActionBlock.Complete(); //servermodelsdatacontext.ServerModelList = (from comp in computers.AsParallel() // select (ServerModel)Updatethesecondsoffset(comp)).ToList(); //ServerModelsDG.DataContext = servermodelsdatacontext.ServerModelList; }
public static ServerModel Updatethesecondsoffset(ServerModel comp) { if (comp is null) { throw new ArgumentNullException("comp", "Cannot be null"); } try { comp.SecondsOffset = (int)((DateTime)RemoteTOD.GetNow(comp.Name, true) - DateTime.Now).TotalSeconds; } catch (Exception ex) { comp.ErrorMessage = ex.Message; if (ex.Message.IndexOf("time", StringComparison.OrdinalIgnoreCase) > 0) { comp.SecondsOffset = 999; } } return(comp); }