BeginInvokeはInvokeと違って、非同期でデリゲートを呼び出します。別スレッドで実行されるので注意が必要です。待ったりするときはIAsyncResultを使います。
なおここで紹介しているBeginInvokeはControl::BeginInvokeとは違うものなので注意が必要です。
最もシンプル
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "stdafx.h" using namespace System; delegate void VVDelegate(void); void myfunc(void) { System::Threading::Thread::Sleep(3000); } int main() { VVDelegate^ dele = gcnew VVDelegate(&myfunc); IAsyncResult^ result = dele->BeginInvoke(nullptr, nullptr); dele->EndInvoke(result); return 0; } |
引数を渡す
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "stdafx.h" using namespace System; delegate void VIDelegate(int); void myfunc(int i) { System::Threading::Thread::Sleep(i); } int main() { VIDelegate^ dele = gcnew VIDelegate(&myfunc); IAsyncResult^ result = dele->BeginInvoke(1000,nullptr,nullptr); dele->EndInvoke(result); return 0; } |
2つの引数を渡す
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include "stdafx.h" using namespace System; delegate void VISDelegate(int, String^ s); void myfunc(int i, String^ s) { Console::WriteLine(s); System::Threading::Thread::Sleep(i); } int main() { VISDelegate^ dele = gcnew VISDelegate(&myfunc); IAsyncResult^ result = dele->BeginInvoke(1000, "Hello World!", nullptr, nullptr); dele->EndInvoke(result); return 0; } |
コールバックを設定してコールしてもらう
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include "stdafx.h" using namespace System; delegate void VISDelegate(int, String^ s); void myfunc(int i, String^ s) { Console::WriteLine(s); System::Threading::Thread::Sleep(i); } void MyAsyncCallback(IAsyncResult^ ar) { Console::WriteLine("CallBack called"); } int main() { VISDelegate^ dele = gcnew VISDelegate(&myfunc); AsyncCallback^ callback = gcnew AsyncCallback(&MyAsyncCallback); Console::WriteLine("Before BeginInvoke"); IAsyncResult^ result = dele->BeginInvoke(1000, "Hello World!", callback, nullptr); Console::WriteLine("After BeginInvoke"); dele->EndInvoke(result); Console::WriteLine("After EndInvoke"); return 0; } |
コールバック関数に値を渡す
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include "stdafx.h" using namespace System; delegate void VISDelegate(int, String^ s); void myfunc(int i, String^ s) { Console::WriteLine(s); System::Threading::Thread::Sleep(i); } void MyAsyncCallback(IAsyncResult^ ar) { Console::WriteLine("CallBack called"); Console::WriteLine(ar->AsyncState); } int main() { VISDelegate^ dele = gcnew VISDelegate(&myfunc); AsyncCallback^ callback = gcnew AsyncCallback(&MyAsyncCallback); Console::WriteLine("Before BeginInvoke"); IAsyncResult^ result = dele->BeginInvoke(1000, "Hello World!", callback, 12345); Console::WriteLine("After BeginInvoke"); dele->EndInvoke(result); Console::WriteLine("After EndInvoke"); return 0; } |
クラスの関数で
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include "stdafx.h" using namespace System; delegate void VVDelegate(void); ref class MyClass { public: static void mystaticfunc(void) { System::Threading::Thread::Sleep(3000); } void myinstfunc(void) { System::Threading::Thread::Sleep(3000); } }; int main() { { VVDelegate^ dele = gcnew VVDelegate(&MyClass::mystaticfunc); IAsyncResult^ result = dele->BeginInvoke(nullptr, nullptr); dele->EndInvoke(result); } { MyClass^ myClass = gcnew MyClass(); VVDelegate^ dele = gcnew VVDelegate(myClass, &MyClass::myinstfunc); IAsyncResult^ result = dele->BeginInvoke(nullptr, nullptr); dele->EndInvoke(result); } return 0; } |
2つのBeginInvokeを同時に待つ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#include "stdafx.h" using namespace System; delegate void VVDelegate(void); ref class MyClass { public: static void mystaticfunc(void) { System::Threading::Thread::Sleep(3000); } void myinstfunc(void) { System::Threading::Thread::Sleep(3000); } }; int main() { IAsyncResult^ result1; IAsyncResult^ result2; { VVDelegate^ dele = gcnew VVDelegate(&MyClass::mystaticfunc); result1 = dele->BeginInvoke(nullptr, nullptr); } { MyClass^ myClass = gcnew MyClass(); VVDelegate^ dele = gcnew VVDelegate(myClass, &MyClass::myinstfunc); result2 = dele->BeginInvoke(nullptr, nullptr); } array<System::Threading::WaitHandle^>^ waitHandles = gcnew array<System::Threading::WaitHandle^>(2); waitHandles[0] = result1->AsyncWaitHandle; waitHandles[1] = result2->AsyncWaitHandle; System::Threading::WaitHandle::WaitAll(waitHandles); return 0; } |