C
C#8mo ago
TreF01L

Delegate with Task problem

I decided to work with delegates. I chose to do different kinds of sorting. I made a Sort class, where the name and the SortMethod response are located, but when I started to look at the results, I saw that the sorting that was not performed until that moment of output is simply not output (an error appears on result = null). Does anyone know why var result = await sortDelegate.SortMethod; doesn't work?
No description
No description
6 Replies
TreF01L
TreF01LOP8mo ago
public class SortStopwatch
{
public delegate Task<MethodResponse> SortMethod();

private List<SortDelegate> _sortsList = new List<SortDelegate>();
private List<SortDelegate> _sortsListToShow = new List<SortDelegate>();
private int[] _array;

public SortStopwatch()
{
InitializeArray();
InitializeSortsList();
ShowMainMenu();
}

private void InitializeArray()
{
Console.WriteLine("Enter length of random array: ");
if (!int.TryParse(Console.ReadLine(), out int length) || length <= 0)
{
Console.WriteLine("Your length is incorrect! Try again");
InitializeArray();
}

_array = ArrayGenerator.GenerateRandomArray(length);
}

private void InitializeSortsList()
{
_sortsList.Add(new SortDelegate(nameof(BubbleSort),
Task.Run(async () => await SortMethodFunction(BubbleSort.Sort, (int[])_array.Clone(), nameof(BubbleSort)))));

_sortsList.Add(new SortDelegate(nameof(ChickenSort) + " (Be careful! If you add it when the array length > 50, it will take some more time to sort it)",
Task.Run(async () => await SortMethodFunction(ChickenSort.Sort, (int[])_array.Clone(), nameof(ChickenSort)))));
}

private async Task<MethodResponse> SortMethodFunction(Func<int[], Task<int[]>> function, int[] array, string methodName)
{
Stopwatch stopwatch = Stopwatch.StartNew();
var result = await function(array);

stopwatch.Stop();
return new MethodResponse()
{
Name = methodName,
Time = stopwatch.Elapsed,
Array = result
};
}





}
public class SortStopwatch
{
public delegate Task<MethodResponse> SortMethod();

private List<SortDelegate> _sortsList = new List<SortDelegate>();
private List<SortDelegate> _sortsListToShow = new List<SortDelegate>();
private int[] _array;

public SortStopwatch()
{
InitializeArray();
InitializeSortsList();
ShowMainMenu();
}

private void InitializeArray()
{
Console.WriteLine("Enter length of random array: ");
if (!int.TryParse(Console.ReadLine(), out int length) || length <= 0)
{
Console.WriteLine("Your length is incorrect! Try again");
InitializeArray();
}

_array = ArrayGenerator.GenerateRandomArray(length);
}

private void InitializeSortsList()
{
_sortsList.Add(new SortDelegate(nameof(BubbleSort),
Task.Run(async () => await SortMethodFunction(BubbleSort.Sort, (int[])_array.Clone(), nameof(BubbleSort)))));

_sortsList.Add(new SortDelegate(nameof(ChickenSort) + " (Be careful! If you add it when the array length > 50, it will take some more time to sort it)",
Task.Run(async () => await SortMethodFunction(ChickenSort.Sort, (int[])_array.Clone(), nameof(ChickenSort)))));
}

private async Task<MethodResponse> SortMethodFunction(Func<int[], Task<int[]>> function, int[] array, string methodName)
{
Stopwatch stopwatch = Stopwatch.StartNew();
var result = await function(array);

stopwatch.Stop();
return new MethodResponse()
{
Name = methodName,
Time = stopwatch.Elapsed,
Array = result
};
}





}
private async void RunAndCompareSortMethodMenu(string? errorMessage = null)
{
try
{
foreach (var sortDelegate in _sortsListToShow)
{
var result = await sortDelegate.SortMethod;
Console.WriteLine($"Name: {result.Name} Time: {result.Time}");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
private async void RunAndCompareSortMethodMenu(string? errorMessage = null)
{
try
{
foreach (var sortDelegate in _sortsListToShow)
{
var result = await sortDelegate.SortMethod;
Console.WriteLine($"Name: {result.Name} Time: {result.Time}");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
reflectronic
reflectronic8mo ago
i think we will need to see the full code you can $paste it
MODiX
MODiX8mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
TreF01L
TreF01LOP8mo ago
BlazeBin - lldfmlogsxih
A tool for sharing your source code with the world!
TreF01L
TreF01LOP8mo ago
I pasted all code
reflectronic
reflectronic8mo ago
sorry, i took a nap right after asking for the code
Want results from more Discord servers?
Add your server