显示运行时的形式变化使用线程
我想为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 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.