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. 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.

Although most of the magic is within the .NET framework, it is important to see what is going on in the back, and that’s what we are going to study here. Sending Emails Asynchronously in C# We're gearing up to begin sending out our email newsletter here at Eggheadcafe.com and so we've been experimenting with different ways to send out a large number of emails to subscribers.

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.

A thread is an independent execution path, able to run simultaneously with other threads. 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”.

Done Static fields offer another way to share data between threads. 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.

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. 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> public class LengthyTask // property to indicate if task has run at least once. private bool _firstRunComplete = false; public bool firstRunComplete get { return _firstRunComplete; } // property to indicate iftask is running. public bool Running.

Asynchronous Method Invocation.