Hi everybody,

I am experiencing a problem that I cannot somehow properly
comprehend...

In my project there is a thread function that runs OK. This function
calls just after the thread start another func

CreateScreenWindow(theApp.m_hInstance);

Its purpose is to create an additional window displaying some extra
output data. The func has the following content :

static void CreateScreenWindow(HINSTANCE hInst) {
const char* className = "ScreenWindow";

WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));

wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = className;

if (RegisterClass(&wc) == 0) {
char buf[128];
sprintf(buf, "Failed to register class (%d)", GetLastError());
MessageBox(NULL, buf, NULL, MB_OK);
return;
}
else {
char buf[128];
sprintf(buf, "Class registered");
MessageBox(NULL, buf, NULL, MB_OK);
}

RECT clientSize;
clientSize.top = 0;
clientSize.left = 0;
clientSize.right = 400;
clientSize.bottom = 250;

DWORD style = WS_BORDER | WS_CAPTION;

AdjustWindowRect(&clientSize, style, FALSE);

int winWidth = clientSize.right - clientSize.left;
int winHeight = clientSize.bottom - clientSize.top;

int winLeft = (GetSystemMetrics(SM_CXSCREEN) - winWidth) / 2;
int winTop = (GetSystemMetrics(SM_CYSCREEN) - winHeight) / 2;

hScreen = CreateWindow(className, "toolscreen", style, winLeft,
winTop, winWidth, winHeight, 0, 0, hInst, 0);

if (hScreen == 0) {
char buf[128];
sprintf(buf, "Failed to create window (%d)", GetLastError());
MessageBox(NULL, buf, NULL, MB_OK);
return;
}

ShowWindow(hScreen, SW_SHOW);
UpdateWindow(hScreen);
MoveWindow(hScreen, winLeft, winTop, winWidth, winHeight, TRUE);
}

Indeed, the windows appears on the screen and has wanted size and
caption, but not the position and the color.
What is even worse, it does not have the arrow cursor but only the
sand glass - obviously being too busy with something that I cannot
identify... This window cannot be moved either...
The thread that has created this window runs happily further and works
also properly (its task is not involved with the window - yet, but
should do later).

I tryed to comment out the both last code lines (Update.. and Move..)
with no difference in the behaviour.

I would highly appreciate any ideas aiding to solve my problem.

Kind regards

Victor

Re: Floating window beside the main app window by David

David
Tue Jun 26 07:29:00 CDT 2007

Victor wrote:
> Hi everybody,
>
> I am experiencing a problem that I cannot somehow properly
> comprehend...
>
> In my project there is a thread function that runs OK. This function
> calls just after the thread start another func
>
> CreateScreenWindow(theApp.m_hInstance);
>
> Its purpose is to create an additional window displaying some extra
> output data. The func has the following content :

Victor:

It is normally not a good idea to create windows in secondary threads.
Why do you feel you need to do this?

--
David Wilkinson
Visual C++ MVP

Re: Floating window beside the main app window by Igor

Igor
Tue Jun 26 07:49:46 CDT 2007

"Victor" <big.boss@chefmail.de> wrote in message
news:1182854009.716990.304390@c77g2000hse.googlegroups.com
> In my project there is a thread function that runs OK. This function
> calls just after the thread start another func
>
> CreateScreenWindow(theApp.m_hInstance);
>
> Its purpose is to create an additional window displaying some extra
> output data.

If you create a window on a thread, you are also responsible for running
a message pump on that thread (see GetMessage, DispatchMessage). It
appears that you are creating a window on a worker thread that has no
intention of running a message pump and delivering messages to this
window. The frozen, unresponsive window is a predictable outcome of this
design.

Create the window on the main thread, have the worker post messages to
it to update progress indicators.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Floating window beside the main app window by Victor

Victor
Tue Jun 26 08:23:44 CDT 2007

Thank you, guys!

Well I admit that it is far not the best way to place the floating
window -- certainly, it is worth to check if it may be done
differently.
This is my first exercise to do it; and it was not required - or even
known - at the beginning of the project.

This window should visually display some data that the worker thread
receives from a network and may disappear when the thread exits -
that's why placing the creation of the window and its handling it the
thread seemed to me a proper place.

The question is : which way is less expensive now - to make the window
normally functioning on the place where it is right now or to move it
elsewhere? The both ways are unknown ares for me. So I would first try
to achieve the goal as is intended originally : or may be it would be
too goofy ? - could anybody access it?..

@Igor :
Do you mean a construct like this :
TranslageMessage....DispatchMessage in the calling thread?

Regards
Victor


Re: Floating window beside the main app window by Igor

Igor
Tue Jun 26 08:31:47 CDT 2007

"Victor" <big.boss@chefmail.de> wrote in message
news:1182864224.969557.127000@m36g2000hse.googlegroups.com
> The question is : which way is less expensive now - to make the window
> normally functioning on the place where it is right now or to move it
> elsewhere?

I recommend keeping all the UI on a single thread (usually the main
thread), at least until you really know what you are doing.

> @Igor :
> Do you mean a construct like this :
> TranslageMessage....DispatchMessage in the calling thread?

I'm not sure I understand this question. A thread that creates a window
must run a message pump (which, yes, involves calls to TranslateMessage
and DispatchMessage). Your main thread already runs one (otherwise your
main window wouldn't have worked either). So just create another window
there, to feed off of the same pump.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Floating window beside the main app window by Alexander

Alexander
Tue Jun 26 14:34:43 CDT 2007

Considering your worker thread already does procedural work,
redesigning it to be event-driven seems the bigger evil than
moving your window to the main thread. However, there's
a third option - create a new thread with the sole purpose of
hosting your window. I wouldn't recommend it long term, but
might do in a crunch... Long term all UI must be on the same
thread.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Victor" <big.boss@chefmail.de> wrote in message
news:1182864224.969557.127000@m36g2000hse.googlegroups.com...
> Thank you, guys!
>
> Well I admit that it is far not the best way to place the floating
> window -- certainly, it is worth to check if it may be done
> differently.
> This is my first exercise to do it; and it was not required - or even
> known - at the beginning of the project.
>
> This window should visually display some data that the worker thread
> receives from a network and may disappear when the thread exits -
> that's why placing the creation of the window and its handling it the
> thread seemed to me a proper place.
>
> The question is : which way is less expensive now - to make the window
> normally functioning on the place where it is right now or to move it
> elsewhere? The both ways are unknown ares for me. So I would first try
> to achieve the goal as is intended originally : or may be it would be
> too goofy ? - could anybody access it?..
>
> @Igor :
> Do you mean a construct like this :
> TranslageMessage....DispatchMessage in the calling thread?
>
> Regards
> Victor
>



Re: Floating window beside the main app window by Victor

Victor
Fri Jun 29 03:32:28 CDT 2007

Thanks a lot guys - for friendly consulting and support !

I moved the creation of the window to the main thread - and it is
doing very fine now.

Best regards

Victor


Re: Floating window beside the main app window by Victor

Victor
Fri Jun 29 07:41:22 CDT 2007

Sorry, b