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



Monday, August 5, 2013

Unable to cast COM object of type 'Microsoft.Office.Interop.PowerPoint.ApplicationClass' to interface type 'Microsoft.Office.Interop.PowerPoint._Application

My team have been struggling with COM Component issue for about one week, it almost frustrated the developer, me and project manager. Uff.. after intense amount of googling and trial method, several time repair and installation of office, the problem was still bugging all the team members. Finally the issue was resolved with couple of settings in Registry. The real cause of the issue was a mystery. Finally we succeeded to identify the reason for the issue and then killed the exception.

Exception thrown by the system:-

Below is the most boring exception frustrated me every time when I debug the code

System.InvalidCastException was unhandled
  Message=Unable to cast COM object of type 'Microsoft.Office.Interop.PowerPoint.ApplicationClass' to interface type 'Microsoft.Office.Interop.PowerPoint._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{91493442-5A91-11CF-8700-00AA0060263B}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

  Source=Microsoft.Office.Interop.PowerPoint

Read the entire post at :  

Wednesday, October 12, 2011

Modal popup dialog box using JQuery

Before going into the implementation let me first share the screen shot how the popup will look like (and obviously you can customize it as per your branding needs)



Just download the css, images and Java script files from below link
Above download will give you a demo html page, you can just take a look and copy pastes the code on your page. This works great!!
For more detail on the JQuery modal dialog box you can visit to http://www.ericmmartin.com/projects/simplemodal-demos/
 
 

Saturday, June 4, 2011

Modifying Web.Config file and IIS Reset


What do you think changing in web.config file reset the IIS? Are you sure??

When you say IIS RESET do you know IIS Reset stops and restarts the entire web server
Lets take a look at IIS.
In IIS you can create multiple websites, application pools. You can run multiple applications under one application pool. Now think in your IIS you have so many applications running, and you just modified one of your application web.config file and if you are thinking modifying web.config file reset the IIS then that means you have impacted all the applications running on the IIS server, is that really making sense??

The Answer to the above question is NO, it doesn't reset the IIS when you modify web.config file of your application.Editing the web.config in a web application only affects that web application (recycles just that app), so it doesn't require to reset the IIS, it just impact that particular application and reload the app domain. And if you are recycling an app pool that will only affect applications running in that app pool. But Editing the machine.config on the machine will recycle all app pools running.

Another question is when you change/update dlls/drop new dlls in the bin directory of your web application, is that will reset IIS?? Again the answer is NO, it doesn't reset the IIS. IIS monitors the /bin directory of your application. Whenever a change is detected in dlls, it will recycle the app and re-load the new dlls.

Thursday, May 19, 2011

How to copy an image from an URL to own server and resize it


First: Download the image with Url.
Second: Resize the image and save it into disk.

<!   1) For the first requirement, you can use WebClient class. Refer to the following code
  private void Page_Load(object sender, System.EventArgs e)
    {
        WebClient wc = new WebClient();

        byte[] data = wc.DownloadData("http://www.google.cn/intl/en-us/images/logo_cn.gif");

        MemoryStream ms = new MemoryStream(data);

        System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

        GetThumbnailImage(img);//Resize Image
    }
   
->   2) For the second requirement refer to the code below:
private void GetThumbnailImage(System.Drawing.Image img)
    {
        float iScale = img.Height > img.Width ? (float)img.Height / 100 : (float)img.Width / 100;

        img = img.GetThumbnailImage((int)(img.Width / iScale), (int)(img.Height / iScale), null, IntPtr.Zero);

        MemoryStream memStream = new MemoryStream();

        img.Save(Server.MapPath("att.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);

        memStream.Flush();
    }