How to write a function that calls a secondary thread and returns back to the main thread?
-
Hi This is more of a code architecture question. I'll try my best to explain what I'm trying to do. I need to create a function that have access to a secondary thread, process it, and returns back to the primary thread. Let say class B has FunctionX.
bool B::FunctionX(LPCTSTR lpszValue, CString& strResponse)
{
//Access the secondary thread to process lpszValue and assign strResponse;
//Returns true if process successful and false if process fail
}Then, class CMyView will call FunctionX like so
void CMyView::Foo()
{
B myB;
CString str;
if(myB.FunctionX(TEXT("TestValue"), str)
{
AfxMessagebox(TEXT("Oh Yeah!"));
}
else
{
AfxMessageBox(TEXT("Boohooo!"));
}
} -
Hi This is more of a code architecture question. I'll try my best to explain what I'm trying to do. I need to create a function that have access to a secondary thread, process it, and returns back to the primary thread. Let say class B has FunctionX.
bool B::FunctionX(LPCTSTR lpszValue, CString& strResponse)
{
//Access the secondary thread to process lpszValue and assign strResponse;
//Returns true if process successful and false if process fail
}Then, class CMyView will call FunctionX like so
void CMyView::Foo()
{
B myB;
CString str;
if(myB.FunctionX(TEXT("TestValue"), str)
{
AfxMessagebox(TEXT("Oh Yeah!"));
}
else
{
AfxMessageBox(TEXT("Boohooo!"));
}
}So u mean you want to create a thread(Secondary) inside a thread(primary) and then do the secondary processing and then continue the primary.
Величие не Бога может быть недооценена.
-
Hi This is more of a code architecture question. I'll try my best to explain what I'm trying to do. I need to create a function that have access to a secondary thread, process it, and returns back to the primary thread. Let say class B has FunctionX.
bool B::FunctionX(LPCTSTR lpszValue, CString& strResponse)
{
//Access the secondary thread to process lpszValue and assign strResponse;
//Returns true if process successful and false if process fail
}Then, class CMyView will call FunctionX like so
void CMyView::Foo()
{
B myB;
CString str;
if(myB.FunctionX(TEXT("TestValue"), str)
{
AfxMessagebox(TEXT("Oh Yeah!"));
}
else
{
AfxMessageBox(TEXT("Boohooo!"));
}
}I think you didn't really understand how threading works. You can't "call" a thread, a thread is running in parallel of your main thread and you cannot call it. From your example, it looks like you want to do some processing, get the results of the processing when done and display that result, all of that WITHOUT freezing the UI (that's why you want a thread, right) ? Am I correct ? If yes, then this is not the way to do it: in your Foo function, you are anyway waiting for the result of your thread (thus blocking the main thread), which means that the UI messages won't be processed anymore. The best way to solve your problem is to start the thread (in your FunctionX for instance) and then post a user defined message containing the result to the UI. I suggest you read this very good article[^] for more information.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I think you didn't really understand how threading works. You can't "call" a thread, a thread is running in parallel of your main thread and you cannot call it. From your example, it looks like you want to do some processing, get the results of the processing when done and display that result, all of that WITHOUT freezing the UI (that's why you want a thread, right) ? Am I correct ? If yes, then this is not the way to do it: in your Foo function, you are anyway waiting for the result of your thread (thus blocking the main thread), which means that the UI messages won't be processed anymore. The best way to solve your problem is to start the thread (in your FunctionX for instance) and then post a user defined message containing the result to the UI. I suggest you read this very good article[^] for more information.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++