Csharp/C Sharp by API/System.Runtime.Remoting.Messaging/IAsyncResult — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:10, 26 мая 2010

IAsyncResult.AsyncState

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
public delegate int BinaryOp(int x, int y);
class Program {
    static void Main(string[] args) {
        BinaryOp b = new BinaryOp(Add);
        IAsyncResult iftAR = b.BeginInvoke(10, 10, new AsyncCallback(AddComplete), "Main() thanks you for adding these numbers.");
        Console.ReadLine();
    }
    static void AddComplete(IAsyncResult itfAR) {
        AsyncResult ar = (AsyncResult)itfAR;
        BinaryOp b = (BinaryOp)ar.AsyncDelegate;
        Console.WriteLine("10 + 10 is {0}.", b.EndInvoke(itfAR));
        string msg = (string)itfAR.AsyncState;
        Console.WriteLine(msg);
    }
    static int Add(int x, int y) {
        Console.WriteLine("Add() invoked on thread {0}.", Thread.CurrentThread.GetHashCode());
        Thread.Sleep(5000);
        return x + y;
    }
}


IAsyncResult.AsyncWaitHandle

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
public delegate int BinaryOp(int x, int y);
class Program {
    static void Main(string[] args) {
        BinaryOp b = new BinaryOp(Add);
        IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);
        while (!iftAR.AsyncWaitHandle.WaitOne(2000, true)) {
            Console.WriteLine("Doing more work in Main()!");
        }
        int answer = b.EndInvoke(iftAR);
        Console.WriteLine("10 + 10 is {0}.", answer);
    }
    static int Add(int x, int y) {
        Console.WriteLine("Add() invoked on thread {0}.", Thread.CurrentThread.GetHashCode());
        Thread.Sleep(5000);
        return x + y;
    }
}