private void Field_PreloadVariable(object sender, PreloadVariableEventArgs e)
        {
            if (IsDataLoading)
            {
                IsPaddingTask = true;
                return;
            }

            if (IsReloading) return;

            if (CacheProvider != null)
                CurrentCache = CacheProvider;
            else
                CurrentCache = new DynamicCache();

            IsDataLoading = true;
            Task task = Task.Factory.StartNew(() =>
            {
                if (CurrentCache.GetOutOfDate(e.Keys, FieldName))
                {//如果不是在最新狀態就呼叫 GetDataAsync 讀取資料,並更新到 Cache 中。

                    if (e.Keys.Count() <= 0) return; //如果沒有資料就不執行。

                    IEnumerable<Value> values = GetDataAsync(e.Keys);
                    HashSet<string> resultSet = new HashSet<string>();
                    foreach (Value v in values)
                    {
                        CurrentCache.FillProperty(v.Id, FieldName, v.Val);
                        resultSet.Add(v.Id);
                    }

                    HashSet<string> clearSet = new HashSet<string>(e.Keys);
                    clearSet.ExceptWith(resultSet);

                    //將沒有回傳的 Id 值清空。
                    foreach (string id in clearSet)
                        CurrentCache.FillProperty(id, FieldName, string.Empty);
                }
            }, new CancellationToken(), TaskCreationOptions.PreferFairness, TaskScheduler.Default);

            task.ContinueWith((x) =>
            {
                IsDataLoading = false;

                if (IsPaddingTask)
                {
                    IsPaddingTask = false;
                    Field.Reload();
                    return;
                }

                if (x.Exception != null)
                    RTOut.WriteError(x.Exception);
                else
                {
                    IsReloading = true;
                    Field.Reload();
                    IsReloading = false;
                }
                CurrentCache = null;
            }, UISyncContext);
        }
Example #2
0
        private void Field_PreloadVariable(object sender, PreloadVariableEventArgs e)
        {
            if (IsDataLoading)
            {
                IsPaddingTask = true;
                return;
            }

            if (IsReloading)
            {
                return;
            }

            if (CacheProvider != null)
            {
                CurrentCache = CacheProvider;
            }
            else
            {
                CurrentCache = new DynamicCache();
            }

            IsDataLoading = true;
            Task task = Task.Factory.StartNew(() =>
            {
                if (CurrentCache.GetOutOfDate(e.Keys, FieldName))
                {//如果不是在最新狀態就呼叫 GetDataAsync 讀取資料,並更新到 Cache 中。
                    if (e.Keys.Count() <= 0)
                    {
                        return;                      //如果沒有資料就不執行。
                    }
                    IEnumerable <Value> values = GetDataAsync(e.Keys);
                    HashSet <string> resultSet = new HashSet <string>();
                    foreach (Value v in values)
                    {
                        CurrentCache.FillProperty(v.Id, FieldName, v.Val);
                        resultSet.Add(v.Id);
                    }

                    HashSet <string> clearSet = new HashSet <string>(e.Keys);
                    clearSet.ExceptWith(resultSet);

                    //將沒有回傳的 Id 值清空。
                    foreach (string id in clearSet)
                    {
                        CurrentCache.FillProperty(id, FieldName, string.Empty);
                    }
                }
            }, new CancellationToken(), TaskCreationOptions.PreferFairness, TaskScheduler.Default);

            task.ContinueWith((x) =>
            {
                IsDataLoading = false;

                if (IsPaddingTask)
                {
                    IsPaddingTask = false;
                    Field.Reload();
                    return;
                }

                if (x.Exception != null)
                {
                    RTOut.WriteError(x.Exception);
                }
                else
                {
                    IsReloading = true;
                    Field.Reload();
                    IsReloading = false;
                }
                CurrentCache = null;
            }, UISyncContext);
        }