(二)线程--通过委托异步调用方法
2009-08-03 13:46
816 查看
(一).描述 先运行个简单的线程示例,认识一下线程 通过委托调用方法,以及使用AsyncResult判断线程的状态 (二).代码 using System; using System.Threading; using System.Runtime.Remoting.Messaging; namespace 通过委托异步调用方法 { //委托声明(函数签名) delegate
(一).描述
先运行个简单的线程示例,认识一下线程
通过委托调用方法,以及使用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();
}
}
}
(一).描述
先运行个简单的线程示例,认识一下线程
通过委托调用方法,以及使用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#多线程-不同线程之间通过事件委托封送调用方法(转)
- C#多线程-不同线程之间通过事件委托封送调用方法
- C#多线程-不同线程之间通过事件委托封送调用方法
- C#多线程-不同线程之间通过事件委托封送调用方法
- C#构建多线程应用程序(1) —— 使用委托来自动创建次线程以实现异步方法的调用
- 通过委托异步调用方法
- !!c# 委托与多线程 -- 相比background来说,本方法针对当需要通过异步代理发起UI操作,而后线程中进行sleep操作的时使用
- C#通过委托和异步调用实现窗体间的通讯
- 在Python中通过threading模块定义和调用线程的方法
- .net知识和学习方法系列(十)委托异步调用方法b
- C#源代码—声明委托,通过委托对象调用所指向的方法
- C#多线程实现方法——异步委托/调用(一)
- VC++ .NET UI同步(使用Invoke托管方法),异步线程调用UI
- 界面-工作者线程结构之:异步委托调用
- 委托和方法的异步调用
- C#多线程实现方法——异步委托/调用