Exemple #1
0
 private IEnumerable <DataModel> StreamProcessPhase1(IEnumerable <DataModel> models)
 {
     foreach (var model in models)
     {
         DataModelHelper.ProcessPhase1(model);
         yield return(model);
     }
 }
Exemple #2
0
 public void Test1()
 {
     foreach (var model in GetModels())
     {
         DataModelHelper.ProcessPhase1(model);
         DataModelHelper.ProcessPhase2(model);
         DataModelHelper.ProcessPhase3(model);
     }
 }
 /// <summary>
 /// 串流處理:處理完一個物件的phase1,2,3才抓下一個物件
 /// WIP只會有一個
 /// </summary>
 private static void Demo2()
 {
     foreach (var model in GetModels())
     {
         DataModelHelper.ProcessPhase1(model);
         DataModelHelper.ProcessPhase2(model);
         DataModelHelper.ProcessPhase3(model);
     }
 }
        public static IEnumerable <DataModel> BlockedCollectionProcessPhase1(IEnumerable <DataModel> models)
        {
            BlockingCollection <DataModel> result = new BlockingCollection <DataModel>(BLOCKING_COLLECTION_CAPACITY);

            Task.Run(() =>
            {
                foreach (var model in models)
                {
                    DataModelHelper.ProcessPhase1(model);
                    result.Add(model);
                }
                result.CompleteAdding();
            });
            return(result.GetConsumingEnumerable());
        }
Exemple #5
0
        private static void Main(string[] args)
        {
            var json       = JsonSerializer.Create();
            var jsonreader = new JsonTextReader(Console.In);

            jsonreader.SupportMultipleContent = true;

            while (jsonreader.Read())
            {
                var d = json.Deserialize <DataModel>(jsonreader);

                DataModelHelper.ProcessPhase1(d);

                json.Serialize(Console.Out, d);
            }
        }
Exemple #6
0
        private IEnumerable <DataModel> StreamAsyncProcessPhase1(IEnumerable <DataModel> models)
        {
            Task <DataModel> previous_result = null;

            foreach (var model in models)
            {
                if (previous_result != null)
                {
                    yield return(previous_result.Result);
                }
                previous_result = Task.Run <DataModel>(() => { DataModelHelper.ProcessPhase1(model); return(model); });
            }
            if (previous_result != null)
            {
                yield return(previous_result.GetAwaiter().GetResult());
            }
        }
        /// <summary>
        /// 批次處理:依序執行所有物件的phase1,再做所有的phase2,最後做所有的phase3
        /// WIP與任務數量呈正比
        /// </summary>
        private static void Demo1()
        {
            DataModel[] models = GetModels().ToArray();

            foreach (var model in models)
            {
                DataModelHelper.ProcessPhase1(model);
            }

            foreach (var model in models)
            {
                DataModelHelper.ProcessPhase2(model);
            }

            foreach (var model in models)
            {
                DataModelHelper.ProcessPhase3(model);
            }
        }
        public static IEnumerable <DataModel> StreamAsyncProcessPhase1(IEnumerable <DataModel> models)
        {
            Task <DataModel> previous_result = null;

            foreach (var model in models)
            {
                // 再送下一筆之前,先判斷上一筆是否完成作業
                // 跟這個迴圈要資料的phase2,就可以等上一筆做完之後,直接return給phase2
                // (因為是管線的關係,每一個phase只能有一個正在執行,所以要等上一筆做完)
                // 然後這個迴圈就可以在去抓下一筆資料來做
                if (previous_result != null)
                {
                    yield return(previous_result.GetAwaiter().GetResult());
                }
                previous_result = Task.Run <DataModel>(() => { DataModelHelper.ProcessPhase1(model); return(model); });
            }
            if (previous_result != null)
            {
                yield return(previous_result.GetAwaiter().GetResult());
            }
        }