From: Scott McPhillips [MVP] on
"JRGlide" <JRGlide(a)discussions.microsoft.com> wrote in message
news:DDB63C1D-08F9-4DCF-803B-DD7B6A2D204E(a)microsoft.com...
> Today I tried using the CWinThread as David and Scott suggested, but I had
> some problems. When I created my own CWinThread and moved my InitInstance
> over from my CWinApp, I got undefines on all the Doc/View type calls, such
> as:
>
> AddDocTemplate(pDocTemplate);
> ParseCommandLine(cmdInfo);
> ProcessShellCommand(cmdInfo)
>
> I think this is because they are only defined in CWinApp and not in
> CWinThread.
>
> So then I started thinking that since CWinApp is derived from CWinThread,
> that maybe I could just call AfxBeginThread and pass it the original
> application instead, like this:
>
> CViewPointCloudApp *pointCloudThread = (CViewPointCloudApp*)
> AfxBeginThread
> (RUNTIME_CLASS (CViewPointCloudApp));
>
> It sounds good in theory anyway, but I got undefines. The RUNTIME_CLASS
> macro didn't like me passing an CWinApp instead of a CWinThread.
>
> Did I understand you right, is this how I should be doing it?
>
> Otherwise, my next idea is to either convert the program and get rid of
> the
> Doc/View and go straight to a Frame and child window (I don't really need
> Doc/View anyway)...

Those CWinApp functions are not applicable (you don't have a command line or
a shell command anyway). The objective inside the thread's InitInstance
should be to create the windows. You could keep the doc/view by looking
inside the MFC source and recreating the calls it makes, or you could simply
create a frame and child window by calling their Create.

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
There's a question about why doc/view would matter at all. But as pointed out, a document
template is not needed because there is no app to open files, and the others are
irrelevant.
joe

On Mon, 12 May 2008 23:22:02 -0400, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>
wrote:

>"JRGlide" <JRGlide(a)discussions.microsoft.com> wrote in message
>news:DDB63C1D-08F9-4DCF-803B-DD7B6A2D204E(a)microsoft.com...
>> Today I tried using the CWinThread as David and Scott suggested, but I had
>> some problems. When I created my own CWinThread and moved my InitInstance
>> over from my CWinApp, I got undefines on all the Doc/View type calls, such
>> as:
>>
>> AddDocTemplate(pDocTemplate);
>> ParseCommandLine(cmdInfo);
>> ProcessShellCommand(cmdInfo)
>>
>> I think this is because they are only defined in CWinApp and not in
>> CWinThread.
>>
>> So then I started thinking that since CWinApp is derived from CWinThread,
>> that maybe I could just call AfxBeginThread and pass it the original
>> application instead, like this:
>>
>> CViewPointCloudApp *pointCloudThread = (CViewPointCloudApp*)
>> AfxBeginThread
>> (RUNTIME_CLASS (CViewPointCloudApp));
>>
>> It sounds good in theory anyway, but I got undefines. The RUNTIME_CLASS
>> macro didn't like me passing an CWinApp instead of a CWinThread.
>>
>> Did I understand you right, is this how I should be doing it?
>>
>> Otherwise, my next idea is to either convert the program and get rid of
>> the
>> Doc/View and go straight to a Frame and child window (I don't really need
>> Doc/View anyway)...
>
>Those CWinApp functions are not applicable (you don't have a command line or
>a shell command anyway). The objective inside the thread's InitInstance
>should be to create the windows. You could keep the doc/view by looking
>inside the MFC source and recreating the calls it makes, or you could simply
>create a frame and child window by calling their Create.
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: JRGlide on

> There's a question about why doc/view would matter at all. But as pointed out, a document
> template is not needed because there is no app to open files, and the others are
> irrelevant.


The only reason I made it a doc/view was because the original program was.
After trying for while to make the doc/view work in a thread I dropped that
idea and went to a WinFrame and child window. But now I'm having trouble
with even that.

For testing purposed I created a simple non-doc/view app from the wizard and
converted it to a CWinThread. Here is how I call the thread from my hook
function:


void ViewPointCloud (void)
{

AFX_MANAGE_STATE (AfxGetStaticModuleState());

CViewPointCloudThread *pointCloudThread =
(CViewPointCloudThread*) AfxBeginThread
(RUNTIME_CLASS(CViewPointCloudThread));

WaitForSingleObject (pointCloudThread->m_hThread, INFINITE);

}

CViewPointClousThread is derived from CWinThread. The trouble I am having
is that I never come out of the WaitForSingleObject call after exiting the
GUI application. The other strange thing is that the GUI doesn't even seem
to appear until after I call the WaitForSingleObject function.

And stranger still, for debugging purposes I added an ExitInstance callback
(even though I didn't need it) and added a breakpoint. But that is never
reached.


Joe, In the "For What It's Worth Department", I discovered your flounder mvp
tips site a year or so ago and visit there every so often. But where I work
they block everything under the sun - including your site. In this case
because they considered them to be "Personal Pages".

I had to file a special request to have your site unblocked (which was
granted) but I thought it was worthwhile.

Thank you again,

Jim
From: Scott McPhillips [MVP] on
"JRGlide" <JRGlide(a)discussions.microsoft.com> wrote in message
news:BB431F6F-5989-49F3-B99E-A204B81FAE94(a)microsoft.com...
>
>> There's a question about why doc/view would matter at all. But as
>> pointed out, a document
>> template is not needed because there is no app to open files, and the
>> others are
>> irrelevant.
>
>
> The only reason I made it a doc/view was because the original program was.
> After trying for while to make the doc/view work in a thread I dropped
> that
> idea and went to a WinFrame and child window. But now I'm having trouble
> with even that.
>
> For testing purposed I created a simple non-doc/view app from the wizard
> and
> converted it to a CWinThread. Here is how I call the thread from my hook
> function:
>
>
> void ViewPointCloud (void)
> {
>
> AFX_MANAGE_STATE (AfxGetStaticModuleState());
>
> CViewPointCloudThread *pointCloudThread =
> (CViewPointCloudThread*) AfxBeginThread
> (RUNTIME_CLASS(CViewPointCloudThread));
>
> WaitForSingleObject (pointCloudThread->m_hThread, INFINITE);
>
> }
>
> CViewPointClousThread is derived from CWinThread. The trouble I am having
> is that I never come out of the WaitForSingleObject call after exiting the
> GUI application.

When your main window is destroyed (PostNCDestroy) try calling
PostQuitMessage. That is how you make the thread quit.

> The other strange thing is that the GUI doesn't even seem
> to appear until after I call the WaitForSingleObject function.

No surprise there. Starting a thread does not instantly run the thread.

There is also a problem with waiting for m_hThread. The thread frees the
handle when it exits. To avoid that problem set the CWinThread's
m_bAutoDelete to FALSE.

--
Scott McPhillips [VC++ MVP]

From: JRGlide on
I just wanted to let everyone know that I finally got this thing working - at
least with a small test case. I created a basic non-Doc/View application
with the wizard, converted it to a dll, and was able to call it from both
MATLAB and a 'C' routine. I even added a simple dialog just to make sure
that would work. Now I just need to convert my original Doc/View app to a
non-Doc/View and see how that does. I was never able to get a Doc/View
version to work as a dll, and didn't really need that support anyway.

Anway, thank you everybody for your help. You were all very helpful.
Turning the app into a thread was the trick. And Scott, your final
suggestions did the trick!

Once I get the viewer working I'll let you know how it turns out.

Thanks again,

Jim