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());
}
}
}
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());
}
}
}