以前没有接触过多线程,想要个例子研究一下;哪位给点示代码提示;
winform中界面:一个datagridview控件,控件id为默认,用来记录所有线程的状态;一个开始按钮,一个停止按钮,一个自动执行按钮
功能要求:
点击开始按钮,多线程执行一次,停止按钮停止所有正在执行的线程。自动执行按钮让所有线程自动执行;
下面是我写的代码,有些不对劲,gridview的数据没有实时更新。望高手指点;using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ThreadControlView
{
public delegate void upview();//线程委托
public partial class Form2 : Form
{
private int threadNum = 10;//线程总数
private Thread[] thread = null;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 创建一个datatable,用来记录线程状态
/// </summary>
private void bd_view()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id");
dt.Columns.Add(dc);
dc = new DataColumn("stats");
dt.Columns.Add(dc);
dc = new DataColumn("msg");
dt.Columns.Add(dc);
for (int i = 0; i < threadNum; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = (i + 1).ToString();
dr["stats"] = "停止";
dr["msg"] = "Thread" + (i + 1).ToString();
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
} private void Form2_Load(object sender, EventArgs e)
{
bd_view();
} private void modi()
{
if (!dataGridView1.InvokeRequired)
{ getModi();
}
else
{
dataGridView1.Invoke(new upview(getModi));
}
}
/// <summary>
/// void,让threadstart调用
/// </summary>
private void getModi()
{
if (thread[commandVar.num] != null)
{
modifyStats(commandVar.num, thread[commandVar.num].ThreadState.ToString(), thread[commandVar.num].Name);
}
}
/// <summary>
/// 更新gridview中指定行的数据,给getModi()调用
/// </summary>
/// <param name="index"></param>
/// <param name="threadstats"></param>
/// <param name="threadname"></param>
private void modifyStats(int index,string threadstats,string threadname)
{
dataGridView1.Rows[index].Cells[1].Value = threadstats;
dataGridView1.Rows[index].Cells[2].Value = threadname;
Thread.Sleep(100);
}
/// <summary>
/// 多线程自动运行
/// </summary>
private void autoRun()
{
thread = new Thread[threadNum];
for (int i = 0; i < threadNum; i++)
{
Thread t = new Thread(new ThreadStart(modi));
thread[i] = t;
thread[i].Name = "Thread" + (i + 1).ToString();
thread[i].Start();
}
int curnum = 0;//计数器
while (commandVar.flag == 0)
{ modi();
Thread.Sleep(100);
curnum++;
if (curnum > commandVar.max)
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
commandVar.flag = 0;
upview up = new upview(getModi);
thread = new Thread[threadNum];
for (int i = 0; i < threadNum; i++)
{
Thread t = new Thread(new ThreadStart(modi));
thread[i] = t;
thread[i].Name = "Thread" + (i + 1).ToString();
thread[i].Start();
modifyStats(i, thread[i].ThreadState.ToString(), thread[i].Name);
}
} private void button2_Click(object sender, EventArgs e)
{
commandVar.flag = 1;
} private void button3_Click(object sender, EventArgs e)
{
autoRun();
}
}
public class commandVar
{
private static int _num=0;//当前进程序号
private static int _flag = 0;//是否运行
private static int _max = 10;//最高运行次数
public static int num
{
set { _num = value; }
get { return _num; }
}
public static int flag
{
set { _flag = value; }
get { return _flag; }
}
public static int max
{
set { _max = value; }
get { return _max; }
}
}
}
winform中界面:一个datagridview控件,控件id为默认,用来记录所有线程的状态;一个开始按钮,一个停止按钮,一个自动执行按钮
功能要求:
点击开始按钮,多线程执行一次,停止按钮停止所有正在执行的线程。自动执行按钮让所有线程自动执行;
下面是我写的代码,有些不对劲,gridview的数据没有实时更新。望高手指点;using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ThreadControlView
{
public delegate void upview();//线程委托
public partial class Form2 : Form
{
private int threadNum = 10;//线程总数
private Thread[] thread = null;
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 创建一个datatable,用来记录线程状态
/// </summary>
private void bd_view()
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("id");
dt.Columns.Add(dc);
dc = new DataColumn("stats");
dt.Columns.Add(dc);
dc = new DataColumn("msg");
dt.Columns.Add(dc);
for (int i = 0; i < threadNum; i++)
{
DataRow dr = dt.NewRow();
dr["id"] = (i + 1).ToString();
dr["stats"] = "停止";
dr["msg"] = "Thread" + (i + 1).ToString();
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt;
} private void Form2_Load(object sender, EventArgs e)
{
bd_view();
} private void modi()
{
if (!dataGridView1.InvokeRequired)
{ getModi();
}
else
{
dataGridView1.Invoke(new upview(getModi));
}
}
/// <summary>
/// void,让threadstart调用
/// </summary>
private void getModi()
{
if (thread[commandVar.num] != null)
{
modifyStats(commandVar.num, thread[commandVar.num].ThreadState.ToString(), thread[commandVar.num].Name);
}
}
/// <summary>
/// 更新gridview中指定行的数据,给getModi()调用
/// </summary>
/// <param name="index"></param>
/// <param name="threadstats"></param>
/// <param name="threadname"></param>
private void modifyStats(int index,string threadstats,string threadname)
{
dataGridView1.Rows[index].Cells[1].Value = threadstats;
dataGridView1.Rows[index].Cells[2].Value = threadname;
Thread.Sleep(100);
}
/// <summary>
/// 多线程自动运行
/// </summary>
private void autoRun()
{
thread = new Thread[threadNum];
for (int i = 0; i < threadNum; i++)
{
Thread t = new Thread(new ThreadStart(modi));
thread[i] = t;
thread[i].Name = "Thread" + (i + 1).ToString();
thread[i].Start();
}
int curnum = 0;//计数器
while (commandVar.flag == 0)
{ modi();
Thread.Sleep(100);
curnum++;
if (curnum > commandVar.max)
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
commandVar.flag = 0;
upview up = new upview(getModi);
thread = new Thread[threadNum];
for (int i = 0; i < threadNum; i++)
{
Thread t = new Thread(new ThreadStart(modi));
thread[i] = t;
thread[i].Name = "Thread" + (i + 1).ToString();
thread[i].Start();
modifyStats(i, thread[i].ThreadState.ToString(), thread[i].Name);
}
} private void button2_Click(object sender, EventArgs e)
{
commandVar.flag = 1;
} private void button3_Click(object sender, EventArgs e)
{
autoRun();
}
}
public class commandVar
{
private static int _num=0;//当前进程序号
private static int _flag = 0;//是否运行
private static int _max = 10;//最高运行次数
public static int num
{
set { _num = value; }
get { return _num; }
}
public static int flag
{
set { _flag = value; }
get { return _flag; }
}
public static int max
{
set { _max = value; }
get { return _max; }
}
}
}
解决方案 »
- 一个排序问题
- Stackoverflow....C#调用C++的DLL
- 关于密码的加解密!
- 用label控件显示程序状态的问题
- 初学者,请问我这么写为什么不能模拟键盘上的 NumLock键??????????????????????
- OWC10/11都不能用,只好用OWC9.0,可是多sheet的excel文件的在线编辑和保存怎么办啊~~
- 用C#连接Sybase 的ASA7数据库时,为什么所有的中文列显示出来全部都为乱码?
- 高分求::怎么样制作一个象fontdialog这样的控件,拖过来后自动放到form下方,最好有例子。
- C#编码优化、规范化微软官方工具:FxCop的介绍、下载、使用-----完全手册
- 虽然发布了我的web service,但是,如果我不希望经过我同意的用户使用我的service功能,或者只提供几个特定的函数供其使用,要如何实现这
- tcp协议和upd协议哪个稳定
- 帮忙看看这个treeview绑定函数,就几行。
大家可以不看我的代码。
简单的说,我的要求,10个线程循环执行十轮,执行过程中,用datagridview实时来显示这十个线程的状态和名称;
我的功能要求是:10个线程循环执行十轮,执行过程中,用datagridview实时来显示这十个线程的状态和名称
只要是c#代码就可以,大家想怎么写就怎么写。不要看我的代码。