Multithreaded App
-
I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code
UINT ThreadFunc(LPVOID pParam)
{
MessageBox("Thread Started");
return 0;
}void CAppDlg::OnButtonClkRunThread()
{
AfxBeginThread(ThreadFunc, this);
}Where am I going wrong?
Manmohan Bishnoi
-
I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code
UINT ThreadFunc(LPVOID pParam)
{
MessageBox("Thread Started");
return 0;
}void CAppDlg::OnButtonClkRunThread()
{
AfxBeginThread(ThreadFunc, this);
}Where am I going wrong?
Manmohan Bishnoi
Usually compiler errors are pretty informative, so have a look at it. I guess the error occurs in
Manmohan29 wrote:
MessageBox("Thread Started");
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code
UINT ThreadFunc(LPVOID pParam)
{
MessageBox("Thread Started");
return 0;
}void CAppDlg::OnButtonClkRunThread()
{
AfxBeginThread(ThreadFunc, this);
}Where am I going wrong?
Manmohan Bishnoi
try this ...
UINT ThreadFunc (LPVOID lpParam)
{
::MessageBox(0,L"Thread Started",L"",1);
return 0;
}void CAppDlg::OnButtonClkRunThread()
{
::AfxBeginThread (ThreadFunc,this);
} -
I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code
UINT ThreadFunc(LPVOID pParam)
{
MessageBox("Thread Started");
return 0;
}void CAppDlg::OnButtonClkRunThread()
{
AfxBeginThread(ThreadFunc, this);
}Where am I going wrong?
Manmohan Bishnoi
Manmohan29 wrote:
doesnot compile
And what does the compiler say? Be specific. To add to it,
Manmohan29 wrote:
UINT ThreadFunc(LPVOID pParam) { MessageBox("Thread Started"); //----> BAD idea! return 0; } void CAppDlg::OnButtonClkRunThread() { AfxBeginThread(ThreadFunc, this); }
Because if your main thread becomes busy into something, the newly spawned thread as well would block for displaying the message. This is because you are using a function (MessageBox from MFC) that would make use of the default message pump of your application. In other words, if you add a
Sleep(3000);
after AfxBeginThread, the message from your thread would probably be displayed after this sleep period only! I see you're just starting to learn, but threading has such "gotchas", that you'll learn along the way. Lesson learned today: It isn't a great idea to use something within a worker thread, that may in turn use the default message pump of the application (in a blocking fashion). :)“Follow your bliss.” – Joseph Campbell
-
Manmohan29 wrote:
doesnot compile
And what does the compiler say? Be specific. To add to it,
Manmohan29 wrote:
UINT ThreadFunc(LPVOID pParam) { MessageBox("Thread Started"); //----> BAD idea! return 0; } void CAppDlg::OnButtonClkRunThread() { AfxBeginThread(ThreadFunc, this); }
Because if your main thread becomes busy into something, the newly spawned thread as well would block for displaying the message. This is because you are using a function (MessageBox from MFC) that would make use of the default message pump of your application. In other words, if you add a
Sleep(3000);
after AfxBeginThread, the message from your thread would probably be displayed after this sleep period only! I see you're just starting to learn, but threading has such "gotchas", that you'll learn along the way. Lesson learned today: It isn't a great idea to use something within a worker thread, that may in turn use the default message pump of the application (in a blocking fashion). :)“Follow your bliss.” – Joseph Campbell
NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Manmohan Bishnoi
-
NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Manmohan Bishnoi
Manmohan29 wrote:
UINT CThreadDlg::ThreadFunc(LPVOID pParam) //---> Here is the problem! { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); }
The thread function should be a global function (not a member function of any class), or it must be a static member function of any class. Try this instead: in your header file:
static UINT CThreadDlg::ThreadFunc(LPVOID pParam);
in the source file:
/*static*/ UINT CThreadDlg::ThreadFunc(LPVOID pParam){
OutputDebugString(_T("*********** Entered thread function ***********\n"));
return false;
}Watch the output window of your debugger to see the text printed from your thread.
“Follow your bliss.” – Joseph Campbell
-
NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Manmohan Bishnoi
Manmohan29 wrote:
UINT CThreadDlg::ThreadFunc(LPVOID pParam)
This member has to be declared as 'static'.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Manmohan Bishnoi
My previous answer to you is updated after you edited your post to include the code. Check it.
“Follow your bliss.” – Joseph Campbell
-
My previous answer to you is updated after you edited your post to include the code. Check it.
“Follow your bliss.” – Joseph Campbell
Thanks, It works great.
Manmohan Bishnoi