Click here to Skip to main content
15,899,313 members
Home / Discussions / C#
   

C#

 
Questionasync and await concept Pin
Anil Sharma198328-Sep-15 20:29
professionalAnil Sharma198328-Sep-15 20:29 
AnswerRe: async and await concept Pin
Pete O'Hanlon28-Sep-15 21:05
mvePete O'Hanlon28-Sep-15 21:05 
GeneralRe: async and await concept Pin
Anil Sharma198328-Sep-15 21:13
professionalAnil Sharma198328-Sep-15 21:13 
GeneralRe: async and await concept Pin
Pete O'Hanlon28-Sep-15 21:16
mvePete O'Hanlon28-Sep-15 21:16 
GeneralRe: async and await concept Pin
Anil Sharma198328-Sep-15 21:26
professionalAnil Sharma198328-Sep-15 21:26 
AnswerRe: async and await concept Pin
Agent__00728-Sep-15 23:19
professionalAgent__00728-Sep-15 23:19 
AnswerRe: async and await concept Pin
F-ES Sitecore29-Sep-15 1:02
professionalF-ES Sitecore29-Sep-15 1:02 
SuggestionRe: async and await concept Pin
Richard Deeming29-Sep-15 2:14
mveRichard Deeming29-Sep-15 2:14 
It's not usually recommended to use Task.Run to convert a synchronous method to an async Task, especially if you're going to immediately await that Task:
Should I expose asynchronous wrappers for synchronous methods? | Stephen Toub[^]

Instead, it's better to push the asynchronous operation down, and make the method your calling return a Task.

Also, when you're using async, you should try to avoid Thread.Sleep; use await Task.Delay(...) instead:
Visual C#: Thread.Sleep vs. Task.Delay[^]

And as Agent__007 said, if you want both methods to run in parallel, you need to use Task.WhenAll to wait for them.
C#
protected async void btnSubmit_Click(object sender, EventArgs e)
{
    Response.Write(DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + " <br/>");
    
    Task first = dowork();
    Task second = DoAnotherWork();
    await Task.WhenAll(first, second);
    
    Response.Write(DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + " <br/>");
}

async Task dowork()
{
    await Task.Delay(5000);
}

async Task DoAnotherWork()
{
    await Task.Delay(3000);
}


Since you're using ASP.NET WebForms, you should consider using the RegisterAsyncTask method[^] to run your async code, and avoid using async void where possible:
Using Asynchronous Methods in ASP.NET 4.5[^]
The Magic of using Asynchronous Methods in ASP.NET 4.5 plus an important gotcha[^]


Some useful resources:
Async and Await | Stephen Cleary[^]
Async/Await - Best Practices in Asynchronous Programming | Stephen Cleary[^]
Parallel Programming with .NET | MSDN Blogs[^]
await (C# Reference)[^]



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


Questiongrid control, tile view Pin
Member 1201862628-Sep-15 20:00
Member 1201862628-Sep-15 20:00 
AnswerRe: grid control, tile view Pin
OriginalGriff28-Sep-15 20:11
mveOriginalGriff28-Sep-15 20:11 
QuestionWhy can I save the error information into the table of SQL server ? Pin
Member 245846728-Sep-15 17:40
Member 245846728-Sep-15 17:40 
AnswerRe: Why can I save the error information into the table of SQL server ? Pin
Wendelius28-Sep-15 17:57
mentorWendelius28-Sep-15 17:57 
GeneralRe: Why can I save the error information into the table of SQL server ? Pin
Member 245846729-Sep-15 17:36
Member 245846729-Sep-15 17:36 
GeneralRe: Why can I save the error information into the table of SQL server ? Pin
Wendelius29-Sep-15 18:15
mentorWendelius29-Sep-15 18:15 
GeneralRe: Why can I save the error information into the table of SQL server ? Pin
Member 24584672-Oct-15 2:56
Member 24584672-Oct-15 2:56 
QuestionSitemapnode(breadcrumb) value changes with multiple users Pin
nitin_ion28-Sep-15 4:09
nitin_ion28-Sep-15 4:09 
AnswerRe: Sitemapnode(breadcrumb) value changes with multiple users Pin
Pete O'Hanlon28-Sep-15 4:58
mvePete O'Hanlon28-Sep-15 4:58 
AnswerRe: Sitemapnode(breadcrumb) value changes with multiple users Pin
Richard Deeming28-Sep-15 6:30
mveRichard Deeming28-Sep-15 6:30 
QuestionPort some C++ code which contains WriteFile to C# Pin
Member 1206160028-Sep-15 1:48
Member 1206160028-Sep-15 1:48 
AnswerRe: WriteFile equivalent in C# Pin
Pete O'Hanlon28-Sep-15 3:00
mvePete O'Hanlon28-Sep-15 3:00 
GeneralRe: WriteFile equivalent in C# Pin
Member 1206160028-Sep-15 3:18
Member 1206160028-Sep-15 3:18 
GeneralRe: WriteFile equivalent in C# Pin
Member 1206160028-Sep-15 3:21
Member 1206160028-Sep-15 3:21 
GeneralRe: WriteFile equivalent in C# Pin
Dave Kreskowiak28-Sep-15 3:40
mveDave Kreskowiak28-Sep-15 3:40 
GeneralRe: WriteFile equivalent in C# Pin
Member 1206160028-Sep-15 3:55
Member 1206160028-Sep-15 3:55 
GeneralRe: WriteFile equivalent in C# Pin
Dave Kreskowiak28-Sep-15 4:09
mveDave Kreskowiak28-Sep-15 4:09 

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.