Vb.net 等待发生事件的单元
我试图生成一个等待事件发生的代码。我使用本页中的答案进行Tcp通信。当收到数据包时,将引发Vb.net 等待发生事件的单元,vb.net,tcp,wait,Vb.net,Tcp,Wait,我试图生成一个等待事件发生的代码。我使用本页中的答案进行Tcp通信。当收到数据包时,将引发事件PacketReceived Private Sub FooBar() 'Send some Tcp Message here` 'Wait until a reply is received here 'Execute some code when reply is received End Sub 这是发送文件名和文件用途的代码。它缺乏文件存在性检查 Private
事件PacketReceived
Private Sub FooBar()
'Send some Tcp Message here`
'Wait until a reply is received here
'Execute some code when reply is received
End Sub
这是发送文件名和文件用途的代码。它缺乏文件存在性检查
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim t = New Thread(AddressOf SendFile)
Dim f = New FileAndData
f.FileList = SlideShowItems
f.FileType = "tempSlideShowImage"
t.Start(f)
End Sub
Private Sub SendFile(obj As Object)
Dim fD = CType(obj, FileAndData)
For Each item In fD.FileList.Items
Dim fileIn As New FileStream(item, FileMode.Open)
Const chunkSize = 10485760
Dim messageFileNamePkt As New TcpMessagePacket(Encoding.Default.GetBytes(Path.GetFileName(item)), TcpMessagePacket.PacketHeader.Filename)
messageFileNamePkt.Send(_etcClient.Client) 'Send file name
Dim messageFiletype As New TcpMessagePacket(Encoding.Default.GetBytes(fD.FileType), TcpMessagePacket.PacketHeader.Filetype)
messageFiletype.Send(_etcClient.Client) 'Send file type
'Wait here for reply if for file existence
While fileIn.Position < fileIn.Length
Dim bytes(chunkSize) As Byte
If fileIn.Length - fileIn.Position < bytes.Length Then
ReDim bytes(fileIn.Length - fileIn.Position - 1)
End If
fileIn.Read(bytes, 0, bytes.Length)
Dim messageFilePkt As New TcpMessagePacket(bytes, TcpMessagePacket.PacketHeader.FileData)
messageFilePkt.Send(_etcClient.Client) 'Send file chunks
End While
fileIn.Close()
Dim messageFileEnd As New TcpMessagePacket(Encoding.Default.GetBytes(""), TcpMessagePacket.PacketHeader.TransferComplete)
messageFileEnd.Send(_etcClient.Client) 'Send transfer complete
Next
End Sub
Public Class FileAndData
Public FileList As ListBox
Public FileType As String
End Class
使用
。前
Imports System.Threading
Module Module1
Private ReadOnly WaitEvent As New ManualResetEvent(True)
Sub Main()
For i = 1 To 100
WaitEvent.WaitOne() 'This is like a door. It's open because of ManualResetEvent(True)
If i = 50 Then
WaitEvent.Reset() 'This closes the door
End If
Console.WriteLine(i)
Next
Console.Read()
End Sub
Private Sub SetTheEventWhenCalled()
WaitEvent.Set() 'This on the other hand, Opens the door
End Sub
End Module
你好!这是我在这里的代码的一个很好的实现,但是有两件事我想指出,这可能会使它更有效率:1)10485760字节(10 MB)的块大小非常需要为每次发送放入网络缓冲区,并且很可能会大大降低速度通过我自己的测试,我发现理想的缓冲区大小是8192-32768(8-32KB)——甚至在局域网上也是如此。高于或低于这些值的所有内容都会导致发送速度大大减慢。(必须将其拆分为两条注释):2)当发送没有数据的数据包时,而不是使用编码。Default.GetBytes(“”)
只需执行以下操作:newbyte(){}
即可创建空字节数组。两者之间的执行速度差异可能只有几百微秒到一毫秒,但这仍然是一种改进,消除了不必要的处理。
Imports System.Threading
Module Module1
Private ReadOnly WaitEvent As New ManualResetEvent(True)
Sub Main()
For i = 1 To 100
WaitEvent.WaitOne() 'This is like a door. It's open because of ManualResetEvent(True)
If i = 50 Then
WaitEvent.Reset() 'This closes the door
End If
Console.WriteLine(i)
Next
Console.Read()
End Sub
Private Sub SetTheEventWhenCalled()
WaitEvent.Set() 'This on the other hand, Opens the door
End Sub
End Module