카테고리 없음2009. 1. 23. 17:05

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

public class MainClass

 public static void Method(string strValue_1, ref string strValue_2, int time)
 {
  TestAsyncClass tac = new TestAsyncClass();
  TestAsyncDelegate tad = new TestAsyncDelegate(tac.AsyncMethod);

  TestAsyncWrapper taw = new TestAsyncWrapper(strValue_1, strValue_2);
  AsyncCallback cb = new AsyncCallback(taw.Results);

  IAsyncResult ar = tad.BeginInvoke(strValue_1, ref strValue_2, cb, null);

  for(int i=0; i<5; i++)
  {
   Console.WriteLine("다른일을 처리합니다.");
   Thread.Sleep(time);
  }

  while(true)
  {
   if(ar.IsCompleted) break;
   Console.WriteLine("처리가 끝날때까지 기다립니다.");
   Thread.Sleep(1000);
  }
 }
 
 public static void Main(String[] args)
 {
  string Value = "World";
  Console.WriteLine("============= 첫번째 동작 =============");
        Method("Hello", ref Value, 500);
  Console.WriteLine("============= 두번째 동작 =============");
  Method("Thank you", ref Value, 2000);
 }
}


public class TestAsyncClass
{
 public int AsyncMethod(string strValue_1, ref string strValue_2)
 {
  Thread.Sleep(5000);

  strValue_2 = strValue_1;
  return strValue_1.Length + strValue_2.Length;
 }
}

public delegate int TestAsyncDelegate(string strValue_1, ref string strValue_2);

public class TestAsyncWrapper
{
 private string m_strValue_1;
 private string m_strValue_2;

 public TestAsyncWrapper(string strValue_1, string strValue_2)
 {
  m_strValue_1 = strValue_1;
  m_strValue_2 = strValue_2;
 }

 [OneWayAttribute()]
 public void Results(IAsyncResult ar)
 {
  int retVal;
  string retStr = "";

  TestAsyncDelegate tad = (TestAsyncDelegate)((AsyncResult)ar).AsyncDelegate;
  retVal = tad.EndInvoke(ref retStr, ar);

  Console.WriteLine("문자의 갯수 : {0}, 반환문자열 : {1}", retVal, retStr);
 }
}

Posted by penguindori
http://msdn.microsoft.com/ko-kr/library/system.asynccallback.aspx
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Specialized;
using System.Collections;

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
    public class UseDelegateForAsyncCallback
    {
        static int requestCounter;
        static ArrayList hostData = new ArrayList();
        static StringCollection hostNames = new StringCollection();
        static void UpdateUserInterface()
        {
            // Print a message to indicate that the application
            // is still working on the remaining requests.
            Console.WriteLine("{0} requests remaining.", requestCounter);
        }
        public static void Main()
        {
            // Create the delegate that will process the results of the
            // asynchronous request.
            AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation);
            string host;
            do
            {
                Console.Write(" Enter the name of a host computer or <enter> to finish: ");
                host = Console.ReadLine();
                if (host.Length > 0)
                {
                    Interlocked.Increment(ref requestCounter);
                    Dns.BeginGetHostEntry(host, callBack, host);
                 }
            } while (host.Length > 0);
            // The user has entered all of the host names for lookup.
            // Now wait until the threads complete.
            while (requestCounter > 0)
            {
                UpdateUserInterface();
            }
            // Display the results.
            for (int i = 0; i< hostNames.Count; i++)
            {
                object data = hostData [i];
                string message = data as string;
                // A SocketException was thrown.
                if (message != null)
                {
                    Console.WriteLine("Request for {0} returned message: {1}",
                        hostNames[i], message);
                    continue;
                }
                // Get the results.
                IPHostEntry h = (IPHostEntry) data;
                string[] aliases = h.Aliases;
                IPAddress[] addresses = h.AddressList;
                if (aliases.Length > 0)
                {
                    Console.WriteLine("Aliases for {0}", hostNames[i]);
                    for (int j = 0; j < aliases.Length; j++)
                    {
                        Console.WriteLine("{0}", aliases[j]);
                    }
                }
                if (addresses.Length > 0)
                {
                    Console.WriteLine("Addresses for {0}", hostNames[i]);
                    for (int k = 0; k < addresses.Length; k++)
                    {
                        Console.WriteLine("{0}",addresses[k].ToString());
                    }
                }
            }
       }
        static void ProcessDnsInformation(IAsyncResult result)
        {
            string hostName = (string) result.AsyncState;
            hostNames.Add(hostName);
            try
            {
                IPHostEntry host = Dns.EndGetHostEntry(result);
                hostData.Add(host);
            }
            catch (SocketException e)
            {
                hostData.Add(e.Message);
            }
            finally
            {
                Interlocked.Decrement(ref requestCounter);
            }
        }
    }
}
Posted by penguindori