На самом деле не самый удачный вариант, учитывая наличие класса CompressedStack
Честно говоря, гугл по фразе fiber CompressedStack нашел весьма мало вариантов. Интересно почему?
Ну раз уж гугл находит так мало, решил набросать вот свое:
- Код: Выделить всё
Public Class Fiber
<ThreadStatic()> Private Shared mCurrentFiber As Fiber
Public Shared ReadOnly Property CurrentFiber() As Fiber
Get
Return mCurrentFiber
End Get
End Property
Dim mProc As ParameterizedThreadStart
Dim mState As Object
Private Context As CompressedStack
Sub New(ByVal nProc As ParameterizedThreadStart, ByVal nState As Object)
mProc = nProc
mState = nState
End Sub
Sub Switch()
If CurrentFiber Is Nothing Then
mCurrentFiber = Me
mProc.Invoke(mState)
mCurrentFiber = Nothing
Else
Dim nBegining As Fiber
CurrentFiber.Context = CompressedStack.Capture
nBegining = mCurrentFiber
mCurrentFiber = Me
If Context Is Nothing Then
mProc.Invoke(mState)
Else
CompressedStack.Run(Context, New ContextCallback(AddressOf CtCallback), Me)
End If
mCurrentFiber = nBegining
End If
End Sub
Sub CtCallback(ByVal s As Object)
If TypeOf s Is Fiber Then
'MsgBox("AAA")
Else
Throw New InvalidOperationException
End If
End Sub
End Class
Тестировать можно какнибудь так
- Код: Выделить всё
Module Module1
Dim f1 As New Fiber(New ParameterizedThreadStart(AddressOf Sub1), "A")
Dim f2 As New Fiber(New ParameterizedThreadStart(AddressOf Sub1), "B")
Dim f3 As New Fiber(New ParameterizedThreadStart(AddressOf Sub1), "C")
Sub Main()
f1.Switch()
Console.ReadKey()
End Sub
Sub Sub1(ByVal s As Object)
Console.WriteLine(s.ToString & "1")
If Fiber.CurrentFiber Is f1 Then
f2.Switch()
ElseIf Fiber.CurrentFiber Is f2 Then
f3.Switch()
Else
f1.Switch()
End If
Console.WriteLine(s.ToString & "2")
End Sub
End Module