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);