Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474345
  • 博文数量: 112
  • 博客积分: 5696
  • 博客等级: 大校
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-17 09:58
文章分类

全部博文(112)

文章存档

2011年(22)

2010年(28)

2009年(21)

2008年(41)

分类:

2010-06-05 11:10:22

 
public delegate void SendOrPostCallback(
	Object state
)

参数

state
类型:object
传递给委托的对象。

表示在消息即将被调度到同步上下文时要调用的方法。

SendOrPostCallback 表示在消息即将被调度到同步上下文时要执行的回调方法。创建委托,方法是将回调方法传递给SendOrPostCallback 构造函数。您的方法必须具有此处所显示的签名。

要做一件 void 类型返回值的,说白了,就是无返回值的,参数是 object的,也就是说,想传啥,就传啥,无法无天了,尺度很大! 网上搜索了一下,有例子,可以很好的证明,这个应用的好处,如下


// Visual C#

using System;
using System.Threading;

namespace SynchronizationContextSampleCS
{
    class BankAccount
    {
        public decimal balance;
        public void Deposit(decimal amount)
        {
            balance += amount;
        }
        public void Withdraw(decimal amount)
        {
            balance -= amount;
        }
    }
    class Program
    {
        static BankAccount account;
        static void Main(string[] args)
        {
            account = new BankAccount();
            SendOrPostCallback deposit = new SendOrPostCallback(Deposit);
            SendOrPostCallback withdraw = new SendOrPostCallback(Withdraw);
            SynchronizationContext ctx = new SynchronizationContext();
            ctx.Post(deposit, 500);
            ctx.Post(withdraw, 500);
            Console.ReadLine();
        }
        static void Withdraw(object state)
        {
            Console.WriteLine("Withdraw: current balance = {0:C}", account.balance);
            account.Withdraw(decimal.Parse(state.ToString()));
            Console.WriteLine("Withdraw: new balance = {0:C}", account.balance);
        }
        static void Deposit(object state)
        {
            Console.WriteLine("Withdraw: current balance = {0:C}", account.balance);
            account.Deposit(decimal.Parse(state.ToString()));
            Console.WriteLine("Deposit: new balance = {0:C}", account.balance);
        }
    }
}


转载自 张玉华

 http://blog.sina.com.cn/s/blog_5eded52b0100e583.html?retcode=0   

阅读(3336) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~