[C #. net] What is the difference between PostMessage and SendMessage,

Source: Internet
Author: User

[C #. net] What is the difference between PostMessage and SendMessage,

When using asynchronous functions such as PostMessage, sendpolicymessage, and SendMessageCallback to send system messages, pointers cannot be used in the parameters, because the sender does not wait for message processing to return, the recipient has been released before processing the pointer. 5. in Windows 2000/XP, each Message Queue can store a maximum of 10,000 Post messages. messages that have not been processed will not be processed and will be discarded directly. This value can be increased to [HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Windows] USERPostMessageLimit, with a minimum value of 4000. PostMessage is only responsible for placing messages in the message queue. It is not sure when and whether SendMessage is processed. The return code (DWord type) is waiting for the message to be processed) after the PostMessage is executed, the system returns the SendMessage immediately after the message is processed. Below is a small example to illustrate the differences between the two methods for parameter transfer:

 

// Win32 API class using System; using System. runtime. interopServices; namespace TestHwnd {public class Win32API {[DllImport ("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow (string lpClassName, string lpWindowName ); [DllImport ("User32.dll", EntryPoint = "find?wex")] public static extern IntPtr find=wex (IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, s Tring lpWindowName); // <summary> // custom structure // </summary> public struct My_lParam {public int I; public string s ;} /// <summary> /// use COPYDATASTRUCT to pass the string /// </summary> [StructLayout (LayoutKind. sequential)] public struct COPYDATASTRUCT {public IntPtr dwData; public int cbData; [financialas (UnmanagedType. LPStr)] public string lpData;} // message sending API [DllImport ("User32.dll", EntryPoint = "SendMess Age ")] public static extern int SendMessage (IntPtr hWnd, // handle of the window to which information is sent int Msg, // Message ID int wParam, // parameter 1 int lParam // parameter 2); // message sending API [DllImport ("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage (IntPtr hWnd, // int Msg, // Message ID int wParam, // parameter 1 ref My_lParam lParam // parameter 2 ); // message sending API [DllImport ("User32.dll", EntryPoint = "SendMessage")] public static Extern int SendMessage (IntPtr hWnd, // handle of the window to which information is sent int Msg, // Message ID int wParam, // parameter 1 ref COPYDATASTRUCT lParam // parameter 2 ); // message sending API [DllImport ("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage (IntPtr hWnd, // handle of the window to which information is sent int Msg, // Message ID int wParam, // parameter 1 int lParam // parameter 2); // message sending API [DllImport ("User32.dll", EntryPoint = "PostMessage")] public static extern int Po StMessage (IntPtr hWnd, // handle int Msg for the window to which information is sent, // Message ID int wParam, // parameter 1 ref My_lParam lParam // parameter 2 ); // asynchronous message sending API [DllImport ("User32.dll", EntryPoint = "PostMessage")] public static extern int PostMessage (IntPtr hWnd, // handle of the window to which information is sent int Msg, // Message ID int wParam, // parameter 1 ref COPYDATASTRUCT lParam // parameter 2) ;}// main form, send message using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. runtime. interopServices; namespace TestHwnd {public partial class Main: Form {public IntPtr hwndTest; public int IwndTest; public IntPtr hwndfrmTest; public Main () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {Test test = new Test (); test. show (this );} Private void timerjavastick (object sender, EventArgs e) {string strTest = "25425"; Win32API. COPYDATASTRUCT cds; cds. dwData = (IntPtr) 100; cds. lpData = strTest; byte [] sarr = System. text. encoding. UTF8.GetBytes (strTest); int len = sarr. length; cds. cbData = len + 1; Win32API. my_lParam lp = new Win32API. my_lParam (); lp. I = 3; lp. s = "test"; if (hwndTest! = (IntPtr) 0) {if (DateTime. now. second % 2 = 0) {Win32API. sendMessage (hwndTest, 0x60, 1, 3); // two Integer Parameters are passed.} if (DateTime. now. second % 3 = 0) {Win32API. sendMessage (hwndTest, 0x61, 5, ref lp); // The integer parameter and structure type are passed successfully. After this method is changed, the object can be passed.} if (DateTime. now. second % 5 = 0) {Win32API. sendMessage (hwndTest, 0x62, 5, ref cds); // an integer parameter and an indefinite string are passed.} if (DateTime. now. second % 7 = 0) {Win32API. postMessage (hwndTest, 0x63, 5, 6); // Two integer parameters passed} if (DateTime. now. second % 9 = 0) {Win32API. postMessage (hwndTest, 0x64, 3, ref lp); // The integer parameter is successfully passed, but the lp parameter fails. 3 can be passed successfully.} If (DateTime. now. second % 11 = 0) {Win32API. postMessage (hwndTest, 0x65, 3, ref cds); // The integer parameter is successfully passed, the parameter cds is failed, and 3 can be passed successfully. }}}}// Subform receives the message and the parameter using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; using System. runtime. interopServices; namespace TestHwnd {public partial class Test: Form {Main main; public Test () {InitializeComponent ();} private void Test_Load (object sender, EventArgs e) {Main = this. owner as Main; main. hwndTest = this. handle;} // rewrite the form's Message processing function DefWndProc. Add the processing entry for custom Message detection protected override void DefWndProc (ref Message m) {switch (m. msg) {// receives the custom message MYMESSAGE and displays its parameter case 0x60: {label1.Text = DateTime. now. toString () + "-" + m. WParam. toInt32 (). toString () + "-" + m. LParam. toInt32 (). toString ();} break; case 0x61: {Win32API. my_lParam ml = new Win32API. my_lParam (); Type t = ml. getType (); ml = (Win32API. my_lParam) m. getLParam (t); label2.Text = DateTime. now. toString () + "-" + m. WParam. toInt32 (). toString () + "-" + ml. i. toString () + ":" + ml. s;} break; case 0x62: {Win32API. COPYDATASTRUCT mystr = new Win32API. COPYDATASTRUCT (); Type mytype = mystr. getType (); mystr = (Win32API. COPYDATASTRUCT) m. getLParam (mytype); string str2 = mystr. lpData; label3.Text = DateTime. now. toString () + "-" + m. WParam. toInt32 (). toString () + "-" + str2;} break; case 0x63: {label4.Text = DateTime. now. toString () + "-" + m. WParam. toInt32 (). toString () + "-" + m. LParam. toInt32 (). toString ();} break; case 0x64: {Win32API. my_lParam ml = new Win32API. my_lParam (); Type t = ml. getType (); ml = (Win32API. my_lParam) m. getLParam (t); label5.Text = DateTime. now. toString () + "-" + m. WParam. toInt32 (). toString () + "-" + ml. i. toString () + ":" + ml. s;} break; case 0x65: {Win32API. COPYDATASTRUCT mystr = new Win32API. COPYDATASTRUCT (); Type mytype = mystr. getType (); mystr = (Win32API. COPYDATASTRUCT) m. getLParam (mytype); string str2 = mystr. lpData; label6.Text = DateTime. now. toString () + "-" + m. WParam. toInt32 (). toString () + "-" + str2;} break; default: base. defWndProc (ref m); break;} private void button#click (object sender, EventArgs e) {main. hwndTest = (IntPtr) (0); this. close ();}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.