zoukankan
html css js c++ java
(二)线程通过委托异步调用方法
(一).描述
先运行个简单的线程示例,认识一下线程
通过委托调用方法,以及使用AsyncResult判断线程的状态
(二).代码
using
System;
using
System.Threading;
using
System.Runtime.Remoting.Messaging;
namespace
通过委托异步调用方法
{
//
委托声明(函数签名)
delegate
string
MyMethodDelegate();
class
MyClass
{
//
要调用的动态方法
public
string
MyMethod1()
{
return
"
Hello Word1
"
;
}
//
要调用的静态方法
public
static
string
MyMethod2()
{
return
"
Hello Word2
"
;
}
}
class
Class1
{
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main(
string
[] args)
{
MyClass myClass
=
new
MyClass();
//
方式1: 声明委托,调用MyMethod1
MyMethodDelegate d
=
new
MyMethodDelegate(myClass.MyMethod1);
string
strEnd
=
d();
Console.WriteLine(strEnd);
//
方式2: 声明委托,调用MyMethod2 (使用AsyncResult对象调用)
d
=
new
MyMethodDelegate(MyClass.MyMethod2);
//
定义一个委托可以供多个方法使用
AsyncResult myResult;
//
此类封闭异步委托异步调用的结果,通过AsyncResult得到结果.
myResult
=
(AsyncResult)d.BeginInvoke(
null
,
null
);
//
开始调用
while
(
!
myResult.IsCompleted)
//
判断线程是否执行完成
{
Console.WriteLine(
"
正在异步执行MyMethod2
..
"
);
}
Console.WriteLine(
"
方法MyMethod2执行完成!
"
);
strEnd
=
d.EndInvoke(myResult);
//
等待委托调用的方法完成,并返回结果
Console.WriteLine(strEnd);
Console.Read();
}
}
}
查看全文
相关阅读:
物理学——总结
创建场景和赛道——挑战:为赛道建立一个新的单元测试
物理学——牛顿运动定律
物理学——挑战:实现道路碰撞检测
1291. Gearwheels 夜
hdu 4442 Physical Examination 夜
hdu 4450 Draw Something 夜
1129. Door Painting 夜
hdu 4431 Mahjong 夜
1128. Partition into Groups 夜
原文地址:https://www.cnblogs.com/engine1984/p/862970.html
最新文章
repeater实现多列显示
母版页里面查找Repeater内控件,并构造URL
CSS Repeater 交错显示行背景色 table行鼠标进入事件特效 禁止文本换行
Repeater如何嵌套绑定数据?
You are using safe update mode and you tried to update a table without a WHERE that uses a K
tomcat 用户名和密码的修改
webstorm 4.0 注册码
Word2010中插入分页符的两种方法
屏幕录制
Word2010页眉页脚设置教程
热门文章
SQL 2008升级SQL 2008 R2完全教程或者10.00.1600升级10.50.1600
Camtasia Studio 6.0
mysql 将一个数据表中的数据插入到另一个表中
mysql 数据库的备份和还原
物理学——实现物理效果
物理学——车辆碰撞检测
物理学——物理引擎
解决办法:sql server 2005 15023错误
创建场景和赛道——总结
物理学——概览
Copyright © 2011-2022 走看看