А вот и код.
Module1 - тестовое приложение. ThreadObject - тестовый поток. ThreadEvents - поток для обработки событий.
| Код | Module Module1
Sub Main() Dim th_log As ThreadEvents(Of String()) Dim i As Integer th_log = New ThreadEvents(Of String())(AddressOf ConsoleWriteInContext)
For i = 0 To 1 Dim th As ThreadObject th = New ThreadObject(i, th_log) Next End Sub
Sub ConsoleWriteInContext(ByVal str As String()) Dim th_name As String th_name = Threading.Thread.CurrentThread.Name Console.WriteLine("To {0} from {1}: {2}", th_name, str(0), str(1)) End Sub
End Module
Class ThreadEvents(Of T) Dim thread As New Threading.Thread(AddressOf Listing) Dim stack As New Stack(Of T) Dim func As ThreadEventRun Public Delegate Sub ThreadEventRun(ByVal e As T)
Sub New(ByVal Func As ThreadEventRun) Me.func = Func thread.Name = "Event" thread.Start() End Sub
Sub AddEvent(ByVal e As T) stack.Push(e) End Sub
Private Sub Listing() Do If stack.Count > 0 Then func.Invoke(stack.Pop) Else Threading.Thread.Sleep(300) End If Loop End Sub End Class
Class ThreadObject Dim th As ThreadEvents(Of String()) Dim thread As New Threading.Thread(AddressOf Wait) Private Shared r As New Random
Sub New(ByVal Index As Integer, ByVal Th As ThreadEvents(Of String())) Me.th = Th thread.Name = "Object #" & Index thread.IsBackground = True thread.Start() End Sub
Sub Wait() Dim i, j As Integer
For i = 0 To 10 j = r.Next(1000) Threading.Thread.Sleep(j) th.AddEvent(New String() {Threading.Thread.CurrentThread.Name, j.ToString}) Next End Sub End Class
|
|