| C# | COBOL | VB.NET |
|---|---|---|
// Calling async methods
await Task.Delay(1000);
var items = await ProcessItemsAsync("user", 8);
// Run code on background thread
await Task.Run(
() => {
Console.WriteLine("On background thread");
});
// An async void method
async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
await Task.Delay(1000);
button1.Enabled = true;
}
// An async method returning no result
async Task ProcessAsync()
{
await Task.Yield();
Console.WriteLine("async...");
}
// An async method returning a result
async Task<string[]> ProcessItemsAsync(string type, int count)
{
await Task.Delay(1000);
return new[] { "a", "b", "c" };
}
// An async value-task method returning a result
async ValueTask<string> MaybeProcess(bool x)
{
if (x) {
await Task.Delay(1000);
return "x";
}
else
{
return "y";
}
}
|
*> Calling async methods
invoke await type Task::Delay(1000)
declare items = await ProcessItemsAsync("user", 8)
*> Run code on background thread
await type Task::Run(
delegate
invoke type Console::WriteLine("On background thread")
end-delegate)
*> An async void method
method-id button1_Click async-void (sender as object,
e as type EventArgs).
set button1::Enabled to false
invoke await type Task::Delay(1000)
set button1::Enabled to true
end method.
*> An async method returning no result
method-id ProcessAsync() async.
invoke await type Task::Yield()
invoke type Console::WriteLine("async...")
end method.
*> An async method returning a result
method-id ProcessItemsAsync async (#type as string,
#count as binary-long)
yielding items as string occurs any.
invoke await type Task::Delay(1000)
set items to table of ("a", "b", "c")
end method.
*> An async value-task method returning a result
method-id MaybeProcess async-value (x as condition-value)
yielding result as string.
if x
invoke await type Task::Delay(1000)
set result to "x"
else
set result to "y"
end-if
end method.
|
' Calling async methods
Await Task.Delay(1000)
Dim items = Await ProcessItemsAsync("user", 8)
' Run code on background thread
Await Task.Run(
Sub()
Console.WriteLine("On background thread")
End Sub)
' An async void method
Async Sub button1_Click(sender As Object, e As EventArgs)
button1.Enabled = False
Await Task.Delay(1000)
button1.Enabled = True
End Sub
' An async method returning no result
Async Function ProcessAsync() As Task
Await Task.Yield()
Console.WriteLine("async...")
End Function
' An async method returning a result
Async Function ProcessItemsAsync([type] As String, count As Integer) As Task(Of String())
Await Task.Delay(1000)
Return New String() {"a", "b", "c"}
End Function
' Async value-task methods are not currently supported in VB.NET
|
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.