Beispiel #1
0
        //Senario >> Same operation in different sources
        public static void Run()
        {
            int[] sampleHits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            //Shared Variable
            int hits = 0;

            Task<int> sideTask = Task.Factory.StartNew<int>(() =>
            {
                List<Task> tasks = new List<Task>();
                object Lock = new object();

                for (int i = 0; i < 1000; i++) //fire 10 parallel thread to update hits
                {
                    var data = new dataClass() { field1 = sampleHits[i % 10].ToString() };
                    Task t = Task.Factory.StartNew((arg1) =>
                    {
                        dataClass localdata = (dataClass)arg1;

                        for (int j = 0; j < 10000; j++)
                        {
                            //Critical Section
                            lock (Lock)
                            {
                                hits += Convert.ToInt32(localdata.field1);
                                //Note : ++ is not an atomic operation
                            }
                        }
                    },
                    data.clone()
                    );

                    tasks.Add(t);
                }

                Task.WaitAll(tasks.ToArray());
                return hits;
            });//Side Task End

            Task updateUI = sideTask.ContinueWith(ft =>
            {
                try
                {
                    int allHits = ft.Result;
                    Program.mainform.Controls["lbl_ConcurrencyResult"].Text = allHits.ToString();
                }
                catch (AggregateException ae)
                {
                    ae = ae.Flatten();
                    foreach (Exception ex in ae.InnerExceptions)
                    {
                        Program.mainform.Controls["lbl_ConcurrencyResult"].Text += ex.Message;
                    }
                }
            },
            TaskScheduler.FromCurrentSynchronizationContext()//Must run on current UI Thread
            );
        }
Beispiel #2
0
        //Senario >> Same operation in different sources

        public static void Run()
        {
            int[] sampleHits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            //Shared Variable
            int hits = 0;

            Task <int> sideTask = Task.Factory.StartNew <int>(() =>
            {
                List <Task> tasks = new List <Task>();
                object Lock       = new object();

                for (int i = 0; i < 1000; i++) //fire 10 parallel thread to update hits
                {
                    var data = new dataClass()
                    {
                        field1 = sampleHits[i % 10].ToString()
                    };
                    Task t = Task.Factory.StartNew((arg1) =>
                    {
                        dataClass localdata = (dataClass)arg1;

                        for (int j = 0; j < 10000; j++)
                        {
                            //Critical Section
                            lock (Lock)
                            {
                                hits += Convert.ToInt32(localdata.field1);
                                //Note : ++ is not an atomic operation
                            }
                        }
                    },
                                                   data.clone()
                                                   );

                    tasks.Add(t);
                }

                Task.WaitAll(tasks.ToArray());
                return(hits);
            });//Side Task End

            Task updateUI = sideTask.ContinueWith(ft =>
            {
                try
                {
                    int allHits = ft.Result;
                    Program.mainform.Controls["lbl_ConcurrencyResult"].Text = allHits.ToString();
                }
                catch (AggregateException ae)
                {
                    ae = ae.Flatten();
                    foreach (Exception ex in ae.InnerExceptions)
                    {
                        Program.mainform.Controls["lbl_ConcurrencyResult"].Text += ex.Message;
                    }
                }
            },
                                                  TaskScheduler.FromCurrentSynchronizationContext()//Must run on current UI Thread
                                                  );
        }