显示运行时的形式变化使用线程

发布于 2024-11-02 01:05:38 字数 3156 浏览 1 评论 0原文

我想为 3 种排序(冒泡排序、快速排序和合并排序)创建 3 个线程来显示哪个函数快速排序?为了展示这一点,我制作了 3 个按钮数组(对于每个 func.1 arr)&我想按身高对它们进行排序。但我有两个重要的问题: 1-如何在运行时显示按钮的交换和变化?表格将在排序完成后显示,但我想看到在运行时显示这些更改。 2-我无法使用线程来同时运行排序并发。

这是我现在的代码:

namespace threade {

public partial class Form1 : Form
{
    Button[] btns1;
    Button[] btns2;
    Button[] btns3;

    public Form1()
    {
        InitializeComponent();

        btns1 =new Button[] { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16};
        btns2 = new Button[] { button17, button18, button19, button20, button21, button22, button23, button24, button25, button26, button27, button28, button29, button30,  button31, button32 };
       btns3 = new Button[] { button33, button34, button35, button36, button37, button38, button39, button40, button41, button42, button43, button44, button45, button46, button47, button48 };
        foreach (Button btn in btns1)
        {
            btn.Text = btn.Size.Height.ToString();
        }
        foreach (Button btn in btns2)
        {
            btn.Text = btn.Size.Height.ToString();
        }
        foreach (Button btn in btns3)
        {
            btn.Text = btn.Size.Height.ToString();
        }

        Thread t1 = new Thread(new ThreadStart(sort1));
        Thread t2 = new Thread(new ThreadStart(sort2));
        Thread t3 = new Thread(new ThreadStart(sort3));





    }

    void sort1() {
        bsort(btns1, 16);
    }

    void sort2()
    {
        qsort(0, 15, btns2);
    }


    void sort3()
    {
        mgsort(0, 15, btns3);
    }

    Button aa = new Button(); Button aa2 = new Button();
    void swap(int i,int j,Button[]btns)
    {

        aa = btns[i];
        btns[i] = btns[j];
        btns[j] = aa;

        int t = btns[i].Location.X;
        btns[i].Location = new Point(btns[j].Location.X, btns[i].Location.Y);
        btns[j].Location = new Point(t, btns[j].Location.Y);



    }
    int part(int l, int h,Button[] btns)
    {
        int i, j=l;
        int p = btns[l].Size.Height;
        for (i = l +1; i <= h; i++)
            if (btns[i].Size.Height < p)
            {
                j++;
                swap(i, j, btns);
            }
        int s = j;
        swap(l, j, btns);
        return s;
  }
    void qsort(int l, int h, Button[] btns)
    {

        if (h > l)
        {
            int p = part(l, h, btns);
            qsort(l, p-1,btns);
            qsort(p+1, h,btns);

        }

   }

    void bsort(Button[] btns,int n)
    {
        for(int i=0;i<n-1;i++)
            for(int j=i+1;j<n;j++)
                if (btns[j].Size.Height < btns[i].Size.Height)
                {
                    aa2 = btns[i];
                    btns[i] = btns[j];
                    btns[j] = aa2;

                    int t = btns[i].Location.X;
                    btns[i].Location = new Point(btns[j].Location.X, btns[i].Location.Y);
                    btns[j].Location = new Point(t, btns[j].Location.Y);
                    Thread.Sleep(10);



                }

   }

}

I want to create 3 threads for 3 sort(bubble sort,quick sort & merge sort) to show which function sort quickly? for showing this I make 3 array of buttons(for every func. 1 arr) & I want to sort them with their height . but I have 2 important problem:
1- how show swapping and changes in buttons on run time? the form will be show after sort done but I want to see showing these change during runtime.
2- I can't work with threads to run sorts concurrence together .

This is my code now:

namespace threade
{

public partial class Form1 : Form
{
    Button[] btns1;
    Button[] btns2;
    Button[] btns3;

