} //end btnRunOnl_Click function

        /*Run Online - new method (inner loop only)*/
        private void Btn_NewOnline_Click(List <double> training_data, List <double> streaming_data, int period, int NLength, int maxEntry, int minEntry, int R, int D)
        {
            //create a new buffer. In this demo, the buffer is training_data (edit later):
            List <double> this_buffer = new List <double>();

            this_buffer.AddRange(training_data);

            // Store furnitures in RTree
            int              this_id_item     = int.MinValue;
            List <int>       this_id_itemList = new List <int>();
            RTree <int>      this_RTree       = new RTree <int>(maxEntry, minEntry);
            List <Rectangle> this_recList     = new List <Rectangle>();

            // Call 'Run Offline' for the first time, store results into variables ("this" object):
            List <double> discord_firsttime = RunOfflineMinDist(training_data, NLength, maxEntry, minEntry, R, D, ref this_id_item, ref this_id_itemList, ref this_recList, ref this_RTree, true);
            double        this_best_so_far_dist_TheMostDiscord = discord_firsttime[0];

            //store the last subsequence of the buffer:
            List <double> last_sub = this_buffer.GetRange(this_buffer.Count - NLength, NLength);


            // STREAMING: keep streaming until we have no more data points
            for (int index_stream = 0; index_stream < streaming_data.Count; index_stream++)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();///calc execution time

                double new_data_point = streaming_data[index_stream];

                //update last_sub at time t to get new_sub at time (t+1):
                last_sub.Add(new_data_point);
                last_sub.RemoveAt(0);
                List <double> new_sub = last_sub; // the same object

                // Insert the new entry into the tree:
                this_id_item++;

                // Add the new rec to the tree:
                Rectangle new_rec = new Rectangle(Utils.MathFuncs.PAA_Lower(new_sub, D, R).ToArray(), Utils.MathFuncs.PAA_Upper(new_sub, D, R).ToArray(), this_buffer.Count - NLength + 1 + index_stream);
                this_RTree.Add(new_rec, this_id_item);
                this_recList.Add(new_rec);
                this_id_itemList.Add(this_id_item);

                //remove the oldest entry:
                this_RTree.Delete(this_recList[index_stream], this_id_itemList[index_stream]);

                // update buffer:
                this_buffer.Add(new_data_point);
                this_buffer.RemoveAt(0);

                /* 'til now, we have already updated the tree.
                 * from now on, almost just copy the offline code:
                 */

                //Run new_online_algorithm:
                NewOnlineAlgorithm(this_buffer, 2 * period, index_stream, period, new_sub, this_RTree, NLength, D, R, maxEntry, minEntry, ref this_best_so_far_dist_TheMostDiscord);

                watch.Stop(); //stop timer
                long elapsedMs = watch.ElapsedMilliseconds;
                //this.txtExeTime.Text = elapsedMs.ToString();

                Console.WriteLine("ExeTime_Online=" + elapsedMs.ToString());

                Console.WriteLine("------------------------");
            } // end For loop (streaming)

            Console.WriteLine("--- Streaming's done (run out of data) ---");
        }
        /*Run Online (Liu method)*/
        private void btnRunOnl_Click(List <double> training_data, List <double> streaming_data, int period, int NLength, int maxEntry, int minEntry, int R, int D)
        {
            //create a new buffer. In this demo, the buffer is training_data (edit later):
            List <double> this_buffer = new List <double>();

            this_buffer.AddRange(training_data);

            // Store furnitures in RTree
            int              this_id_item     = int.MinValue;
            List <int>       this_id_itemList = new List <int>();
            RTree <int>      this_RTree       = new RTree <int>(maxEntry, minEntry);
            List <Rectangle> this_recList     = new List <Rectangle>();

            // Call 'Run Offline' for the first time, store results into variables ("this" object):
            List <double> discord_firsttime = RunOfflineMinDist(training_data, NLength, maxEntry, minEntry, R, D, ref this_id_item, ref this_id_itemList, ref this_recList, ref this_RTree, true);

            // Store current discord in buffer
            double this_best_so_far_dist = discord_firsttime[0];
            double this_best_so_far_loc  = discord_firsttime[1];

            //store the last subsequence of the buffer:
            List <double> last_sub = this_buffer.GetRange(this_buffer.Count - NLength, NLength);


            // STREAMING: keep streaming until we have no more data points
            for (int index_stream = 0; index_stream < streaming_data.Count; index_stream++)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();///calc execution time

                double new_data_point = streaming_data[index_stream];

                //update last_sub at time t to get new_sub at time (t+1):
                last_sub.Add(new_data_point);
                last_sub.RemoveAt(0);
                List <double> new_sub = last_sub; // the same object

                // Insert the new entry into the tree:
                this_id_item++;

                // Add the new rec to the tree:
                Rectangle new_rec = new Rectangle(Utils.MathFuncs.PAA_Lower(new_sub, D, R).ToArray(), Utils.MathFuncs.PAA_Upper(new_sub, D, R).ToArray(), this_buffer.Count - NLength + 1 + index_stream);
                this_RTree.Add(new_rec, this_id_item);
                this_recList.Add(new_rec);
                this_id_itemList.Add(this_id_item);

                //remove the oldest entry:
                this_RTree.Delete(this_recList[index_stream], this_id_itemList[index_stream]);

                //get the first sub before update the buffer (help to find the small match in Liu's method)
                List <double> first_sub = this_buffer.GetRange(0, NLength);

                // update buffer:
                this_buffer.Add(new_data_point);
                this_buffer.RemoveAt(0);

                /* 'til now, we have already updated the tree.
                 * from now on, almost just copy the offline code:
                 */

                //Method 1: just re-order the 2 loops:
                //RunOnline_Method1(this_buffer, index_stream);

                //Method 2: Liu's algorithm:
                //Note: Method_2 includes method_1 (case a)
                //RunOnline_LiuMethod_origin(this_buffer, index_stream, first_sub);

                //Method 3: motified_Liu's
                List <double> discord_Liu = RunOnline_LiuMethod_edited(this_buffer, index_stream, first_sub, this_RTree, this_best_so_far_dist, (int)this_best_so_far_loc, NLength, D);
                this_best_so_far_dist = discord_Liu[0];
                this_best_so_far_loc  = discord_Liu[1];

                watch.Stop(); //stop timer
                long elapsedMs = watch.ElapsedMilliseconds;
                //this.txtExeTime.Text = elapsedMs.ToString();

                Console.WriteLine("ExeTime_Online=" + elapsedMs.ToString());

                // Useless variable to pass parameter
                int              dumb          = 0;
                List <int>       dumb_list     = new List <int>();
                List <Rectangle> dumb_rectlist = new List <Rectangle>();
                RTree <int>      dumb_rtree    = new RTree <int>(maxEntry, minEntry);



                //call offline version to assure the results and compare the time executions:
                var           watch2          = System.Diagnostics.Stopwatch.StartNew();///calc execution time offline
                List <double> discord_offline = RunOfflineMinDist(training_data, NLength, maxEntry, minEntry, R, D, ref dumb, ref dumb_list, ref dumb_rectlist, ref dumb_rtree, false);
                double        this_best_so_far_dist_offline = discord_offline[0];

                watch2.Stop(); //stop timer
                long this_exeTimeOffline = watch2.ElapsedMilliseconds;

                //check:
                if (Math.Abs(this_best_so_far_dist - this_best_so_far_dist_offline) < 10e-7)
                {
                    Console.WriteLine("checked, ok.");
                    if (elapsedMs > this_exeTimeOffline)
                    {
                        Console.WriteLine("Online takes more time than Offline !!!");
                    }
                }
                else
                {
                    Console.WriteLine("this.best_so_far_dist = " + this_best_so_far_dist);
                    Console.WriteLine("this.best_so_far_dist_Offline = " + this_best_so_far_dist_offline);
                    Console.WriteLine("The results are different. Stop Streaming !!!");
                    return;
                }

                Console.WriteLine("------------------------");
            } // end For loop (streaming)

            Console.WriteLine("--- Streaming's done (run out of data) ---");
        } //end btnRunOnl_Click function