0
이벤트를 알리기 위해 콜백을 사용하는 라이브러리의 래퍼를 개발했습니다. 이 콜백은 UI의 스레드가 아닌 다른 스레드를 사용하여 호출되므로 래퍼는 다음 스크립트를 사용하여 이벤트 처리기를 WinForm 응용 프로그램의 올바른 스레드로 호출합니다. 오른쪽 스레드로 이벤트 디스패치
void AoComm::Utiles::Managed::DispatchEvent(Delegate^ ev, Object^ sender, Object^ args)
{
ComponentModel::ISynchronizeInvoke^ si;
array<Delegate^>^ handlers;
if(ev != nullptr)
{
handlers= ev->GetInvocationList();
for(int i = 0; i < handlers->Length; ++i)
{
// target implements ISynchronizeInvoke?
si = dynamic_cast<ComponentModel::ISynchronizeInvoke^>(handlers[i]->Target);
try{
if(si != nullptr && si->InvokeRequired)
{
IAsyncResult^ res = si->BeginInvoke(handlers[i], gcnew array<Object^>{sender, args});
si->EndInvoke(res);
}else{
Delegate^ del = handlers[i];
del->Method->Invoke(del->Target, gcnew array<Object^>{sender, args});
}
}catch(System::Reflection::TargetException^ e){
Exception^ innerException;
if (e->InnerException != nullptr)
{
innerException = e->InnerException;
}else{
innerException = e;
}
Threading::ThreadStart^ savestack = (Threading::ThreadStart^) Delegate::CreateDelegate(Threading::ThreadStart::typeid, innerException, "InternalPreserveStackTrace", false, false);
if(savestack != nullptr) savestack();
throw innerException;// -- now we can re-throw without trashing the stack
}
}
}
}
이 코드
꽤 잘 작동하지만, 나는 (물론, 등) 내 코드보다 동일한 작업을 수행 WPF에 대한 디스패처 클래스에 대해 읽었습니다. 그래서, 뭔가 (클래스, 메커니즘, ...) WinForms에 대한 Dispatcher 클래스에 상응합니까?감사합니다.
정보 주셔서 감사합니다. SynchronizationContext는 합리적인 방법 인 것 같습니다. – Jairo