By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

trouble using delegate in WndProc

P: n/a
when i use delegate like this:
-----------------
[System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{

delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromData Process);
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.S ecurity.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()));
}
--------------

everything is ok,so why and how i can handle this,thanks

Mar 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

sh***********@gmail.com wrote:
when i use delegate like this:

-----------------
[System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromData Process);
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.S ecurity.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()));
}
--------------

everything is ok,so why and how i can handle this,thanks


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.

--
Larry Lard
Replies to group please

Mar 1 '06 #2

P: n/a
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.S ecurity.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromData Process);

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

base.WndProc(ref m);

}
-----------

can u handle this,thanks

Larry Lard wrote:
sh***********@gmail.com wrote:
when i use delegate like this:

-----------------
[System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
delegate_ReplyFromDataProcess = new
PrepareDelegate_ReplyFromDataProcess(ReplyFromData Process);
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.S ecurity.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()));
}
--------------

everything is ok,so why and how i can handle this,thanks


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.

--
Larry Lard
Replies to group please


Mar 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.