C#多线程刷新多个winform控件

chenjiecao123 2014-06-11 03:14:18
各位大神,小弟最近刚学习C#,刚学习多线程刷新winform,//注释代码是没有问题的,但是我想用红色的那部分代码实现,搞了半天也出不来,希望大鸟们解释下,帮我看看哪里出错了,谢过~~~

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.Threading;

namespace random
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread th;
Random rd;
System.Windows.Forms.Timer time;
public delegate void setdelegate();
setdelegate setd;
bool judgeThread;
private void button1_Click(object sender, EventArgs e)
{
// time.Start();

th = new Thread(Run);
th.Start();
}

private void Form1_Load(object sender, EventArgs e)
{

rd = new Random();
judgeThread = false;
}

void time_Tick(object sender, EventArgs e)
{

for (int i = 0; i < this.Controls.Count; i++)
{
if (this.Controls[i] is TextBox)
{
this.Controls[i].Text = rd.Next(20).ToString();
}
}
}

private void button2_Click(object sender, EventArgs e)
{

if (th.IsAlive)
{
th.Abort();
}
}

public void Run()
{
while (true)
{
GenerateRandom();
Thread.Sleep(100);
}
}

//public void GenerateRandom()
//{


// if (this.textBox1.InvokeRequired || this.textBox2.InvokeRequired || this.textBox3.InvokeRequired || this.textBox4.InvokeRequired || this.textBox5.InvokeRequired)
// {
// setd = GenerateRandom;
// this.textBox1.Invoke(setd);
// this.textBox2.Invoke(setd);
// this.textBox3.Invoke(setd);
// this.textBox4.Invoke(setd);
// this.textBox5.Invoke(setd);
// }
// else
// {
// this.textBox1.Text = rd.Next(20).ToString();
// this.textBox2.Text = rd.Next(20).ToString();
// this.textBox3.Text = rd.Next(20).ToString();
// this.textBox4.Text = rd.Next(20).ToString();
// this.textBox5.Text = rd.Next(20).ToString();
// }

//}

public void GenerateRandom()
{
if (!judgeThread)
{
for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
{
if (i == this.groupBox1.Controls.Owner.Controls.Count-1)
{
judgeThread = true;
}
if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
{
if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired)
{
setd = GenerateRandom;
this.groupBox1.Controls.Owner.Controls[i].Invoke(setd);
}
else
continue;
}
}
}
else
{
for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
{
if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
{
this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString();
}
}
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (th.IsAlive)
{
th.Abort();
}
}
}
}
...全文
512 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
changjiangzhibin 2014-06-11
  • 打赏
  • 举报
回复
一般是重开一个Threa做操作 也可以使用易用的 如 BackgroundWorker
chenjiecao123 2014-06-11
  • 打赏
  • 举报
回复
引用 4 楼 sunny906 的回复:

        public void GenerateRandom()
        {
            if (!judgeThread)
            {
                for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
                {
                    if (i == this.groupBox1.Controls.Owner.Controls.Count - 1)
                    {
                        judgeThread = true;
                    }
                    if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
                    {
                        if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired)
                        {
                            setd = GenerateRandom;
                            this.groupBox1.Controls.Owner.Controls[i].Invoke(setd);
                        }
                        else
                            continue;
                    }
                }
            }
            else
            {
                for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
                {
                    if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
                    {
                       // this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString();
                       //改成下面的写法
                        this.groupBox1.Controls.Owner.Controls[i].Invoke(new MethodInvoker(() => 
                        {
                            this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString(); 
                        }));
                    }
                }
            }
        }
你好,大鸟,你的方法确实有用,有个问题,如此写的话 if (!judgeThread) { for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++) { if (i == this.groupBox1.Controls.Owner.Controls.Count - 1) { judgeThread = true; } if (this.groupBox1.Controls.Owner.Controls[i] is TextBox) { if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired) { setd = GenerateRandom; this.groupBox1.Controls.Owner.Controls[i].Invoke(setd); } else continue; } } } 这段代码都没用了?(这段代码的本意就是让这些控件invoke,而您给我的答案又invoke了) 我想知道我的代码问题出在哪
sunny906 2014-06-11
  • 打赏
  • 举报
回复

        public void GenerateRandom()
        {
            if (!judgeThread)
            {
                for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
                {
                    if (i == this.groupBox1.Controls.Owner.Controls.Count - 1)
                    {
                        judgeThread = true;
                    }
                    if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
                    {
                        if (this.groupBox1.Controls.Owner.Controls[i].InvokeRequired)
                        {
                            setd = GenerateRandom;
                            this.groupBox1.Controls.Owner.Controls[i].Invoke(setd);
                        }
                        else
                            continue;
                    }
                }
            }
            else
            {
                for (int i = 0; i < this.groupBox1.Controls.Owner.Controls.Count; i++)
                {
                    if (this.groupBox1.Controls.Owner.Controls[i] is TextBox)
                    {
                       // this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString();
                       //改成下面的写法
                        this.groupBox1.Controls.Owner.Controls[i].Invoke(new MethodInvoker(() => 
                        {
                            this.groupBox1.Controls.Owner.Controls[i].Text = rd.Next(20).ToString(); 
                        }));
                    }
                }
            }
        }
於黾 2014-06-11
  • 打赏
  • 举报
回复
另外,textbox[]=new textbox[]{textbox1,textbox2,textbox3}; 所有对象都可以放数组里的
於黾 2014-06-11
  • 打赏
  • 举报
回复
这也不是多线程啊,你只定义了一个线程. 要么用个线程数组,循环为每个控件开一个线程 要么用线程池
exception92 2014-06-11
  • 打赏
  • 举报
回复
你把 textBox 放到一个list集合,再进行便利。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