vb.net 使用内存映射文件进行进程通信
vb.net proccess communication using memory mapped files
你好。我转换了这个 c# 代码:
我正在使用这个项目:https://code.msdn.microsoft.com/Inter-process-communication-e96e94e7
private void frmMain_Load(object sender, EventArgs e)
{
communicator = new MemoryMappedFileCommunicator("MemoryMappedShare", 4096);
// This process reads data that begins in the position 0 and writes starting from the position 2000.
communicator.ReadPosition = 0;
communicator.WritePosition = 2000;
// Creates an handler for the event that is raised when data are available in the
// MemoryMappedFile.
communicator.DataReceived += new EventHandler<MemoryMappedDataReceivedEventArgs>(communicator_DataReceived);
communicator.StartReader();
}
private void communicator_DataReceived(object sender, MemoryMappedDataReceivedEventArgs e)
{
var receivedMessage = System.Text.Encoding.UTF8.GetString(e.Data);
lstMessages.Items.Add(receivedMessage);
lstMessages.SelectedIndex = lstMessages.Items.Count - 1;
// Sends a message as a response.
communicator.Write("Message from Windows App: data received at " + DateTime.Now);
}
到 vb.net 这样:
Dim communicator As New MemoryMappedFileCommunicator("MemoryMappedShare", 4096)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This process reads data that begins in the position 2000 and writes starting from the position 0.
communicator.ReadPosition = 2000
communicator.WritePosition = 0
' Creates an handler for the event that is raised when data are available in the MemoryMappedFile.
AddHandler communicator.DataReceived, AddressOf New EventHandler(Of MemoryMappedDataReceivedEventArgs)(communicator_DataReceived)
communicator.StartReader()
End Sub
Private Sub communicator_DataReceived(ByVal sender As Object, ByVal e As MemoryMappedDataReceivedEventArgs)
Dim receivedMessage = System.Text.Encoding.UTF8.GetString(e.Data)
lstMessages.Items.Add(receivedMessage)
lstMessages.SelectedIndex = lstMessages.Items.Count - 1
' Sends a message as a response.
communicator.Write("Message from Windows App: data received at " + DateTime.Now)
End Sub
转换后,我在 vb.net 代码中遇到如下错误:
VB.NET ERROR
EDelegate 'System.EventHandler(Of MemoryMappedFileManager.MemoryMappedDataReceivedEventArgs)' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.
在这一行:
AddHandler communicator.DataReceived, AddressOf New EventHandler(Of MemoryMappedDataReceivedEventArgs)(communicator_DataReceived)
您不能同时使用 AddressOf
和 New EventHandler
,因为 AddressOf
也会创建委托。
推荐的解决办法是改成:
AddHandler communicator.DataReceived, AddressOf communicator_DataReceived
以下内容同样有效,但可读性较差:
AddHandler communicator.DataReceived, New EventHandler(Of MemoryMappedDataReceivedEventArgs)(AddressOf communicator_DataReceived)
你好。我转换了这个 c# 代码:
我正在使用这个项目:https://code.msdn.microsoft.com/Inter-process-communication-e96e94e7
private void frmMain_Load(object sender, EventArgs e)
{
communicator = new MemoryMappedFileCommunicator("MemoryMappedShare", 4096);
// This process reads data that begins in the position 0 and writes starting from the position 2000.
communicator.ReadPosition = 0;
communicator.WritePosition = 2000;
// Creates an handler for the event that is raised when data are available in the
// MemoryMappedFile.
communicator.DataReceived += new EventHandler<MemoryMappedDataReceivedEventArgs>(communicator_DataReceived);
communicator.StartReader();
}
private void communicator_DataReceived(object sender, MemoryMappedDataReceivedEventArgs e)
{
var receivedMessage = System.Text.Encoding.UTF8.GetString(e.Data);
lstMessages.Items.Add(receivedMessage);
lstMessages.SelectedIndex = lstMessages.Items.Count - 1;
// Sends a message as a response.
communicator.Write("Message from Windows App: data received at " + DateTime.Now);
}
到 vb.net 这样:
Dim communicator As New MemoryMappedFileCommunicator("MemoryMappedShare", 4096)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' This process reads data that begins in the position 2000 and writes starting from the position 0.
communicator.ReadPosition = 2000
communicator.WritePosition = 0
' Creates an handler for the event that is raised when data are available in the MemoryMappedFile.
AddHandler communicator.DataReceived, AddressOf New EventHandler(Of MemoryMappedDataReceivedEventArgs)(communicator_DataReceived)
communicator.StartReader()
End Sub
Private Sub communicator_DataReceived(ByVal sender As Object, ByVal e As MemoryMappedDataReceivedEventArgs)
Dim receivedMessage = System.Text.Encoding.UTF8.GetString(e.Data)
lstMessages.Items.Add(receivedMessage)
lstMessages.SelectedIndex = lstMessages.Items.Count - 1
' Sends a message as a response.
communicator.Write("Message from Windows App: data received at " + DateTime.Now)
End Sub
转换后,我在 vb.net 代码中遇到如下错误: VB.NET ERROR
EDelegate 'System.EventHandler(Of MemoryMappedFileManager.MemoryMappedDataReceivedEventArgs)' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.
在这一行:
AddHandler communicator.DataReceived, AddressOf New EventHandler(Of MemoryMappedDataReceivedEventArgs)(communicator_DataReceived)
您不能同时使用 AddressOf
和 New EventHandler
,因为 AddressOf
也会创建委托。
推荐的解决办法是改成:
AddHandler communicator.DataReceived, AddressOf communicator_DataReceived
以下内容同样有效,但可读性较差:
AddHandler communicator.DataReceived, New EventHandler(Of MemoryMappedDataReceivedEventArgs)(AddressOf communicator_DataReceived)