MC++ FAQ
Saturday 04 | 17 | 2004
Async Delegates Sample
Here's a simple example showing how you can use Asynchronous invocation of delegates in MC++:
#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;

public __gc class ClassWithThreads
{
public:
   void MethodWithArg(Object* data)
   {
      Object* argt __gc[] = { __box(Thread::CurrentThread->GetHashCode()), data};
      Console::WriteLine (
             S"MethodWithArgs(): Thread Hash={0}, Data={1}", argt
          );
   }
};

public __delegate void ThreadDelegate(Object*);

int main()
{
   Console::WriteLine(S"Main(): Thread Hash={0}", __box(Thread::CurrentThread->GetHashCode()));
   ClassWithThreads* obj = new ClassWithThreads();


   ThreadDelegate* del = new ThreadDelegate(obj, &ClassWithThreads::MethodWithArg);

   IAsyncResult* ar = del->BeginInvoke (
                             S"some string",
                             0 /* AsyncCallback */,
                             0 /* asyncState */
                      );
   ar->AsyncWaitHandle->WaitOne();
}
Posted by tomasr at April 17, 2004 04:27 PM
Copyright(C) 2002, Tomas Restrepo, All rights reserved. Powered by Movable Type 2.63