    public Form1()
    {
        InitializeComponent();

        btns1 =new Button[] { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16};
        btns2 = new Button[] { button17, button18, button19, button20, button21, button22, button23, button24, button25, button26, button27, button28, button29, button30,  button31, button32 };
       btns3 = new Button[] { button33, button34, button35, button36, button37, button38, button39, button40, button41, button42, button43, button44, button45, button46, button47, button48 };
        foreach (Button btn in btns1)
        {
            btn.Text = btn.Size.Height.ToString();
        }
        foreach (Button btn in btns2)
        {
            btn.Text = btn.Size.Height.ToString();
        }
        foreach (Button btn in btns3)
        {
            btn.Text = btn.Size.Height.ToString();
        }

        Thread t1 = new Thread(new ThreadStart(sort1));
        Thread t2 = new Thread(new ThreadStart(sort2));
        Thread t3 = new Thread(new ThreadStart(sort3));





    }

    void sort1() {
        bsort(btns1, 16);
    }

    void sort2()
    {
        qsort(0, 15, btns2);
    }


    void sort3()
    {
        mgsort(0, 15, btns3);
    }

    Button aa = new Button(); Button aa2 = new Button();
    void swap(int i,int j,Button[]btns)
    {

        aa = btns[i];
        btns[i] = btns[j];
        btns[j] = aa;

        int t = btns[i].Location.X;
        btns[i].Location = new Point(btns[j].Location.X, btns[i].Location.Y);
        btns[j].Location = new Point(t, btns[j].Location.Y);



    }
    int part(int l, int h,Button[] btns)
    {
        int i, j=l;
        int p = btns[l].Size.Height;
        for (i = l +1; i <= h; i++)
            if (btns[i].Size.Height < p)
            {
                j++;
                swap(i, j, btns);
            }
        int s = j;
        swap(l, j, btns);
        return s;
  }
    void qsort(int l, int h, Button[] btns)
    {

        if (h > l)
        {
            int p = part(l, h, btns);
            qsort(l, p-1,btns);
            qsort(p+1, h,btns);

        }

   }

