Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Asynchrony #20

Open
mansellan opened this issue May 30, 2021 · 1 comment
Open

Discussion: Asynchrony #20

mansellan opened this issue May 30, 2021 · 1 comment

Comments

@mansellan
Copy link

mansellan commented May 30, 2021

Is your feature request related to a problem? Please describe.
Asynchronous programming doesn't always require a programmer to think in terms of multiple threads. .Net's async\await is an object lesson in making difficult things seem simple.

Describe the solution you'd like
Consider the following method:

Public Sub UpdateDatabase(CustomerId As Integer)
  On Error Goto DatabaseError

  _connection.Execute("UPDATE MyTable SET IsActive = 1 WHERE CustomerId = @p1", CustomerId)

DatabaseError:
  Msgbox "Failed"
End Sub

This is a synchronous call which will block the thread until the database responds. We could solve this by offloading to a background thread, but then we have to worry about thread synchronisation, exception marshalling, and protecting shared-state variables.

The async\await apparatus in .Net allows us to write async code in a very similar style to synchronous code:

Public Async Function UpdateDatabase(CustomerId As Integer) As Task
  On Error Goto DatabaseError

  Await _connection.Execute("UPDATE MyTable SET IsActive = 1 WHERE CustomerId = @p1", CustomerId)

DatabaseError:
  Msgbox "Failed"
End Sub

Describe alternatives you've considered
Multi-threading can definitely achieve asynchrony, but it's kinda a sledgehammer to crack a nut when all you want to do is to carry on whilst you wait for an I/O operation to complete.

Additional context
Async/Await is a classic example of making difficult things appear to be simple. I suspect Microsoft invested millions in making it work; it may be impossible for twinBASIC to replicate. For example, Awaiting inside of a loop requires the .Net compiler to rewrite sections of your code as a state machine. I may be asking for the moon here.

@sancarn
Copy link

sancarn commented Apr 21, 2022

Interestingly there is a way to have a COM complient asynchronous interface which coincidentally was recently discussed by Raymond Chen

@bclothier bclothier transferred this issue from twinbasic/twinbasic Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants