Tuesday, February 18, 2014

Quick note on Microsoft.Net Thread.Start, Delegate.BeginInvoke, Control.BeginInvoke

Thread.Start
Starts a new OS thread to execute the delegate. When the delegate returns, the thread is destroyed. This is quite a heavy-weight operation (starting and destroying a thread) so you typically only do it if the method is going to be long-running.

Delegate.BeginInvoke
Delegate.BeginInvoke will call the delegate on a thread pool thread. Once the method returns, the thread is returned to the pool to be reused by another task. The advantage of this is that queuing a method to the thread pool is relatively light-weight because you don't have to spin up a whole new thread every time.

Control.BeginInvoke

Control.BeginInvoke executes the specified delegate asynchronously on the thread that the control's underlying handle was created on. It basically takes a delegate and runs it on the thread that created the control on which you called BeginInvoke. In other words Control.BeginInvoke invokes the method on the thread for the control. UI components are inherently single-threaded and every interaction with a UI control must be done on the thread that created it. Control.BeginInvoke is a handy way to do that.

Reference links:-

TechNet Guru Award - Jan 2014 on How to make asynchronous method call in C#

Monday, January 20, 2014

How to make asynchronous method call in C#

I created an application wherein I needed to call a method and forget it; the called method will perform several operations in background. I don’t want to wait for all the operation to be completed and then proceed. I wanted to call the method to perform the operations in background and meanwhile I can proceed with other operations.

There are various ways the above problem can be solved; however I wanted to go with asynchronous method call which is demonstrated below with code example:-

Demo Console Application: Programm.CS class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MethodA();

            Console.ReadKey();
        }

        private static void MethodA()
        {
            Console.WriteLine("Method A Execution Started");

            DemoAsyncMethodCall.Capture("Call from Method A");

            MethodB();

            Console.WriteLine("Method A completed");

        }

        private static void MethodB()
        {
            Console.WriteLine("Inside Method B");
        }
    }
}


Demo Console Application: DemoAsyncMethodCall.CS class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
    public class DemoAsyncMethodCall
    {
        delegate void MethodDelegate(string message);
       
        public static void Capture(string message)
        {
            MethodDelegate dlgt = new MethodDelegate(saveData);

            IAsyncResult ar = dlgt.BeginInvoke(message, null, null);
        }
       
        private static void saveData(string message)
        {
            Console.WriteLine("Aync method call saveData with message: " + message);
        }
               
    }
}


Demo Console Application:  Output