Click here to Skip to main content
15,885,665 members
Home / Discussions / C#
   

C#

 
GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Kevin Marois18-Oct-21 4:01
professionalKevin Marois18-Oct-21 4:01 
QuestionCodility fails to compile even though it compiles in VS code? Pin
Cliff ludo13-Oct-21 6:41
Cliff ludo13-Oct-21 6:41 
AnswerRe: Codility fails to compile even though it compiles in VS code? Pin
BillWoodruff13-Oct-21 17:11
professionalBillWoodruff13-Oct-21 17:11 
Generalpass array to function Pin
Cliff ludo12-Oct-21 23:04
Cliff ludo12-Oct-21 23:04 
GeneralRe: pass array to function Pin
Richard Deeming12-Oct-21 23:25
mveRichard Deeming12-Oct-21 23:25 
GeneralRe: pass array to function Pin
Cliff ludo12-Oct-21 23:32
Cliff ludo12-Oct-21 23:32 
QuestionCreating An Async Method Pin
Kevin Marois7-Oct-21 11:02
professionalKevin Marois7-Oct-21 11:02 
AnswerRe: Creating An Async Method Pin
Richard Deeming7-Oct-21 21:41
mveRichard Deeming7-Oct-21 21:41 
The article you linked to is a poor example - it shows how to wrap a synchronous result in a Task<T>, not how to implement an asynchronous method. That's usually not a sensible thing to do:
Should I expose asynchronous wrappers for synchronous methods? - .NET Parallel Programming[^]

Your code has been mangled, and also has several severe problems. For a start: Avoid async void methods[^]

Try:
C#
static async Task Main()
{
    Console.WriteLine("Calling Test()");
    Task t = Test();
    Console.WriteLine("After Test");
    await t;
}

private static async Task Test()
{
    Console.WriteLine("Calling GetSomeData()");
    var results = await GetSomeData();
    Console.WriteLine($"Results: {results}");
}

private static async Task<int> GetSomeData()
{
    int x = 0;
    for (; x < 9000; x++)
    {
        await Task.Delay(5);
    }
    
    return x;
}
Output:
Calling Test()
Calling GetSomeData()
After Test
{delay}
Results: 9000

NB: If you're waiting for five seconds on every iteration of 900,000,000 loops, you'll have to wait roughly 142 years for your code to finish.

Waiting five seconds for every iteration of 9,000 loops would take 12½ hours to finish.

Reducing it to five milliseconds for 9,000 loops should technically let the code complete in 45 seconds. However, due to the Windows timer resolution it will almost certainly take longer.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Creating An Async Method Pin
Kevin Marois8-Oct-21 6:00
professionalKevin Marois8-Oct-21 6:00 
GeneralRe: Creating An Async Method Pin
Richard Deeming8-Oct-21 6:35
mveRichard Deeming8-Oct-21 6:35 
GeneralRe: Creating An Async Method Pin
Kevin Marois8-Oct-21 6:38
professionalKevin Marois8-Oct-21 6:38 
GeneralRe: Creating An Async Method Pin
Kevin Marois8-Oct-21 9:46
professionalKevin Marois8-Oct-21 9:46 
GeneralRe: Creating An Async Method Pin
lmoelleb9-Oct-21 0:18
lmoelleb9-Oct-21 0:18 
GeneralRe: Creating An Async Method Pin
Kevin Marois9-Oct-21 7:03
professionalKevin Marois9-Oct-21 7:03 
GeneralRe: Creating An Async Method Pin
lmoelleb10-Oct-21 8:56
lmoelleb10-Oct-21 8:56 
Question(xaml, wpf) ScrollViewer Binding confusion (beginner) (ANSWERED) Pin
Maximilien6-Oct-21 9:50
Maximilien6-Oct-21 9:50 
SuggestionRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard MacCutchan6-Oct-21 21:30
mveRichard MacCutchan6-Oct-21 21:30 
GeneralRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien7-Oct-21 3:49
Maximilien7-Oct-21 3:49 
AnswerRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard Deeming6-Oct-21 21:45
mveRichard Deeming6-Oct-21 21:45 
GeneralRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien8-Oct-21 3:03
Maximilien8-Oct-21 3:03 
QuestionCan't catch NullReferenceException while explicitly defined for that Pin
Exoskeletor1-Oct-21 23:03
Exoskeletor1-Oct-21 23:03 
AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon1-Oct-21 23:38
mvePete O'Hanlon1-Oct-21 23:38 
AnswerRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 0:33
mveOriginalGriff2-Oct-21 0:33 
JokeRe: Can't catch NullReferenceException while explicitly defined for that Pin
Pete O'Hanlon2-Oct-21 1:32
mvePete O'Hanlon2-Oct-21 1:32 
GeneralRe: Can't catch NullReferenceException while explicitly defined for that Pin
OriginalGriff2-Oct-21 2:14
mveOriginalGriff2-Oct-21 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.