. Net uses messages for inter-process communication to achieve process interoperability, and. net processes for inter-process communication
Sometimes we may encounter the need to implement interoperability between two processes in some way, there are many ways, for example, you can try to let the two processes continuously monitor an external file, the file records the data of their respective processes; you can also use network ports to implement inter-process communication, share a piece of memory area records, and transmit data of the respective processes. net.
Yes, the message here refers to the Windows message mechanism. For I T cainiao, we can simply understand the Windows message mechanism: Windows systems can run many applications at the same time, in Windows, if you want a program to do one thing, it will send a message to notify the program of what to do.
First introduce the Windows API method SendMessage:
1 [DllImport("User32.dll", EntryPoint = "SendMessage")]2 private static extern int SendMessage(IntPtr wnd, int msg, IntPtr wP, IntPtr lP);
Rewrite the message processing process for a specified form object used to respond to a message
1 protected override void WndProc(ref System.Windows.Forms.Message msg) 2 { 3 switch(msg.Msg) 4 { 5 case 0x0400: 6 bool OnOff = Convert.ToBoolean((int)msg.LParam); 7 break; 8 default: 9 base.WndProc(ref msg); 10 break; 11 }12 }
The above code implements how the program that receives the message processes the custom message with the received Message Address (ID) 0x0400.
Now you only need to write the message content in the project for sending the message.
1 SendMessage(Program.hWnd, 0x0400, (IntPtr)0, (IntPtr)1);
Lists five methods of inter-process communication in windows.
Haha, select me! 1. CliPBoard: Commonly Used in the 16-bit era, supported by CWnd
2. The Windows message standard and the dedicated WM_COPYDATA message SENDMESSAGE () receiving end must have a window
3. Use shared memory (SharedMemory)
A. Set a shared memory area
HANDLECreateFileMapping (HANDLE, LPSECURITY_ATTRIBUTES, DWORD, LPCSTR)
Generate a file-mapping core object
LPVOIDMapViewOfFile (
HANDLEhFileMappingObject,
DWORDdwDesiredAcess,
DWORDdwFileOffsetHigh,
DWORDdwFileOffsetLow,
DWORDdwNumberOfBytesToMap
);
Get the shared memory pointer
B. Find out the shared memory
It is determined that this memory should be presented in a point-to-point (peertopeer) form. Each process must have the same capability, generate shared memory, and initialize it. Each process
You should call CreateFileMapping () and then call GetLastError (). if the returned error code is ERROR_ALREADY_EXISTS, the process can assume that the shared memory area has been opened and initialized by another process. Otherwise, the process can reasonably think that it is ranked first, then, initialize the shared memory.
In the client/server architecture, only the server process should generate and initialize the shared memory. All processes should use
HANDLEOpenFileMapping (DWORDdwDesiredAccess,
BOOLbInheritHandle,
LPCTSTRlpName );
Call MapViewOfFile () to obtain the shared memory pointer.
C. synchronous processing (Mutex)
D. Cleaningup BOOLUnmapViewOfFile (LPCVOIDlpBaseAddress );
CloseHandle ()
4. Dynamic Data Exchange (DDE) enables inter-application transfer by maintaining global memory allocation
The method is to manually place a large amount of data in the global memory, and then use the window message to pass the memory pointer. this is the method used in the 16-bit WIN era, because there is no global or local memory in WIN32, and currently there is only one kind of memory: virtual memory.
5. MessagePipe)
It is used to set a permanent communication channel between applications. Through this channel, you can read and write data as if your applications were accessing a flat file.
Anonymous pipeline (AnonymousPipes)
One-way flow, and can only flow between processes on the same computer.
NamedPipes)
Two-way, cross-network, any process can easily grasp, put into the pipeline data has a fixed format, and use ReadFile () can only read a multiple of this size.
Can be used in I/OCompletionPorts
6. Mailslots)
Broadcast Communication, a new method provided in 32 systems, supports data exchange between different hosts, and only supports mail slot customers in WIN9X
7. Windows Socket)
It provides all the functions of the message pipeline, but complies with a set of communication standards so that applications on different operating systems can communicate with each other.
8. Internet communication: allows applications to upload or download files from Internet addresses.
9. RPC: Remote Procedure Call is rarely used because it is not compatible with unix rpc.
10. Serial/parallel communication (Serial & # ...... remaining full text>
Use C # To implement inter-process communication
The inter-process communication means inter-thread communication.
Multi-thread communication is a multi-purpose delegate. A delegate can pass a method as a variable to another method.
Create a delegate in the basic thread of the thread to be created. The delegate is called in the thread.