Could someone please show me a small snippet of code which demonstrates how to call a method asynchronously in c#?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you use action.BeginInvoke(), you have to call EndInvoke somewhere – else the framework has to hold the result of the async call on the heap, resulting in a memory leak.
If you don’t want to jump to C# 5 with the async/await keywords, you can just use the Task Parallels library in .Net 4. It’s much, much nicer than using BeginInvoke/EndInvoke, and gives a clean way to fire-and-forget for async jobs:
If you have methods to call that take parameters, you can use a lambda to simplify the call without having to create delegates:
I’m pretty sure (but admittedly not positive) that the C# 5 async/await syntax is just syntactic sugar around the Task library.