Skip to content

Commit

Permalink
Merge pull request #2853 from ElektroKill/vb-async
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Dec 2, 2022
2 parents 4951ef0 + 908a85e commit 8e48f63
Show file tree
Hide file tree
Showing 5 changed files with 656 additions and 132 deletions.
4 changes: 3 additions & 1 deletion ICSharpCode.Decompiler.Tests/TestCases/VBPretty/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.dll
/*.dll
/*.exe
/*.pdb
262 changes: 236 additions & 26 deletions ICSharpCode.Decompiler.Tests/TestCases/VBPretty/Async.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,255 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace EquivalentCSharpConsoleApp
using Microsoft.VisualBasic.CompilerServices;

namespace ICSharpCode.Decompiler.Tests.TestCases.VBPretty
{
static class Program
public class Async
{
public static void Main(string[] args)
private int memberField;

private static bool True()
{
return true;
}

public async void SimpleVoidMethod()
{
Console.WriteLine("Before");
await Task.Delay(TimeSpan.FromSeconds(1.0));
Console.WriteLine("After");
}

public async void VoidMethodWithoutAwait()
{
Console.WriteLine("No Await");
}

public async void EmptyVoidMethod()
{
}

public async void AwaitYield()
{
await Task.Yield();
}

public async void AwaitDefaultYieldAwaitable()
{
#if LEGACY_VBC || (OPTIMIZE && !ROSLYN4)
YieldAwaitable yieldAwaitable = default(YieldAwaitable);
YieldAwaitable yieldAwaitable2 = yieldAwaitable;
await yieldAwaitable2;
#else
await default(YieldAwaitable);
#endif
}

public async void AwaitDefaultHopToThreadPool()
{
#if LEGACY_VBC || (OPTIMIZE && !ROSLYN4)
HopToThreadPoolAwaitable hopToThreadPoolAwaitable = default(HopToThreadPoolAwaitable);
HopToThreadPoolAwaitable hopToThreadPoolAwaitable2 = hopToThreadPoolAwaitable;
await hopToThreadPoolAwaitable2;
#else
await default(HopToThreadPoolAwaitable);
#endif
}

public async Task SimpleVoidTaskMethod()
{
Console.WriteLine("Before");
await Task.Delay(TimeSpan.FromSeconds(1.0));
Console.WriteLine("After");
}

public async Task TaskMethodWithoutAwait()
{
Console.WriteLine("No Await");
}

public async Task CapturingThis()
{
await Task.Delay(memberField);
}

public async Task CapturingThisWithoutAwait()
{
Console.WriteLine(memberField);
}

public async Task<bool> SimpleBoolTaskMethod()
{
var task = new Task(ProcessDataAsync);
task.Start();
task.Wait();
Console.ReadLine();
Console.WriteLine("Before");
await Task.Delay(TimeSpan.FromSeconds(1.0));
Console.WriteLine("After");
return true;
}

public async static void ProcessDataAsync()
public async void TwoAwaitsWithDifferentAwaiterTypes()
{
Task<int> task = HandleFileAsync("C:\\enable1.txt");
Console.WriteLine("Please wait, processing");
int result = await task;
Console.WriteLine("Count: " + result.ToString());
Console.WriteLine("Before");
if (await SimpleBoolTaskMethod())
{
await Task.Delay(TimeSpan.FromSeconds(1.0));
}
Console.WriteLine("After");
}

public async void AwaitInLoopCondition()
{
while (await SimpleBoolTaskMethod())
{
Console.WriteLine("Body");
}
}

public async static Task<int> HandleFileAsync(string file)
public async Task Issue2366a()
{
Console.WriteLine("HandleFile enter");
int count = 0;
using (StreamReader reader = new StreamReader(file))
while (true)
{
string value = await reader.ReadToEndAsync();
count += value.Length;
for (var i = 0; i <= 10000; i += 1)
try
{
var x = value.GetHashCode();
if (x == 0)
count -= 1;
await Task.CompletedTask;
}
catch (Exception projectError)
{
ProjectData.SetProjectError(projectError);
ProjectData.ClearProjectError();
}
}
}

public static async Task<int> GetIntegerSumAsync(IEnumerable<int> items)
{
await Task.Delay(100);
int num = 0;
foreach (int item in items)
{
num = checked(num + item);
}
return num;
}

Console.WriteLine("HandleFile exit");
return count;
public async Task AsyncCatch(bool b, Task<int> task1, Task<int> task2)
{
try
{
Console.WriteLine("Start try");
await task1;
Console.WriteLine("End try");
}
catch (Exception projectError)
{
ProjectData.SetProjectError(projectError);
Console.WriteLine("No await");
ProjectData.ClearProjectError();
}
}

public async Task AsyncCatchThrow(bool b, Task<int> task1, Task<int> task2)
{
try
{
Console.WriteLine("Start try");
await task1;
Console.WriteLine("End try");
}
catch (Exception projectError)
{
ProjectData.SetProjectError(projectError);
Console.WriteLine("No await");
throw;
}
}

public async Task AsyncFinally(bool b, Task<int> task1, Task<int> task2)
{
try
{
Console.WriteLine("Start try");
await task1;
Console.WriteLine("End try");
}
finally
{
Console.WriteLine("No await");
}
}

public static async Task AlwaysThrow()
{
throw new Exception();
}

public static async Task InfiniteLoop()
{
while (true)
{
}
}

public static async Task InfiniteLoopWithAwait()
{
while (true)
{
await Task.Delay(10);
}
}

public async Task AsyncWithLocalVar()
{
object objectValue = RuntimeHelpers.GetObjectValue(new object());
await UseObj(RuntimeHelpers.GetObjectValue(objectValue));
await UseObj(RuntimeHelpers.GetObjectValue(objectValue));
}

public static async Task UseObj(object a)
{
}
}

public struct AsyncInStruct
{
private int i;

public async Task<int> Test(AsyncInStruct xx)
{
checked
{
xx.i++;
i++;
await Task.Yield();
return i + xx.i;
}
}
}

public struct HopToThreadPoolAwaitable : INotifyCompletion
{
public bool IsCompleted { get; set; }

public HopToThreadPoolAwaitable GetAwaiter()
{
return this;
}

public void OnCompleted(Action continuation)
{
Task.Run(continuation);
}

void INotifyCompletion.OnCompleted(Action continuation)
{
//ILSpy generated this explicit interface implementation from .override directive in OnCompleted
this.OnCompleted(continuation);
}

public void GetResult()
{
}
}
}
}
Loading

0 comments on commit 8e48f63

Please sign in to comment.