. NET multi-process communication

Source: Internet
Author: User

In practical applications, we often need to share information among multiple processes and implement inter-process control transmission.

There are many ways to share a local data file or a database. But the problem also arises-constantly operating on the hard disk will cause great damage to the hard disk hardware. In addition, Because reading the hard disk requires a lot of time, it also compromises the operating efficiency of the software!

Therefore, the more advanced level is our shared memory! In the past, this seems convenient in C/C ++, but VB6 requires many API operations. For. NET, APIs can only be implemented through delegation.

Now, NET4 provides us with a powerful function-memory file ing. The memory ing can perfectly share information between multiple processes!

 

The following is a simple example:

Implementation Task: process A writes information to the memory, and process B reads the information, so that process B shares the information of process.

 

 

Process A code is as follows:

Public Class Form1
Dim MF As System. IO. MemoryMappedFiles. MemoryMappedFile 'memorymappedfile memory ing file object
Private Sub Form1_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
MF = MF. CreateNew ("SYSIO", 5000) 'creates a memory ing file in the master shared process, which is 5000 bytes in size.
End Sub

Private Sub timereffectick (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Timer1.Tick
Dim buf (4) As Byte
Dim s As Single
Dim MS As System. IO. MemoryMappedFiles. MemoryMappedViewStream 'view stream of memory ing File
MS = MF. CreateViewStream ()
S = Rnd ()
Buf = BitConverter. GetBytes (s)
Ms. Write (buf, 0, buf. Length) 'writes information to the memory ing File
End Sub
End Class

 

The process B code is as follows:

Public Class Form1
Dim MF As System. IO. MemoryMappedFiles. MemoryMappedFile
Private Sub Form1_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
MF = MF. OpenExisting ("SYSIO") 'opens a memory ing, that is, the memory ing file created in the master shared process.
End Sub


Private Sub timereffectick (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles Timer1.Tick
Dim buf () As Byte = {0, 0, 0, 0}
Dim MS As System. IO. MemoryMappedFiles. MemoryMappedViewStream
MS = MF. CreateViewStream ()
Ms. Read (buf, 0, buf. Length) 'reads the specified content in the ing File

Me. TextBox1.Text = BitConverter. ToSingle (buf, 0)
End Sub
End Class


From SANTOOK's column

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.