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