    void bsort(Button[] btns,int n)
    {
        for(int i=0;i<n-1;i++)
            for(int j=i+1;j<n;j++)
                if (btns[j].Size.Height < btns[i].Size.Height)
                {
                    aa2 = btns[i];
                    btns[i] = btns[j];
                    btns[j] = aa2;

                    int t = btns[i].Location.X;
                    btns[i].Location = new Point(btns[j].Location.X, btns[i].Location.Y);
                    btns[j].Location = new Point(t, btns[j].Location.Y);
                    Thread.Sleep(10);



                }

   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-11-09 01:05:38

您可以使用 Control.Invoke() 发布到原始线程。

在 dotnet 中,您可以使用一些选项将计算结果从工作线程传播到 UI。一种涉及返回结果,另一种将结果发布到 UI 线程。

You could use Control.Invoke() to post to the original thread.

You have a few options in dotnet to propagate computation results from worker threads to the UI. One would involve returning a result, and another would be posting the result to UI thread.

宛菡 2024-11-09 01:05:38
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

namespace threade
{
    //Delegate function for work with Invoke function
    public delegate void swaps(int i, int j, Button[] btns);

    public partial class Threads : Form
    {
        Thread t1;
        Thread t2;
        Thread t3;

        //num= num of buttons
        public const int num = 30;

        Button[] btns1 =new Button[num];
        Button[] btns2 =new Button[num];
        Button[] btns3 =new Button[num];

        //num array for bubble sort & quick sort
        public int[] nums = new int[num];
        //mnum array for merge sort
        public int[] mnums = new int[num];

        public Threads()
        {
            InitializeComponent();
            //Create buttons function:
            Create_buttons();

            ////thread starts
        }

        public void Create_buttons() {
            int space = 10;
            for (int i = 0; i < num; i++) {
                mnums[i]=nums[i] = (new Random(DateTime.Now.Millisecond + i)).Next(20, 150);
                btns1[i] = new Button();
                btns1[i].Top = bubpanel.Height - nums[i] - 10;
                btns1[i].Left = space;
                btns1[i].Size = new Size(35, nums[i]);
                btns1[i].Text = btns1[i].Size.Height.ToString();
                btns1[i].BackColor = Color.Gold;
                bubpanel.Controls.Add(btns1[i]);

                btns2[i] = new Button();
                btns2[i].Top = quickpanel.Height - nums[i] - 10;
                btns2[i].Left = space;
                btns2[i].Size = new Size(35, nums[i]);
                btns2[i].Text = btns1[i].Size.Height.ToString();
                btns2[i].BackColor = Color.LightBlue;
                quickpanel.Controls.Add(btns2[i]);

                btns3[i] = new Button();
                btns3[i].Top = mergepanel.Height - mnums[i] - 10;
                btns3[i].Left = space;
                btns3[i].Size = new Size(35, mnums[i]);
                btns3[i].Text = btns1[i].Size.Height.ToString();
                btns3[i].BackColor = Color.Pink;
                mergepanel.Controls.Add(btns3[i]);
                space += 46;
                t1 = new Thread(new ThreadStart(sort1));
                t2 = new Thread(new ThreadStart(sort2));
                t3 = new Thread(new ThreadStart(sort3));
            }
        }

        void sort1()
        {
            bsort(btns1, num);
        }
        void sort2()
        {
            qsort(0, num-1, btns2);
        }
        void sort3()
        {
            mgsort(0, num);
        }

        //swap for b-sort & qsorts  
        void swap(int i, int j, Button[] btns)
        {
            Button aa = new Button();
            aa = btns[i];
            btns[i] = btns[j];
            btns[j] = aa;

            int t = btns[i].Location.X;
            btns[i].Location = new Point(btns[j].Location.X, btns[i].Location.Y);
            btns[j].Location = new Point(t, btns[j].Location.Y);
        }

        //partition
        int part(int l, int h, Button[] btns)
        {
            int i, j=l;
            int p = btns[l].Size.Height;
            for (i = l +1; i <= h; i++)
                if (btns[i].Size.Height < p)
                {
                    j++;
                    //swap button[i] and button[j]
                    Invoke(new swaps(swap), i, j, btns);
                    Thread.Sleep(100);
                }
            int s = j;
            Invoke(new swaps(swap), l, j, btns);
            Thread.Sleep(100);
            return s;
        }

        //qsort
        void qsort(int l, int h, Button[] btns)
        {
            if (h > l)
            {
                int p = part(l, h, btns);
                qsort(l, p-1,btns);
                qsort(p+1, h,btns);
            }
        }

        //bubble sort
        void bsort(Button[] btns,int n)
        {
            for (int i = 0; i < n-1; i++)                             
                for(int j=i+1;j<n;j++)
                    if (btns[j].Size.Height < btns[i].Size.Height)
                    {
                        //swap button[i] and button[j]
                        Invoke(new swaps(swap), i, j, btns);
                        Thread.Sleep(100);
                    }
        }

        //mgsort        
        void merge(int l, int m, int h)
        {
            int i = l, j = m + 1, k = l;
            int[] u = new int[num];

            while (i <= m && j <= h)
            {
                if (j == num) break;
                else if (mnums[i] < mnums[j])
                {
                    u[k++] = mnums[i++];
                }
                else
                {
                    u[k++] = mnums[j++];
                }
            }
            if (i <= m)
            {
                for (int q = k; q <= h; q++)
                    if (i < num && q < num)
                    {
                        u[q] = mnums[i++];
                    }
            }
            else
                for (int w = k; w <= h; w++)
                    if (j < num && w < num)
                    {
                        u[w] = mnums[j++];
                    }
            for (int r = l; r <= h; r++)
            {
                if (l < num && r < num)
                {
                    mnums[r] = u[l++];

                    //swap button[r] and button[mnums[r]]
                    Invoke(new swaps(swap),r, search(mnums[r]), btns3);
                    Thread.Sleep(100);
                }
            }
        }

        //for swaping in merge sort needed
        int search(int x)
        {
            for (int j = 0; j < num; j++)
                if (btns3[j].Size.Height == x)
                    return j;
            return 0;
        }

        //merge sort
        void mgsort(int l, int h)
        {
            if (l >= h) return;
                int m = (l + h) / 2;
                mgsort(l, m);
                mgsort(m+1, h);
                merge(l, m, h); 
        }

        private void runbtn_Click(object sender, EventArgs e)
        {
            t1.Start();
            t2.Start();
            t3.Start();
            runbtn.Visible = false;
            pausebtn.Visible = true;
        }

        private void pausebtn_Click(object sender, EventArgs e)
        {
            if (t1.ThreadState.ToString() != "Stopped")
                t1.Suspend();
            if (t2.ThreadState.ToString() != "Stopped")
                t2.Suspend();
            if (t3.ThreadState.ToString() != "Stopped")
                t3.Suspend();
            pausebtn.Visible = false;
            resumebtn.Visible = true;
        }

        private void resumebtn_Click(object sender, EventArgs e)
        {
            if (t1.ThreadState.ToString() != "Stopped")
                    t1.Resume();
            if (t2.ThreadState.ToString() != "Stopped")
                    t2.Resume();
            if (t3.ThreadState.ToString() != "Stopped")
                    t3.Resume();
            resumebtn.Visible = false;
            pausebtn.Visible = true;
        }

        private void stopbtn_Click(object sender, EventArgs e)
        {
            if (t1.ThreadState.ToString() != "Suspended")
                t1.Abort();
            if (t2.ThreadState.ToString() != "Suspended")
                t2.Abort();
            if (t3.ThreadState.ToString() != "Suspended")
                t3.Abort();
            stopbtn.Visible = true;

            for (int i = 0; i < num; i++)
            {
                bubpanel.Controls.Remove(btns1[i]);
                mergepanel.Controls.Remove(btns3[i]);
                quickpanel.Controls.Remove(btns2[i]);
            }
            Create_buttons();
            runbtn.Visible = true;
            pausebtn.Visible = false;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;

namespace threade
{
    //Delegate function for work with Invoke function
    public delegate void swaps(int i, int j, Button[] btns);

    public partial class Threads : Form
    {
        Thread t1;
        Thread t2;
        Thread t3;

        //num= num of buttons
        public const int num = 30;

        Button[] btns1 =new Button[num];
        Button[] btns2 =new Button[num];
        Button[] btns3 =new Button[num];

        //num array for bubble sort & quick sort
        public int[] nums = new int[num];
        //mnum array for merge sort
        public int[] mnums = new int[num];

        public Threads()
        {
            InitializeComponent();
            //Create buttons function:
            Create_buttons();

            ////thread starts
        }

        public void Create_buttons() {
            int space = 10;
            for (int i = 0; i < num; i++) {
                mnums[i]=nums[i] = (new Random(DateTime.Now.Millisecond + i)).Next(20, 150);
                btns1[i] = new Button();
                btns1[i].Top = bubpanel.Height - nums[i] - 10;
                btns1[i].Left = space;
                btns1[i].Size = new Size(35, nums[i]);
                btns1[i].Text = btns1[i].Size.Height.ToString();
                btns1[i].BackColor = Color.Gold;
                bubpanel.Controls.Add(btns1[i]);

                btns2[i] = new Button();
                btns2[i].Top = quickpanel.Height - nums[i] - 10;
                btns2[i].Left = space;
                btns2[i].Size = new Size(35, nums[i]);
                btns2[i].Text = btns1[i].Size.Height.ToString();
                btns2[i].BackColor = Color.LightBlue;
                quickpanel.Controls.Add(btns2[i]);

                btns3[i] = new Button();
                btns3[i].Top = mergepanel.Height - mnums[i] - 10;
                btns3[i].Left = space;
                btns3[i].Size = new Size(35, mnums[i]);
                btns3[i].Text = btns1[i].Size.Height.ToString();
                btns3[i].BackColor = Color.Pink;
                mergepanel.Controls.Add(btns3[i]);
                space += 46;
                t1 = new Thread(new ThreadStart(sort1));
                t2 = new Thread(new ThreadStart(sort2));
                t3 = new Thread(new ThreadStart(sort3));
            }
        }

        void sort1()
        {
            bsort(btns1, num);
        }
        void sort2()
        {
            qsort(0, num-1, btns2);
        }
        void sort3()
        {
            mgsort(0, num);
        }

        //swap for b-sort & qsorts  
        void swap(int i, int j, Button[] btns)
        {
            Button aa = new Button();
            aa = btns[i];
            btns[i] = btns[j];
            btns[j] = aa;

            int t = btns[i].Location.X;
            btns[i].Location = new Point(btns[j].Location.X, btns[i].Location.Y);
            btns[j].Location = new Point(t, btns[j].Location.Y);
        }

        //partition
        int part(int l, int h, Button[] btns)
        {
            int i, j=l;
            int p = btns[l].Size.Height;
            for (i = l +1; i <= h; i++)
                if (btns[i].Size.Height < p)
                {
                    j++;
                    //swap button[i] and button[j]
                    Invoke(new swaps(swap), i, j, btns);
                    Thread.Sleep(100);
                }
            int s = j;
            Invoke(new swaps(swap), l, j, btns);
            Thread.Sleep(100);
            return s;
        }

        //qsort
        void qsort(int l, int h, Button[] btns)
        {
            if (h > l)
            {
                int p = part(l, h, btns);
                qsort(l, p-1,btns);
                qsort(p+1, h,btns);
            }
        }

        //bubble sort
        void bsort(Button[] btns,int n)
        {
            for (int i = 0; i < n-1; i++)                             
                for(int j=i+1;j<n;j++)
                    if (btns[j].Size.Height < btns[i].Size.Height)
                    {
                        //swap button[i] and button[j]
                        Invoke(new swaps(swap), i, j, btns);
                        Thread.Sleep(100);
                    }
        }

        //mgsort        
        void merge(int l, int m, int h)
        {
            int i = l, j = m + 1, k = l;
            int[] u = new int[num];

            while (i <= m && j <= h)
            {
                if (j == num) break;
                else if (mnums[i] < mnums[j])
                {
                    u[k++] = mnums[i++];
                }
                else
                {
                    u[k++] = mnums[j++];
                }
            }
            if (i <= m)
            {
                for (int q = k; q <= h; q++)
                    if (i < num && q < num)
                    {
                        u[q] = mnums[i++];
                    }
            }
            else
                for (int w = k; w <= h; w++)
                    if (j < num && w < num)
                    {
                        u[w] = mnums[j++];
                    }
            for (int r = l; r <= h; r++)
            {
                if (l < num && r < num)
                {
                    mnums[r] = u[l++];

                    //swap button[r] and button[mnums[r]]
                    Invoke(new swaps(swap),r, search(mnums[r]), btns3);
                    Thread.Sleep(100);
                }
            }
        }

        //for swaping in merge sort needed
        int search(int x)
        {
            for (int j = 0; j < num; j++)
                if (btns3[j].Size.Height == x)
                    return j;
            return 0;
        }

        //merge sort
        void mgsort(int l, int h)
        {
            if (l >= h) return;
                int m = (l + h) / 2;
                mgsort(l, m);
                mgsort(m+1, h);
                merge(l, m, h); 
        }

        private void runbtn_Click(object sender, EventArgs e)
        {
            t1.Start();
            t2.Start();
            t3.Start();
            runbtn.Visible = false;
            pausebtn.Visible = true;
        }

        private void pausebtn_Click(object sender, EventArgs e)
        {
            if (t1.ThreadState.ToString() != "Stopped")
                t1.Suspend();
            if (t2.ThreadState.ToString() != "Stopped")
                t2.Suspend();
            if (t3.ThreadState.ToString() != "Stopped")
                t3.Suspend();
            pausebtn.Visible = false;
            resumebtn.Visible = true;
        }

        private void resumebtn_Click(object sender, EventArgs e)
        {
            if (t1.ThreadState.ToString() != "Stopped")
                    t1.Resume();
            if (t2.ThreadState.ToString() != "Stopped")
                    t2.Resume();
            if (t3.ThreadState.ToString() != "Stopped")
                    t3.Resume();
            resumebtn.Visible = false;
            pausebtn.Visible = true;
        }

        private void stopbtn_Click(object sender, EventArgs e)
        {
            if (t1.ThreadState.ToString() != "Suspended")
                t1.Abort();
            if (t2.ThreadState.ToString() != "Suspended")
                t2.Abort();
            if (t3.ThreadState.ToString() != "Suspended")
                t3.Abort();
            stopbtn.Visible = true;

            for (int i = 0; i < num; i++)
            {
                bubpanel.Controls.Remove(btns1[i]);
                mergepanel.Controls.Remove(btns3[i]);
                quickpanel.Controls.Remove(btns2[i]);
            }
            Create_buttons();
            runbtn.Visible = true;
            pausebtn.Visible = false;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文