Пример #1
0
        //builds and starts a sorting thread of the given sorting type.
        private void sort(Sorts the_sort)
        {
            //removes the old sorting thread if present
            if (my_drawable != null)
            {
                resetOldThread();
            }

            //gets the maximum input value from the user
            int max = getMax();

            if (max == -1)
            {
                return;
            }

            //setup new sorting thread
            my_drawable = new DrawnSort(the_sort, getInputArray(max), pnlDraw.Width, pnlDraw.Height, max, drawPanel, this);

            //make sure old memory is reclaimed (otherwise a leak can occur)
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //start the sorting thread
            my_drawable.start();
        }
        //stops old sorting threads.
        private void clearOldSorts()
        {
            //stop all threads
            Iterator <DrawnSort> it = my_sorts.values().iterator();

            while (it.hasNext())
            {
                DrawnSort ds = it.next();
                //if the old thread is still not stopped
                if (ds.active())
                {
                    ds.stop();
                    Cursor = Cursors.WaitCursor;

                    //loop while the thread is active
                    while (ds.active())
                    {
                        Application.DoEvents();
                        Thread.Sleep(100);
                    }

                    Cursor = Cursors.Default;
                }
            }

            //clear old entries
            my_sorts.clear();
        }
        /// <summary>
        /// Updates the progress bars on the window to display the finished percentage
        /// for each sort.
        /// </summary>
        public void updatePercentage()
        {
            //loop through all sorts
            for (int i = 0; i < my_sort_types.Length; i++)
            {
                //find the sort and update the sort's progress bar
                DSString    sort = new DSString(my_sort_types[i]);
                DrawnSort   ds   = my_sorts.get(sort);
                ProgressBar bar  = my_progress_bars.get(sort);
                bar.Value = (int)(100.0 * ds.percentage_complete);

                //if the sort is finished and has not been marked so
                if (ds.time_taken != 0 && !checkCompletedContains(sort))
                {
                    //record the finish time
                    my_completed.add(new FinishTime(sort, new DSLong(ds.time_taken)));
                }
            }

            //check for whether all labels are correct
            int  count = 0;
            long last  = 0;

            FinishTime[] times = my_completed.toArray();
            Sorting <FinishTime> .Sort(Sorts.InsertionSort, ref times); //sort the finish times

            for (int i = 0; i < times.Length; i++)
            {
                FinishTime sort_done = times[i];
                //this only advances count if a different finish time was found (it recognizes the ties)
                if (last != sort_done.finish_time.value)
                {
                    count++;
                }
                last = sort_done.finish_time.value;

                my_sort_labels.get(sort_done.sort).Text = sort_done.sort.value + " (" + count + ")";
            }

            //check for all sorts finished
            if (my_completed.size() == NUMBER_OF_SORTS)
            {
                Application.DoEvents();
                btnStartTheRace.Enabled = true;
                tmrUpdate.Enabled       = false;
                return;
            }
            Application.DoEvents();
        }