trouble using delegate in WndProc

S

shengmin.ruan

when i use delegate like this:


-----------------
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{

delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromDataProcess);
delegate_ReplyFromDataProcess.BeginInvoke(m, null, null);

}



delegate void PrepareDelegate_ReplyFromDataProcess(Message m);
private void ReplyFromDataProcess(Message m)
{
IntPtr pnt = dp.OnReply(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()));
}

-----------------



i met the error:
*********

{"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."}

*********


but if i changed the code like this:

--------------
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
IntPtr pnt = dp.OnReply(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()));
}
 
L

Larry Lard

when i use delegate like this:

-----------------
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromDataProcess);
delegate_ReplyFromDataProcess.BeginInvoke(m, null, null);
}

delegate void PrepareDelegate_ReplyFromDataProcess(Message m);
private void ReplyFromDataProcess(Message m)
{
IntPtr pnt = dp.OnReply(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()));
}

-----------------

i met the error:
*********

{"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."}

*********


but if i changed the code like this:

--------------
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
IntPtr pnt = dp.OnReply(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()));
}

I think the problem is that Delegate.BeginInvoke causes the called
method to run on a different thread, and it looks like you can't get at
the Message across a thread boundary. You might try changing the
delegate signature to use the *members* of Message, and explicitly pass
those, so that the delegate doesn't have to access the Message itself.
ie instead of taking (Message m), the delegate takes (IntPtr LParam,
Int32 Msg, IntPtr WParam). Might work.
 
S

shengmin.ruan

hi,
i've tried the way you said ,but the error is the same

------

delegate void PrepareDelegate_ReplyFromDataProcess(UInt32 wParam, Int32
lParam);
private void ReplyFromDataProcess(UInt32 wParam, Int32 lParam)
{
}

-----

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromDataProcess);

delegate_ReplyFromDataProcess.BeginInvoke(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()), null, null);



base.WndProc(ref m);

}


-----------

can u handle this,thanks





Larry said:
when i use delegate like this:

-----------------
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromDataProcess);
delegate_ReplyFromDataProcess.BeginInvoke(m, null, null);
}

delegate void PrepareDelegate_ReplyFromDataProcess(Message m);
private void ReplyFromDataProcess(Message m)
{
IntPtr pnt = dp.OnReply(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()));
}

-----------------

i met the error:
*********

{"Attempted to read or write protected memory. This is often an
indication that other memory is corrupt."}

*********


but if i changed the code like this:

--------------
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
IntPtr pnt = dp.OnReply(UInt32.Parse(m.WParam.ToString()),
Int32.Parse(m.LParam.ToString()));
}

I think the problem is that Delegate.BeginInvoke causes the called
method to run on a different thread, and it looks like you can't get at
the Message across a thread boundary. You might try changing the
delegate signature to use the *members* of Message, and explicitly pass
those, so that the delegate doesn't have to access the Message itself.
ie instead of taking (Message m), the delegate takes (IntPtr LParam,
Int32 Msg, IntPtr WParam). Might work.
 

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