Ejemplo n.º 1
0
        public void GetData_Pass_Object_Good()
        {
            var getter = new DataGetter();
            var data = new DataHolder() { Multipler = 1 };

            getter.NewData += (e) =>
            {
                // This simulates situation where something else changes public property
                if (e > 2)
                    data.Multipler = 4;

                Trace.WriteLine(Extensions.TraceMessage(e.ToString()));
            };

            getter.Ready += (s, e) =>
            {
                stopFlag.Set();
            };

            getter.GetData_Good(data);

            stopFlag.WaitOne();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Local copy of data is created, so changing data outside wont affect task
        /// </summary>
        /// <param name="data"></param>
        public void GetData_Good(DataHolder data)
        {
            var getDataTask = new Action<object>((dataH) =>
            {
                for (int i = 0; i < 10; i++)
                {
                    // Got data from somewhere
                    System.Threading.Thread.Sleep(100);

                    var handler = NewData;

                    if (handler != null)
                    {
                        handler(i * ((DataHolder)dataH).Multipler);
                    }
                }
            });

            // Create local copy of passed object
            var temp = new DataHolder() { Multipler = data.Multipler };

            var getData = new Task(getDataTask, temp);
            getData.ContinueWith(t => { SendReady(); });
            getData.Start();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// No local copy is created
        /// </summary>
        public void GetData_No_ThreadSafe2(DataHolder data)
        {
            var getDataTask = new Action<object>((dataH) =>
            {
                for (int i = 0; i < 10; i++)
                {
                    // Got data from somewhere
                    System.Threading.Thread.Sleep(100);

                    var handler = NewData;

                    if (handler != null)
                    {
                        handler(i * ((DataHolder)dataH).Multipler);
                    }
                }
            });

            var getData = new Task(getDataTask, data);
            getData.ContinueWith(t => { SendReady(); });
            getData.Start();
        }