C#多线程、委托使用
来源:互联网 发布:防范电信网络诈骗征文 编辑:程序博客网 时间:2024/05/16 17:02
int test1 = 0;
int test2 = 0;
public void haveP(object i)
{
int num = (int)i;
for (int j = 0; j < 100; j++)
{
num++;
Thread.Sleep(100);
}
x.BeginInvoke(new Action(() =>
{
number.Text = num.ToString();
}), DispatcherPriority.Normal);
}
public void norP()
{
for (int j = 0; j < 100; j++)
{
test2++;
Thread.Sleep(100);
}
x.BeginInvoke(new Action(() =>
{
number2.Text = test2.ToString();
}), DispatcherPriority.Normal);
}
Dispatcher x;
private void star_Click(object sender, RoutedEventArgs e)
{
x = this.Dispatcher;
if (number.Text.Trim()!="")
{
test1 = (int)Convert.ToDouble(number.Text);
test2 = (int)Convert.ToDouble(number2.Text);
}
//带参数多线程使用
//带参数的多线程使用时,方法必须只有一个参数,并且是object类型
ParameterizedThreadStart ParStart = new ParameterizedThreadStart(haveP);
Thread myThread = new Thread(ParStart);
myThread.Start(test1);
//不带参数多线程使用
Thread newThread = new Thread(norP);
newThread.Start();
}
//定义委托
delegate void DelegateTest(ref int j);
public void norP(ref int i)
{
for (int j = 0; j < 100; j++)
{
i++;
Thread.Sleep(100);
}
x.BeginInvoke(new Action(() =>
{
number.Text = test1.ToString();
number2.Text = test1.ToString();
}), DispatcherPriority.Normal);
}
private void end_Click(object sender, RoutedEventArgs e)
{
x = this.Dispatcher;
//创建委托实例
DelegateTest testdel = new DelegateTest(norP);
if (number.Text.Trim()!="")
{
test1 = (int)Convert.ToDouble(number.Text);
}
//执行委托
testdel(ref test1);
}//定义操作次线程的委托
Threadtest.rar
- C#多线程、委托使用
- C#多线程中使用代理(委托)
- C#中多线程 委托的使用
- C#多线程中使用代理(委托)
- c# 委托与多线程
- c#多线程与委托
- C#委托和多线程
- c# 委托与多线程
- C#委托和多线程
- c# 委托与多线程
- C#委托 多线程学习
- C#多线程之使用委托进行异步编程
- C#使用多线程并发之异步委托IAsyncResult
- C#委托-委托的使用
- 多线程、委托的使用
- c# delegate(委托)与多线程
- C# Delegate(委托)与多线程
- C# Delegate(委托)与多线程
- Windows 下串口通信(下)
- C与C++归并排序,用时对比
- CSP开发——基本函数详解2
- 正则表达式-用户名 数字,中文,英文,下划线
- 主动撞错感受
- C#多线程、委托使用
- IOS设计模式之四:观察者模式
- IOS设计模式之三:MVC模式
- 关键字过虑实现的思路及Aho–Corasick高效字符串匹配算法应用
- ibatis 常用语句 带参数的
- Eclipse下Tomcat插件的安装
- 贷款计算
- Linux静态库和动态库
- ORACLE,SQLSERVER,DB2 删除重复数据SQL