From: bernd on
Hi,

I want to start an user thread at startup of the sdi application.
Unfortunately I have to wait until the windows-handle is valid :

m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();

Where`s the best place to start such a thread? After these two lines
in the initInstance-function of the main window???

Within the user thread, I want to initalize an udp-socket.

best regards
Bernd
From: Scott McPhillips [MVP] on
"bernd" <bernd.schuster12(a)googlemail.com> wrote in message
news:83a458a4-cd49-4c6b-aeb4-f1f07ad8ce3b(a)e28g2000vbd.googlegroups.com...
> Hi,
>
> I want to start an user thread at startup of the sdi application.
> Unfortunately I have to wait until the windows-handle is valid :
>
> m_pMainWnd->ShowWindow(SW_SHOW);
> m_pMainWnd->UpdateWindow();
>
> Where`s the best place to start such a thread? After these two lines
> in the initInstance-function of the main window???
>
> Within the user thread, I want to initalize an udp-socket.
>
> best regards
> Bernd

The first place where the main window handle is available is the CMainFrame
WM_CREATE message handler. And CMainFrame WM_CLOSE is a good place to
manage shutdown of threads.

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
Note that sockets must work entirely within a thread if you are using CAsyncSocket (and
you NEVER want to use CSocket, but it is also true for that class). Socket objects cannot
cross thread boundaries.

More below...
On Fri, 21 May 2010 03:44:41 -0700 (PDT), bernd <bernd.schuster12(a)googlemail.com> wrote:

>Hi,
>
>I want to start an user thread at startup of the sdi application.
>Unfortunately I have to wait until the windows-handle is valid :
>
>m_pMainWnd->ShowWindow(SW_SHOW);
>m_pMainWnd->UpdateWindow();
>
>Where`s the best place to start such a thread? After these two lines
>in the initInstance-function of the main window???
>
****
If you require the window handle to be valid, then yes, you must handle it here. At
least, no earlier. You will have to shut it down in the OnClose handler. See my article
on worker threads for asynchronous shutdown techniques for threads.
****
>Within the user thread, I want to initalize an udp-socket.
****
UDP is a lousy protocol for nearly anything. Unless you are working in a very limited
context, VERY limited, it is going to be a royal pain to deal with. Why do you want to
use UDP? Note if your answer is "because it looks easier than TCP" then you are making a
VERY bad choice for the wrong reason.
joe
****
>
>best regards
>Bernd
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: bernd on
thanks for your answers.

more below:

> ****
> If you require the window handle to be valid, then yes, you must handle it here.  At
> least, no earlier.  You will have to shut it down in the OnClose handler.  See my article
> on worker threads for asynchronous shutdown techniques for threads.
> ****>Within the user thread, I want to initalize an udp-socket.

I`ll take a look at your example (shutting down a thread).

> UDP is a lousy protocol for nearly anything.  Unless you are working in a very limited
> context, VERY limited, it is going to be a royal pain to deal with.  Why do you want to
> use UDP?  Note if your answer is "because it looks easier than TCP" then you are making a
> VERY bad choice for the wrong reason.  

The protocol (e.g. dhcp client or snmp) require to deal with udp.

But in the end, I need two sockets; one for incoming udp packets and
one for incoming tcp connection requests (for the http webserver).
Both sockets have to be initialized at the startup of the programm. Do
I have to install two casyncsockets with two different threads?

best regards
Bernd


From: ScottMcP [MVP] on
On May 22, 5:51 am, bernd <bernd.schuste...(a)googlemail.com> wrote:
> But in the end, I need two sockets; one for incoming udp packets and
> one for incoming tcp connection requests (for the http webserver).
> Both sockets have to be initialized at the startup of the programm. Do
> I have to install two casyncsockets with two different threads?

Yes you will need two CAsyncSockets. (And a third one when a TCP
connection request arrives at OnAccept.)

No, you will not need two different threads. In fact you can use both
CAsyncSockets in the program's main thread. The socket notifications
(such as OnReceive) arrive as windows messages, and CAsyncSocket calls
your OnReceive when the notification arrives, just like any other
message handler function in your main thread.