zoukankan
html css js c++ java
异步方式调用同步方法
一段异步方式调用同步方法代码
执行顺序 执行主程序>>执行异步调用>>执行
WaitOne()后代码>>执行回调>>执行主程序
using
System.Text;
using
System.Data;
using
System.Data.SqlClient;
using
System.Collections;
using
System.Threading;
using
System.IO;
using
System.Runtime.InteropServices;
using
System;
using
System.Runtime.Remoting.Messaging;
namespace
Program
{
public
class
Programmers
{
public
static
void
Main(
string
[] args)
{
Programmers pr
=
new
Programmers();
Console.WriteLine(
"
开始执行
..
"
);
pr.smork();
Thread.Sleep(
1000
);
//
注释掉结果有点不同
Console.WriteLine(
"
我是主线程
"
);
Console.ReadLine();
}
delegate
void
mydelegate();
private
void
smork()
{
/**/
///
异步方式调用同步方法1
AsyncCallback mycallback
=
new
AsyncCallback(tellyou);
//
(回调函数)
mydelegate mdg
=
new
mydelegate(move);
IAsyncResult result
=
mdg.BeginInvoke(mycallback,
null
);
result.AsyncWaitHandle.WaitOne();
//
等待异步完成
Console.WriteLine(
"
异步调用后
"
);
MyRegion
#region
MyRegion
//
异步方式调用同步方法2
//
AsyncCallback mycallback = new AsyncCallback(tellyou);
//
mydelegate mdg = new mydelegate(move);
//
IAsyncResult result = mdg.BeginInvoke(mycallback, null);
//
while (!result.IsCompleted)
//
{
//
//
Thread.Sleep(50);
//
不要与要有区别
//
}
/**/
///
/ 异步方式调用同步方法3(没有回调函数)
//
mydelegate mdg = new mydelegate(move);
//
IAsyncResult result = mdg.BeginInvoke(null, null);
//
while (!result.IsCompleted)
//
{
//
//
Thread.Sleep(60);
//
}
//
mdg.EndInvoke(result);
//
停止调用
//
异步方式调用同步方法4(没有回调函数)
//
mydelegate mdg = new mydelegate(move);
//
IAsyncResult result = mdg.BeginInvoke(null, null);
//
result.AsyncWaitHandle.WaitOne();
//
mdg.EndInvoke(result);
//
停止调用
#endregion
}
public
void
move()
{
for
(
int
i
=
0
; i
<
5
; i
++
)
{
Console.WriteLine(
"
异步任务执行
"
);
Thread.Sleep(
1000
);
}
}
public
void
tellyou(IAsyncResult result)
//
回调函数
{
//
Console.WriteLine("异步任务完成");
//
当异步完成后输出结果 通知已经异步完了
AsyncResult asyncResult
=
result
as
AsyncResult;
mydelegate mydelegate
=
asyncResult.AsyncDelegate
as
mydelegate;
mydelegate.EndInvoke(result);
if
(result.IsCompleted)
{
Console.WriteLine(
"
异步任务完成
"
);
}
}
}
}
查看全文
相关阅读:
MSDN仿站
跟我学android—02.CustomActivity
iptables redirect outside requests to 127.0.0.1
linux 查看端口使用情况
防火墙、Iptables、netfilter/iptables、NAT 概述
POSTROUTING与PREROUTING区别
android:layout_gravity和android:gravity的区别
EasyUI datagrid 分页Json字符串格式
[转载]easyui datagrid 时间格化(JS 日期时间本地化显示)
[转载]EasyUI Pagination 分页的两种做法
原文地址:https://www.cnblogs.com/xiaobaigang/p/927920.html
最新文章
APP安全--网络传输安全 AES/RSA/ECC/MD5/SHA
iOS性能优化:Instruments使用实战
iOS中self与_的区别
KVO等具体实现步骤以及注意事项
面试题汇总--数据储存/应用程序/UI控件/客户端的安全性与框架处理。。。
面试题--客户端的安全性与框架处理
iOSXML & JSON 简介
Interview-Increasing Sequence with Length 3.
Leetcode- Single Number II
Leetcode-Single Number
热门文章
Leetcode-Gas Station
Leetcode-Sudoku Solver
Leetcode-Fraction to Recurring Decimal
Leetcode-Largest Rectangle in Histogram
Leetcode-Divide Two Integers
Leetcode-Max Points on a Line
Leetcode-Median of Two Sorted Arrays
Activity栈简析
Android:Layout_weight的深刻理解
android:layout_weight的真实含义
Copyright © 2011-2022 走看看