This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
{ | |
public: | |
static void Run() | |
{ | |
boost::thread nonGUI( FooThread ); | |
nonGUI.join(); | |
} | |
static void FooThread() | |
{ | |
Foo foo; | |
for(;;) | |
{ | |
foo.MethodInNonUserThread(); | |
} | |
} | |
void MethodInNonUserThread() | |
{ | |
PrepareDrawing(); | |
MethodThatHasToBeInUserThread(); // CRASH HERE due to AfxGetMainWnd() | |
} | |
void MethodThatHasToBeInUserThread() | |
{ | |
CMainFrame* mainFrame = (CMainFrame*)AfxGetMainWnd(); | |
mainFrame->DrawFoo(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
{ | |
void MethodInNonUserThread() | |
{ | |
PrepareDrawing(); | |
SignalGUI(); | |
WaitForGUIToFinish(); | |
} | |
void MethodThatHasToBeInUserThread() | |
{ | |
CMainFrame* mainFrame = (CMainFrame*)AfxGetMainWnd(); | |
mainFrame->DrawFoo(); | |
} | |
}; | |
class CMainFrame | |
{ | |
void Timer() | |
{ | |
if( m_Foo->IsGUISignaled() ) | |
{ | |
m_Foo->MethodThatHasToBeInUserThread(); | |
m_Foo->SignalFoo(); | |
} | |
if( m_Bar->IsGUISignaled() ) | |
{ | |
m_Bar->MethodThatHasToBeInUserThread(); | |
m_Bar->SignalBar(); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
{ | |
struct UserData | |
{ | |
HANDLE EventToFinish; | |
}; | |
// run in non-GUI thread | |
void MethodInNonUserThread() | |
{ | |
PrepareDrawing(); | |
UserData data; | |
data.EventToFinish = CreateEvent( NULL, FALSE, FALSE, NULL ); | |
m_Controller->Post( MethodThatHasToBeInUserThread, &data ); | |
WaitForSingleObject( t.EventToFinish, INFINITE ); | |
} | |
// run in GUI thread i.e. CMainFrame::Timer() call | |
static void MethodThatHasToBeInUserThread( void* arg ) | |
{ | |
UserData* data = static_cast<UserData*>(arg); | |
CMainFrame* mainFrame = (CMainFrame*)AfxGetMainWnd(); | |
mainFrame->DrawFoo(); | |
SetEvent( data->EventToFinish ); | |
} | |
}; | |
class Controller | |
{ | |
boost::asio::io_service m_Service; | |
void Post( CALLBACK_FN_USER_THREAD fn, void *user ) | |
{ | |
m_Service.dispatch( boost::bind(fn, user) ); | |
} | |
void Poll() | |
{ | |
m_Service.poll(); | |
} | |
}; | |
class CMainFrame | |
{ | |
void Timer() | |
{ | |
m_Controller->Poll(); | |
} | |
}; |