private void GetComputerActivity(int labId) { try { List <Computer> compList = _aController.GetLabComputers(labId); List <Task> tasks = new List <Task>(); try { foreach (var comp in compList) { // Task t = Task.Factory.StartNew(() => // { try { //check for the last activity of the computer ActivityType lastAct = _aController.CurrentActivityDetails(comp.ComputerId); // if the last activity is user activity from the day before- split to two activities for each day if (!lastAct.Equals(ActivityType.On)) { _aController.SplitActivity(comp.ComputerId); } string logedOn = IsComputerLogedOn(comp.ComputerName, _aController.GetCompDomain(comp.ComputerId)); // TBD- change the ugly T: isComputerLogedOn should return a boolean (in the func use trim) if (logedOn.Contains("T")) { String userName = ""; userName = GetUserLogOn(comp.ComputerName, _aController.GetCompDomain(comp.ComputerId)); if (!Regex.Replace(userName, @"\s+", "").Equals("")) { ////computer is taken by user 'userName'- compare with last activity and update if neseccery if (lastAct == ActivityType.On) { //create user activity AddNewActivity(comp.ComputerId, ActivityType.User, userName); } else if (_aController.GetCurrentComputerUser(comp.ComputerId) != userName) { //close current activity and create new user activity CloseActivity(comp.ComputerId); AddNewActivity(comp.ComputerId, ActivityType.User, userName); } } else { //computer is avilable- compare with last activity and update if neseccery //if last activity is null- no need to update. if not close the last activity if (lastAct != ActivityType.On) { CloseActivity(comp.ComputerId); } } } else { //computer is disconnected- compare with last activity and update if neseccery if (lastAct == ActivityType.On) { //create off activity AddNewActivity(comp.ComputerId, ActivityType.Off, null); } else if (lastAct != ActivityType.Off) { //close current activity and create new off activity CloseActivity(comp.ComputerId); AddNewActivity(comp.ComputerId, ActivityType.Off, null); } // else- it already in off mode- dont change anything } } catch (Exception ex) { Debug.WriteLine("faild on computer:" + comp.ComputerName + ". error is:" + ex.Message); } // }); // tasks.Add(t); } } catch (Exception ex) { Debug.WriteLine("faild in computer foreach. error is:" + ex.Message); } Task.WaitAll(tasks.ToArray()); } catch (Exception ex) { Debug.WriteLine("faild in computer waitall. error is:" + ex.Message); } }