PostMessage problem

J

James

I have a form, and that form needs to process incommimg messages.For
that I have created a message handler. The reason for creating a
seperate message handler is, to not interrupt the forms activities.
The messages would be put in a que and we need to get them from the
que. If we were not in a form we simple could have read the que in a
class running on a separate thread such as,

while(running)
{
try
{
getmessage((CMessage)Mailbox.Dequeue());
}
catch(Exception e)
{
}

here the que will block the thread, but since we are not doing
anything it is ok.

But my case since I am working with a form I cannot block the
Application.Run
since then then my form would be idle.

So when a message arrives I call the messagehanlers enque method, and
the message will be put in the que, then I will post a windows message
in the handler, this message will be received by the WndProc method,
in WndProc method I call a Deque method to get the received message
which will be returned to the forms message handling function through
a function delegate which was set up at the startup.

I need to set the MessageHanler to derive from nativeWindow and thats
where I am having problems with getting the parent.Handle. So I have
used the createparams class. But this.CreateHandle(cp) throws an
exeption.

I hope this made sense.


Can anyone help!

Thanks

here is the code,


using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class CMessageHandler : System.Windows.Forms.NativeWindow
{

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue queue;
private CMessageDelegate receiveMessage;

public CMessageHandler(CMsgDelegate receiveMessage)
{
queue = new Queue();
this.receiveMessage = receiveMessage;
}

public void Enqueue(CMessage message)
{
queue.Enqueue(message);

CreateParams cp = new CreateParams();

cp.Parent = (IntPtr)this.Handle;

this.CreateHandle(cp);

PostMessage((IntPtr)this.Handle,1234,(IntPtr)2,(IntPtr)3);

}

private CMessage GetMessage()
{
return (CMessage)queue.Dequeue();
}

protected override void WndProc(ref Message msg)
{
if (msg.Msg == 1234)
{
this.receiveMessage(GetMessage());


}
}
}
 
C

Claes Bergefall

I'm not really sure what you're trying to do
What is the original problem you're trying to solve?
Why do you have to inherit NativeWindow?
Are you sure you know how the NativeWindow class works?

You're CreateParams is clearly wrong. The following
statement
cp.Parent = (IntPtr)this.Handle;
is invalid for two reasons:
1. A control can't be the parent of itself
2. In the NativeWindow class this.Handle doesn't contain
a value unless you created it with a call to CreateHandle or
AssignHandle

/claes


James said:
I have a form, and that form needs to process incommimg messages.For
that I have created a message handler. The reason for creating a
seperate message handler is, to not interrupt the forms activities.
The messages would be put in a que and we need to get them from the
que. If we were not in a form we simple could have read the que in a
class running on a separate thread such as,

while(running)
{
try
{
getmessage((CMessage)Mailbox.Dequeue());
}
catch(Exception e)
{
}

here the que will block the thread, but since we are not doing
anything it is ok.

But my case since I am working with a form I cannot block the
Application.Run
since then then my form would be idle.

So when a message arrives I call the messagehanlers enque method, and
the message will be put in the que, then I will post a windows message
in the handler, this message will be received by the WndProc method,
in WndProc method I call a Deque method to get the received message
which will be returned to the forms message handling function through
a function delegate which was set up at the startup.

I need to set the MessageHanler to derive from nativeWindow and thats
where I am having problems with getting the parent.Handle. So I have
used the createparams class. But this.CreateHandle(cp) throws an
exeption.

I hope this made sense.


Can anyone help!

Thanks

here is the code,


using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public class CMessageHandler : System.Windows.Forms.NativeWindow
{

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue queue;
private CMessageDelegate receiveMessage;

public CMessageHandler(CMsgDelegate receiveMessage)
{
queue = new Queue();
this.receiveMessage = receiveMessage;
}

public void Enqueue(CMessage message)
{
queue.Enqueue(message);

CreateParams cp = new CreateParams();

cp.Parent = (IntPtr)this.Handle;

this.CreateHandle(cp);

PostMessage((IntPtr)this.Handle,1234,(IntPtr)2,(IntPtr)3);

}

private CMessage GetMessage()
{
return (CMessage)queue.Dequeue();
}

protected override void WndProc(ref Message msg)
{
if (msg.Msg == 1234)
{
this.receiveMessage(GetMessage());


}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top