background preloader

AsyncTasks & Scheduling

Facebook Twitter

Asynchronous Method Invocation. Introduction In this article, I am going to explain asynchronous method calls and how to use them.

Asynchronous Method Invocation

After playing with delegates, threads, and asynchronous invocation for so long, it would be a sin not to share some of my wisdom and knowledge on the subject, so hopefully, you won’t be looking at an MSDN article at 1 AM wondering why you decided to go into computers. I will try to use baby steps and lots of examples… Overall, I will cover how to call methods asynchronously, how to pass parameters to such methods, and how to find out when a method completes execution. Finally, I will show the Command Pattern in use for simplifying some of the code. The big advantage with .NET asynchronous method invocation is that you can take any method you have in your project, and you can call it asynchronously without touching the code of your method. Synchronous vs.

Synchronous method invocation Suppose we have a function Foo() that takes 10 seconds to execute. Sending Emails Asynchronously in C# We're gearing up to begin sending out our email newsletter here at and so we've been experimenting with different ways to send out a large number of emails to subscribers.

Sending Emails Asynchronously in C#

Since the SMTP class does not expose an asynchronous version of the SmtpMail.Send(Message) method, (e.g. BeginSend / EndSend) we either have to use a commercial or open source component that does, or roll our own. Fortunately the Framework comes to our rescue here with callback delegates and the BeginInvoke method, which (unfortunately) is only documented by Microsoft for Windows Forms controls (making developers think that that's the only place it's actually available to use).

To add to the potential confusion, the BeginInvoke / EndInvoke methods for Windows Forms controls are actually a completely different animal from what we are discussing here with AsyncCallbacks. Suffice to say that the methods we want to use here are in fact available anytime you use a delegate. Threading in C# - Free E-book.

Threading in C# Joseph Albahari Last updated: 2011-4-27 Translations: Chinese | Czech | Persian | Russian | Japanese Download PDF Part 1: Getting Started C# supports parallel execution of code through multithreading.

Threading in C# - Free E-book

A C# client program (Console, WPF, or Windows Forms) starts in a single thread created automatically by the CLR and operating system (the “main” thread), and is made multithreaded by creating additional threads. All examples assume the following namespaces are imported: using System; using System.Threading; class ThreadTest{ static void Main() { Thread t = new Thread (WriteY); t.Start(); for (int i = 0; i < 1000; i++) Console.Write ("x"); } static void WriteY() { for (int i = 0; i < 1000; i++) Console.Write ("y"); }} The main thread creates a new thread t on which it runs a method that repeatedly prints the character “y”.

Once started, a thread’s IsAlive property returns true, until the point where the thread ends. Done static void Go(){ if (! Done Done (usually!) Join and Sleep. ASP.NET: Long Running Task with Page Feedback. “It's not that I'm so smart , it's just that I stay with problems longer .” -- Einstein We often get questions about how to provide feedback in an ASP.NET page when something is going on in the background.

ASP.NET: Long Running Task with Page Feedback

A lot of times developers get confused about the difference between running a task asynchronously and running a task on a background thread. While these are similar operations, they have many differences. "Asynchronous" implies the use of a callback mechanism. Just kicking off a method on a separate thread does not make it asynchronous (although, as in this example, that is all we really need to do). Now let's look at one of a number of techniques to run a long - running task from an ASP.NET page and provide feedback to the user while the task is running. Namespace LongRunningTask using System; using System.Threading; /// <summary> /// Class to wrap and handle any long running task /// </summary> Asynchronous Method Invocation.