all groups > dotnet compact framework > october 2005 >
You're in the

dotnet compact framework

group:

PostMessage and C# multithread application - HOW IT REALLY WORKS


PostMessage and C# multithread application - HOW IT REALLY WORKS minimega
10/31/2005 9:15:25 AM
dotnet compact framework:
Hello to all NG.

Startint from the (working) "Asynchronous callbacks from native Win32
code" MSDN example, I modified the source code adding a second instance
of "public MyMessageWnd msgWnd;", calling them msgWnd1 and msgWnd2 (I
can observe that two classes have different .hWnd values).

Then I modified the C++ class, adding 2 new WM_ events
(WM_ASYNCDATA_START and WM_ASYNCDATA_STOP) and the code in
RandomWndDataFeeder like this:

DWORD WINAPI RandomWndDataFeeder (LPVOID lpParameter)
{

if(g_hWndManaged2 != NULL)
{
PostMessage(g_hWndManaged2, WM_ASYNCDATA_START, 0,
::GetTickCount());//0);
//::Sleep(0);
}

for(int i = 0; i < g_dwCicli; i++)
{
PostMessage(g_hWndManaged1, WM_ASYNCDATA_RECEIVED, 0,
::GetTickCount());//(LPARAM)g_pszWndSource);
//::Sleep(0);
}

if(g_hWndManaged2 != NULL)
{
PostMessage(g_hWndManaged2, WM_ASYNCDATA_STOP, 0,
::GetTickCount());//0);
//::Sleep(0);
}

return 0L;
}

The function
RANDOMDATA_API void StartWndCommunication (DWORD messageType, HWND
hWnd1, HWND hWnd2, DWORD cicli)

now has 2 HWND parameters, one for the first managed class and one for
the second managed class and is called by the managed application with:
UnmanagedWndAPI.StartWndCommunication(msgWnd1.Hwnd, msgWnd2.Hwnd,
1000);

What I doing? I want to demonstrate that with PostMessage the message
queue is asyncronous, so the C++ "for" cicle, adds the messages as
quick as possible, then ends BEFORE the C# appl has readed all the
messages in his queue (in fact C++ adds in the queue at least 8-9
messages in 1 milliseconds, and the maneged application needs at least
30-35 milliseconds to get the message and add it to the list).

[quoted text, click to view]
the second class instance, then 1000 WM_ASYNCDATA_RECEIVED in the
first class instance, and finally a WM_ASYNCDATA_STOP in the second
class instance.

Every message received is displayes on a list control. What happens if
I run the application? I get the messages in the list, clear! However
the WM_ASYNCDATA_STOP message is displayed as last item in the list,
after the last WM_ASYNCDATA_RECEIVED: this is wrong. If I send 2
different hWnd to C++ application, the 2 queues are separated, and I
have to receive the WM_ASYNCDATA_STOP in the second class instance
before then first class instance ends his messages.

If I create 2 different exe, and I pass to C++ the hWnd of the class
instance of each different exe, it works! in second exe I get the
WM_ASYNCDATA_STOP message when the first exe is receiving the 8th-9th
WM_ASYNCDATA_RECEIVED message (and there are 1000 messages in his
queue!)

Is seems that different hWnd queue in the same exe are working in
syncronous mode each other.

Who can explain me better how Windows Messages works?

Thanks.
Re: PostMessage and C# multithread application - HOW IT REALLY WORKS Paul G. Tobey [eMVP]
10/31/2005 10:25:01 AM
Sure, two windows in the same application do work synchronously to each
other. Read the code for any C/C++ Hello, World program. There's a
*single* message loop that calls GetMessage() to get the next message from
the *application message queue* and dispatch it to the right window. I
suppose that you could try to use threading to create two separate message
loops, but I'm not sure why you'd do this and there would be numerous
synchronization issues to be handled.

Paul T.

[quoted text, click to view]

Re: PostMessage and C# multithread application - HOW IT REALLY WORKS Max
11/1/2005 12:00:00 AM
Hi Paul,
thanks for your reply. You give me a reason why, so I don't need to go
over! In fact I've tried to make the window class instance in two
separated threads, but the result is that I get no callback to the wndproc
function! So, isn't possible to speak in an asyncronous way with two
windows owned by the same application (exe) with the windows message way.

Thanks,
Massimo

"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT com>
ha scritto nel messaggio news:O35m9$j3FHA.2676@TK2MSFTNGP15.phx.gbl...
[quoted text, click to view]

Re: PostMessage and C# multithread application - HOW IT REALLY WORKS Paul G. Tobey [eMVP]
11/1/2005 8:03:14 AM
Not completely asynchronous, no. There's no good reason that I can think of
for doing that. The user interface is really a synchronous object, anyway.
If you have something that wants to happen in the background, use a thread,
but don't try to associate that with a window. Just have it do its job and
use an event or post a message to the main application, when the process is
done.

Paul T.

[quoted text, click to view]

Re: PostMessage and C# multithread application - HOW IT REALLY WORKS minimega
11/1/2005 11:42:45 PM
I'm tring to understand all the way to let managed (C#) and unmanaged
(C++) speak theyself, why we have a Win32 C++ DLL that we want to port
in WinCE environment to reuse it. However we have to found a safe way
to exchange data and raise events between the two applications.

Ive found the Microsoft Example and
http://www.danielmoth.com/Blog/2004/09/ipc-with-cf-on-ce-part-1.html
link where some ways are descrived.

At this time I've not the need to have two asyncronous message queue in
the same application, but I want to go deep in this problem, why if in
future this will be needed, I don't want to use now a method that I
must change next at all.

Today I explore the P/Invoke method in the Microsoft example. Hope to
understand that this way can give asyncorous support to message and
data exchange.

Thanks,
Massimo

Paul G. Tobey [eMVP] ha scritto:

[quoted text, click to view]
AddThis Social Bookmark Button