From: brogers5884 on
In my application I have some list controls that load fairly large amounts of
data, a few thousand records, or the query to get the records just take a
really large amount of time. So I created threads to load the list controls
instead of locking up the front end while it tries to load and it works like
a champ. My problem being is while the list is loading if the user closes
that dialog by switching to another screen or trying to close the dialog the
program errors out and shuts down. So I put code in the destructor of the
list control that initiates that the thread:

m_pThread->PostThreadMessage(WM_USER_STOPTHREAD,0,0);
while(WaitForSingleObject(m_pThread->m_hThread, 1000) != WAIT_FAILED);

Now I don't get immediate severe errors but the program hangs indefinitely
on the WaitForSingleObject command. Any ideas on how I can get that thread
to stop it's execution so the window can close gracefully and not lock up the
program? Thank you.
From: JD on

"brogers5884" <brogers5884(a)discussions.microsoft.com> wrote in message
news:1051D7E5-C396-4231-B02E-3E0B1A9B3B09(a)microsoft.com...
> In my application I have some list controls that load fairly large amounts
of
> data, a few thousand records, or the query to get the records just take a
> really large amount of time. So I created threads to load the list
controls
> instead of locking up the front end while it tries to load and it works
like
> a champ. My problem being is while the list is loading if the user closes
> that dialog by switching to another screen or trying to close the dialog
the
> program errors out and shuts down. So I put code in the destructor of the
> list control that initiates that the thread:
>
> m_pThread->PostThreadMessage(WM_USER_STOPTHREAD,0,0);
> while(WaitForSingleObject(m_pThread->m_hThread, 1000) != WAIT_FAILED);
>
> Now I don't get immediate severe errors but the program hangs indefinitely
> on the WaitForSingleObject command. Any ideas on how I can get that
thread
> to stop it's execution so the window can close gracefully and not lock up
the
> program? Thank you.

Is the thread running a message loop while it's loading the list
controls???? If not, the thread will never see the message. Also,
PostThreadMessage has some odd behavior -- a google on it might be helpful,
I'm not remembering the details.

A quick and dirty solution might be just to have a flag that's visible to
the thread -- Set the flag and wait on the handle. On each iteration thru
the lit control loading, check the flag and bail if appropriate.

JD


From: AliR on
I am assuming it is a UI thread and not a worker thread.

Have you tried:

m_pThread->PostQuitMessage();

AliR.

"brogers5884" <brogers5884(a)discussions.microsoft.com> wrote in message
news:1051D7E5-C396-4231-B02E-3E0B1A9B3B09(a)microsoft.com...
> In my application I have some list controls that load fairly large amounts
of
> data, a few thousand records, or the query to get the records just take a
> really large amount of time. So I created threads to load the list
controls
> instead of locking up the front end while it tries to load and it works
like
> a champ. My problem being is while the list is loading if the user closes
> that dialog by switching to another screen or trying to close the dialog
the
> program errors out and shuts down. So I put code in the destructor of the
> list control that initiates that the thread:
>
> m_pThread->PostThreadMessage(WM_USER_STOPTHREAD,0,0);
> while(WaitForSingleObject(m_pThread->m_hThread, 1000) != WAIT_FAILED);
>
> Now I don't get immediate severe errors but the program hangs indefinitely
> on the WaitForSingleObject command. Any ideas on how I can get that
thread
> to stop it's execution so the window can close gracefully and not lock up
the
> program? Thank you.


From: Joseph M. Newcomer on
This works only if the thread returning to the message pump. And the WaitForSingleObject
is almost certainly fatal. The fact that it times out is even more suspicious. In fact,
the piece of code is remarkably silly, in that it simply loops; INFINITE would have been a
more effective timeout.

But why do you have to wait for it at all?

And putting it in the destructor is probably the wrong place to put it.

You should shut the thread down in the OnClose handler. The destructor is far too late.

Read my essay on worker threads on my MVP Tips site.
joe

On Wed, 16 Feb 2005 12:53:01 -0800, "brogers5884" <brogers5884(a)discussions.microsoft.com>
wrote:

>In my application I have some list controls that load fairly large amounts of
>data, a few thousand records, or the query to get the records just take a
>really large amount of time. So I created threads to load the list controls
>instead of locking up the front end while it tries to load and it works like
>a champ. My problem being is while the list is loading if the user closes
>that dialog by switching to another screen or trying to close the dialog the
>program errors out and shuts down. So I put code in the destructor of the
>list control that initiates that the thread:
>
>m_pThread->PostThreadMessage(WM_USER_STOPTHREAD,0,0);
>while(WaitForSingleObject(m_pThread->m_hThread, 1000) != WAIT_FAILED);
>
>Now I don't get immediate severe errors but the program hangs indefinitely
>on the WaitForSingleObject command. Any ideas on how I can get that thread
>to stop it's execution so the window can close gracefully and not lock up the
>program? Thank you.

Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: brogers5884 on
I don't want to wait for it at all, I want it to exit immediately. In the
thread it just gets a recordset and then loops through the recordset and
loads each record into a custom drawn list control. If you try to close that
dialog while the thread is in it's loop loading the list control it blows up
in glorious fashion each and every time. I'm just trying to find some way to
tell that thread to stop doing anything and get out. Thanks for all of the
responses.

"Joseph M. Newcomer" wrote:

> This works only if the thread returning to the message pump. And the WaitForSingleObject
> is almost certainly fatal. The fact that it times out is even more suspicious. In fact,
> the piece of code is remarkably silly, in that it simply loops; INFINITE would have been a
> more effective timeout.
>
> But why do you have to wait for it at all?
>
> And putting it in the destructor is probably the wrong place to put it.
>
> You should shut the thread down in the OnClose handler. The destructor is far too late.
>
> Read my essay on worker threads on my MVP Tips site.
> joe
>
> On Wed, 16 Feb 2005 12:53:01 -0800, "brogers5884" <brogers5884(a)discussions.microsoft.com>
> wrote:
>
> >In my application I have some list controls that load fairly large amounts of
> >data, a few thousand records, or the query to get the records just take a
> >really large amount of time. So I created threads to load the list controls
> >instead of locking up the front end while it tries to load and it works like
> >a champ. My problem being is while the list is loading if the user closes
> >that dialog by switching to another screen or trying to close the dialog the
> >program errors out and shuts down. So I put code in the destructor of the
> >list control that initiates that the thread:
> >
> >m_pThread->PostThreadMessage(WM_USER_STOPTHREAD,0,0);
> >while(WaitForSingleObject(m_pThread->m_hThread, 1000) != WAIT_FAILED);
> >
> >Now I don't get immediate severe errors but the program hangs indefinitely
> >on the WaitForSingleObject command. Any ideas on how I can get that thread
> >to stop it's execution so the window can close gracefully and not lock up the
> >program? Thank you.
>
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm
>