public void GenerateCommandInstantly() { IsButtonEnable = false; _executedComputer = 0; _totalComputer = 0; StartNotification(); List <ComputerModel> computers = new List <ComputerModel>(); Computers = GenerateDataFromNetwork(); try { TotalComputer = Computers.Count; foreach (var item in Computers) { ComputerDetection.GetAdditionalInfo(item); item.IsProgress = false; ExecutedComputer++; if (ExecutedComputer == TotalComputer) { Computers = new ObservableCollection <ComputerModel>(Computers.DistinctBy(p => p.Ip).ToList()); Computers = ComputerDetection.FilterComputer(Computers); if (AutoSave && Computers.Count > 0) { SaveCommand(); } EndNotification("Network searching has finished!!"); } } } catch (System.Exception ex) { EndNotification(ex.Message); return; } }
private void GenerateCommand() { IsButtonEnable = false; _executedComputer = 0; _totalComputer = 0; StartNotification(); List <ComputerModel> computers = new List <ComputerModel>(); Computers = GenerateDataFromNetwork(); System.Action DoInBackground = new System.Action(() => { try { TotalComputer = Computers.Count; foreach (var item in Computers) { System.Action ChildDoInBackground = new System.Action(() => { ComputerDetection.GetAdditionalInfo(item); }); System.Action ChildDoOnUiThread = new System.Action(() => { item.IsProgress = false; ExecutedComputer++; if (ExecutedComputer == TotalComputer) { Computers = new ObservableCollection <ComputerModel>(Computers.DistinctBy(p => p.Ip).ToList()); Computers = ComputerDetection.FilterComputer(Computers); if (AutoSave) { SaveCommand(); } EndNotification("Network searching has finished!!"); } }); Canceller = new CancellationTokenSource(); var t3 = Task.Factory.StartNew(() => { using (Canceller.Token.Register(Thread.CurrentThread.Abort)) { ChildDoInBackground(); } }, Canceller.Token); // when t1 is done run t1..on the Ui thread. var t4 = t3.ContinueWith(t => { using (Canceller.Token.Register(Thread.CurrentThread.Abort)) { ChildDoOnUiThread(); } }, Canceller.Token); } } catch (System.Exception ex) { EndNotification(ex.Message); return; } }); System.Action DoOnUiThread = new System.Action(() => { }); Canceller = new CancellationTokenSource(); // start the background task var t1 = Task.Factory.StartNew(() => { using (Canceller.Token.Register(Thread.CurrentThread.Abort)) { DoInBackground(); } }, Canceller.Token); // when t1 is done run t1..on the Ui thread. var t2 = t1.ContinueWith(t => { using (Canceller.Token.Register(Thread.CurrentThread.Abort)) { DoOnUiThread(); } }, Canceller.Token); //I assume BitmapFromUri is the slow step. //Now that we have our bitmap, now go to the main thread. }