private static int GetRunningTaskCount() { int count = 0; var tasks = Tasks.Values.ToList(); for (int i = 0; i < tasks.Count; i++) { DataTask dt = tasks[i]; if (dt == null) { continue; } Task t = dt.Task; if (t.Status == TaskStatus.Canceled || t.Status == TaskStatus.Faulted) { continue; } if (t.Status == TaskStatus.Created || t.Status == TaskStatus.WaitingForActivation) { continue; } if (t.Status == TaskStatus.RanToCompletion) { continue; } count++; } return(count); }
public static object GetData(object owner, string dataName, TimeSpan updateInterval, object nullValue, Func <object> getData) { Dictionary <string, object> props = GetProperties(owner); object res = nullValue; if (!props.TryGetValue(dataName, out res)) { props.Add(dataName, nullValue); res = nullValue; } if (!object.Equals(res, nullValue)) { return(res); } DataTask dt = null; if (!Tasks.TryGetValue(GetKey(owner, dataName), out dt)) { CreateTask(owner, dataName, getData); } else { dt.Priority++; } UpdateTasks(); return(nullValue); }
private static void CreateTask(object owner, string dataName, Func <object> getData) { string key = GetKey(owner, dataName); Task <object> t = new Task <object>(() => { object res = getData(); SetValue(owner, dataName, res); OnTaskCompleted(owner); return(res); }); DataTask dt = new DataTask() { Task = t, Priority = 1 }; Tasks.Add(GetKey(owner, dataName), dt); if (GetRunningTaskCount() < MaxAllowedTaskCount) { t.Start(); } }