Click here to Skip to main content
15,886,632 members
Home / Discussions / C#
   

C#

 
GeneralRe: Matching Percent Symbol Using REGEX Pin
Liagapi29-Nov-17 16:27
Liagapi29-Nov-17 16:27 
GeneralRe: Matching Percent Symbol Using REGEX Pin
PIEBALDconsult29-Nov-17 16:46
mvePIEBALDconsult29-Nov-17 16:46 
AnswerRe: Matching Percent Symbol Using REGEX Pin
Kenneth Haugland29-Nov-17 16:31
mvaKenneth Haugland29-Nov-17 16:31 
GeneralRe: Matching Percent Symbol Using REGEX Pin
Liagapi29-Nov-17 17:02
Liagapi29-Nov-17 17:02 
GeneralRe: Matching Percent Symbol Using REGEX Pin
PIEBALDconsult29-Nov-17 17:06
mvePIEBALDconsult29-Nov-17 17:06 
GeneralRe: Matching Percent Symbol Using REGEX Pin
jschell1-Dec-17 7:48
jschell1-Dec-17 7:48 
QuestionHow To Make This Async [UPDATED] Pin
Kevin Marois29-Nov-17 6:03
professionalKevin Marois29-Nov-17 6:03 
AnswerRe: How To Make This Async [UPDATED] Pin
Dave Kreskowiak29-Nov-17 14:42
mveDave Kreskowiak29-Nov-17 14:42 
You don't. The way you set it up, it's "fire and forget".

You've got the await in the wrong place. That should go on the callers side, not the function ...Async side. The Async method should return a Task. That lets the caller await the Task.

Something like this:
C#
private Task<List<string>> GetUsersAsync()
{
    return Task.Run(() =>
    {
        List<string> userList = new List<string>() {
            "User1", "User2", "User3", "User4", "User5"
        };

        Thread.Sleep(5000);

        return userList;
    });
}

Then you can call it like this (very simple Windows Forms example):
C#
private async void button1_Click(object sender, EventArgs e)
{
    // The await just says "Put a bookmark here and return from the method until the Task returns". 
    // In this simple Windows Forms example, the code will go back to processing the message pump, 
    // keeping the UI alive.
    var users = await GetUsersAsync();

    // When the Task returns, the code will resume here and show the list that was returned.
    foreach (string user in users)
    {
        listBox1.Items.Add(user);
    }
}

Is this production quality code? NO! It's meant to be a simple example demonstrating Task/Async/Await. It doesn't handle exceptions and doesn't make sure the Click handler doesn't get called again while the first Task is still running.
System.ItDidntWorkException: Something didn't work as expected.

C# - How to debug code[^].
Seriously, go read these articles.

Dave Kreskowiak

GeneralRe: How To Make This Async [UPDATED] Pin
Kevin Marois30-Nov-17 4:19
professionalKevin Marois30-Nov-17 4:19 
AnswerRe: How To Make This Async [UPDATED] Pin
Member 1351776130-Nov-17 2:36
Member 1351776130-Nov-17 2:36 
GeneralRe: How To Make This Async [UPDATED] Pin
Dave Kreskowiak30-Nov-17 4:31
mveDave Kreskowiak30-Nov-17 4:31 
GeneralRe: How To Make This Async [UPDATED] Pin
Alex Schunk8-Jan-18 8:04
Alex Schunk8-Jan-18 8:04 
GeneralRe: How To Make This Async [UPDATED] Pin
Alex Schunk8-Jan-18 8:01
Alex Schunk8-Jan-18 8:01 
QuestionSimple Injector and Strategy pattern (Register dependency runtime based on request) Pin
nikunjmochi29-Nov-17 2:02
nikunjmochi29-Nov-17 2:02 
AnswerRe: Simple Injector and Strategy pattern (Register dependency runtime based on request) Pin
Eddy Vluggen29-Nov-17 2:26
professionalEddy Vluggen29-Nov-17 2:26 
GeneralRe: Simple Injector and Strategy pattern (Register dependency runtime based on request) Pin
nikunjmochi29-Nov-17 2:34
nikunjmochi29-Nov-17 2:34 
GeneralRe: Simple Injector and Strategy pattern (Register dependency runtime based on request) Pin
Eddy Vluggen29-Nov-17 2:52
professionalEddy Vluggen29-Nov-17 2:52 
QuestionUsing Reflection Pin
Mycroft Holmes28-Nov-17 14:21
professionalMycroft Holmes28-Nov-17 14:21 
GeneralRe: Using Reflection Pin
PIEBALDconsult28-Nov-17 14:30
mvePIEBALDconsult28-Nov-17 14:30 
AnswerRe: Using Reflection Pin
User 740747028-Nov-17 16:28
User 740747028-Nov-17 16:28 
GeneralRe: Using Reflection Pin
Mycroft Holmes28-Nov-17 23:02
professionalMycroft Holmes28-Nov-17 23:02 
GeneralRe: Using Reflection Pin
User 740747028-Nov-17 23:37
User 740747028-Nov-17 23:37 
AnswerRe: Using Reflection Pin
BillWoodruff30-Nov-17 0:35
professionalBillWoodruff30-Nov-17 0:35 
GeneralRe: Using Reflection Pin
Mycroft Holmes30-Nov-17 12:01
professionalMycroft Holmes30-Nov-17 12:01 
GeneralRe: Using Reflection Pin
BillWoodruff30-Nov-17 21:48
professionalBillWoodruff30-Nov-17 21:48 

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.