Search code examples
vb.netfunctiontaskbackground-process

Is an inline function a background task?


I'm using this function (I call it inline function as I didn't know what the common name for that is):

Private Shared _fixationDataStream As FixationDataStream

Private Shared Sub CreateAndVisualizeSensitiveFilteredFixationsStream()
    _fixationDataStream = _host.Streams.CreateFixationDataStream()
    _fixationDataStream.Begin(Function(x, y, __)
                                  Debug.Print(vbLf & "Fixation started at X: {0}, Y: {1}", x, y)
                                  _fixationBeginTime = DateTime.Now
                              End Function).Data(Function(x, y, __)
                                                     Debug.Print("During fixation, currently at: X: {0}, Y: {1}", x, y)
                                                 End Function).[End](Function(x, y, __)
                                                                         Debug.Print("Fixation ended at X: {0}, Y: {1}", x, y)

                                                                         If _fixationBeginTime <> Nothing Then
                                                                             Console.ForegroundColor = ConsoleColor.Cyan
                                                                             Debug.Print("Fixation duration: {0}", DateTime.Now - _fixationBeginTime)
                                                                             Console.ForegroundColor = _defaultForegroundColor
                                                                         End If
                                                                     End Function)
End Sub

When I use a Background Task or a FolderWatcher and do something within it like raising event, it goes horribly wrong.

I would like to know if this can happen here, too.

How does this inline function operate?

Does it run in an endless loop? And if yes, where do it get its signal to stop?

And would it be possible to use an AddHandler instead of this inline function?

Thank you.

This is how the functions like Begin, Next and End are declared:

Namespace Tobii.Interaction
Public Class FixationDataStream
    Inherits DataStreamBase(Of FixationDataBehavior, FixationData)

    Public Sub New(fixationDataBehavior As FixationDataBehavior, hasSubscribersChanged As Action, enabled As Boolean)

    Public Function Begin(action As Action(Of Double, Double, Double)) As FixationDataStream
    Public Function [End](action As Action(Of Double, Double, Double)) As FixationDataStream
    Public Function Data(action As Action(Of Double, Double, Double)) As FixationDataStream
End Class
End Namespace

Solution

  • You can use AddressOf instead of inline lambdas if you have a routine with the right signature. Consider something like the following:

    Sub BeginRoutine(ByVal x As Double, ByVal y As Double, ByVal z As Double)
        '...
    End Sub
    
    Sub DataRoutine(ByVal x As Double, ByVal y As Double, ByVal z As Double)
        '...
    End Sub
    
    Sub EndRoutine(ByVal x As Double, ByVal y As Double, ByVal z As Double)
        '...
    End Sub
    

    Then, you can use AddressOf to make delegates to pass to the API, e.g.

    _fixationDataStream.Begin(AddressOf BeginRoutine).Data(AddressOf DataRoutine).[End](AddressOf EndRoutine)
    

    Whether it's better to use inline lambdas or AddressOf depends on your code base. If all you need are one-offs, then inline is usually more convenient. On the other hand, if you will need to use the same routine for the purpose in more than one place, then AddressOf will probably be a better choice.