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();
}
}
}
查看全文
相关阅读:
【 数据结构(C语言)】栈的应用——行编辑程序
【 数据结构 (C语言)】栈的应用(二)——括号匹配问题
节点
页面加载--延迟加载
雅黑php 探针
Swiper 触屏滑动切换
tab 选择悬停展示
翻牌抽奖功能讲解
公告信息滚动功能
织梦提交表单不进行跳转
原文地址:https://www.cnblogs.com/engine1984/p/862970.html
最新文章
【新项目】ATM机+商场系统逻辑
模块之random、shutil、shevle、logging
关于Unix时间戳(Unix timestamp)的种种
[MySQL]mysqlnd cannot connect to MySQL 4.1+ using the old insecure【解决方法】
linux下mysql的备份和还原
yum报错Error: Cannot find a valid baseurl for repo: base解决方法
如何设置mysql远程访问
centos防火墙开放某个端口
一些常用的端口
win7防火墙设置开放某个端口
热门文章
一些高端的词汇
推荐一个ISO、安卓神软件——北京实时公交
减法是加法的逆运算,除法是乘法的逆运算
2017年11月小结
扇贝新闻
【 C语言指针详解 】(七)野指针
【数学】素数筛和线性筛
【数学】欧几里得(辗转相除)的证明
底层优化(一) sum += fuc(x)
【 数据结构(C语言)】线性表——链表的应用(一)
Copyright © 2011-2022 走看看