determining if current thread is main thread 
Author Message
 determining if current thread is main thread

I have some functions that can take a few seconds to complete. The
user can either wait ( with an unresponsive UI) or spawn the function
via a worker thread and periodically poll a boolean which is set by
the function on completion. The function itself has some AfxMessageBox
es that report diagnostic info. I believe that AfxMessageBox shouldn't
be used in a worker thread, so I plan to bracket them in some kind of
conditional that can tell if the call is being made from the main
thread or a worker thread and then not bother displaying the boxes
from the worker thread (maybe I'll write the message to a file or
something).

My question is:

Is there an MFC/API function that I can call like this:

if(AfxGetThisThreadContext() == MAIN_THREAD)
{
  bla bla bla

Quote:
}

thanks

martin



Tue, 07 Sep 2004 18:22:29 GMT  
 determining if current thread is main thread
CWinApp *AfxGetApp()
CWinThread *AfxGetThread()

CWinApp is derived from CWinApp so probably

if ( ::AfxGetThread() == ::AfxGetApp() )

But only if your thread is CWinThread derived.


Quote:
> I have some functions that can take a few seconds to complete. The
> user can either wait ( with an unresponsive UI) or spawn the function
> via a worker thread and periodically poll a boolean which is set by
> the function on completion. The function itself has some AfxMessageBox
> es that report diagnostic info. I believe that AfxMessageBox shouldn't
> be used in a worker thread, so I plan to bracket them in some kind of
> conditional that can tell if the call is being made from the main
> thread or a worker thread and then not bother displaying the boxes
> from the worker thread (maybe I'll write the message to a file or
> something).

> My question is:

> Is there an MFC/API function that I can call like this:

> if(AfxGetThisThreadContext() == MAIN_THREAD)
> {
>   bla bla bla
> }

> thanks

> martin



Tue, 07 Sep 2004 18:51:43 GMT  
 determining if current thread is main thread
When your thread is first spawned, the code "knows" it is in a worker thread.
At some point, of course, it will call other code in which the thread will be
ambiguous, but it does "know" at the beginning of thread execution that it is a
worker thread.  At that point, can you not save that knowledge and pass it to
the function (directly or indirectly), so that the function will also know
whether or not to do UI?


Tue, 07 Sep 2004 22:46:25 GMT  
 determining if current thread is main thread
A bit of confusion here. When you say the "user", do you mean the person sitting at the
GUI interface of the program, or a programmer using your subroutine library? In the former
case, I'd suggest never worrying about the issue, but simply spawning the thread all the
time. And if a programmer, "polling for a boolean" is the worst possible way to deal with
the notification issue; I would suggest defining a user-defined message, preferrably a
Registered Window Message, that the thread posts to the main GUI thread (when starting the
thread, pass in a CWnd * that the message is to be posted to) to indicate it has
completed. But a key idea is that a worker thread must never pop up a messagebox. I
usually just post diagnostic messages to a window and log them into a listbox; this saves
ever having to do a messagebox in the thread. You can download such a control from my MVP
Tips site as my logging listbox control.
                                joe


Quote:
>I have some functions that can take a few seconds to complete. The
>user can either wait ( with an unresponsive UI) or spawn the function
>via a worker thread and periodically poll a boolean which is set by
>the function on completion. The function itself has some AfxMessageBox
>es that report diagnostic info. I believe that AfxMessageBox shouldn't
>be used in a worker thread, so I plan to bracket them in some kind of
>conditional that can tell if the call is being made from the main
>thread or a worker thread and then not bother displaying the boxes
>from the worker thread (maybe I'll write the message to a file or
>something).

>My question is:

>Is there an MFC/API function that I can call like this:

>if(AfxGetThisThreadContext() == MAIN_THREAD)
>{
>  bla bla bla
>}

>thanks

>martin

Joseph M. Newcomer [MVP]

Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm


Wed, 08 Sep 2004 01:26:36 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Determine if current thread is a worker thread

2. main GUI thread -vs- Main Frame Thread

3. worker thread vs main thread?

4. Handling events from a worker thread in the main thread

5. Knowing when threads have finished without locking the main thread

6. component in main threads used in worker threads

7. second thread stops while main thread is busy

8. How to Send messages from worker thread to main thread

9. killing threads from the main thread

10. Synchronization problem between main thread and worker thread

11. Posting a message from a worker thread to the main app thread

12. How do you determine if the current thread is a worker thread?

 

 
Powered by phpBB® Forum Software