多线程传数据例子
2009-03-04 09:41
295 查看
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;
using System;
using System.Threading;
namespace DataImportFromAccess
{
//声明一个回调函数:注意传递的参数要与Example类中的函数参数类型一致
public delegate void ExampleCallback(int lineCount, Label lb);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void CurrentNumber(int tempCurrent,Label lb)
{
lb.Text = tempCurrent.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
ThreadWithData twd = new ThreadWithData(1, 100,this.label1,new ExampleCallback(CurrentNumber));
Thread td = new Thread(new ThreadStart(twd.RunMethod));
td.Start();
}
private void button2_Click(object sender, EventArgs e)
{
ThreadWithData twd = new ThreadWithData(2, 200,this.label2, new ExampleCallback(CurrentNumber));
Thread td = new Thread(new ThreadStart(twd.RunMethod));
td.Start();
}
}
public class ThreadWithData
{
private int start = 0;
private int end = 0;
private ExampleCallback callBack;
private Label lb;
public ThreadWithData(int start,int end,Label lb,ExampleCallback callBack)
{
this.start = start;
this.end = end;
this.callBack=callBack;
this.lb = lb;
}
public void RunMethod()
{
for(int i=start;i<end;i++)
{
Thread.Sleep(1000);
if (callBack != null)
callBack(i,lb);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System;
using System.Threading;
namespace DataImportFromAccess
{
//声明一个回调函数:注意传递的参数要与Example类中的函数参数类型一致
public delegate void ExampleCallback(int lineCount, Label lb);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void CurrentNumber(int tempCurrent,Label lb)
{
lb.Text = tempCurrent.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
ThreadWithData twd = new ThreadWithData(1, 100,this.label1,new ExampleCallback(CurrentNumber));
Thread td = new Thread(new ThreadStart(twd.RunMethod));
td.Start();
}
private void button2_Click(object sender, EventArgs e)
{
ThreadWithData twd = new ThreadWithData(2, 200,this.label2, new ExampleCallback(CurrentNumber));
Thread td = new Thread(new ThreadStart(twd.RunMethod));
td.Start();
}
}
public class ThreadWithData
{
private int start = 0;
private int end = 0;
private ExampleCallback callBack;
private Label lb;
public ThreadWithData(int start,int end,Label lb,ExampleCallback callBack)
{
this.start = start;
this.end = end;
this.callBack=callBack;
this.lb = lb;
}
public void RunMethod()
{
for(int i=start;i<end;i++)
{
Thread.Sleep(1000);
if (callBack != null)
callBack(i,lb);
}
}
}
}
相关文章推荐
- 关于多线程、安装信号、长跳转、数据结构等的例子
- 多线程实现大批量数据导入(例子)
- 多线程例子,传数据|软件开发|转自博客园
- 多线程加载数据显示进度窗体的例子
- 多线程例子,传数据|软件开发|转自博客园
- 多线程传数据例子
- Flex中如何通过dataTipOffset样式设置HSlider控件的数据开销的例子
- 多线程例子
- 网络与多线程的设计例子
- sqlalchemy 多线程的数据存储
- 多线程内数据长效保护的问题二
- [SQL]一个删选数据的例子,使用GROUP、DISTINCT
- 多线程之线程范围内的数据共享ThreadLocal
- c# BackGroundWorker 多线程操作的小例子
- openMP-数据并行化小例子
- Python多线程获取上证50成分股交易数据
- 证明Servlet是单实例多线程的小例子
- c#实现多线程代码例子
- asp.net socket多线程 简单监听端口,获得数据
- PHP使用CURL_MULTI实现多线程采集的例子