Tuesday 30 June 2015

Multi-Threading in asp.net c# .

C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.

Each thread defines a unique flow of control. If your application involves complicated and time consuming operations, then it is often helpful to set different execution paths or threads, with each thread performing a particular job.

Threads are lightweight processes. One common example of use of thread is implementation of concurrent programming by modern operating systems. Use of threads saves wastage of CPU cycle and increase efficiency of an application.

So far we wrote the programs where a single thread runs as a single process which is the running instance of the application. However, this way the application can perform one job at a time. To make it execute more than one task at a time, it could be divided into smaller threads.

How Threading Works

Multithreading is managed internally by a thread scheduler, a function the CLR typically delegates to the operating system. A thread scheduler ensures all active threads are allocated appropriate execution time, and that threads that are waiting or blocked (for instance, on an exclusive lock or on user input)  do not consume CPU time.

On a single-processor computer, a thread scheduler performs time-slicing — rapidly switching execution between each of the active threads. Under Windows, a time-slice is typically in the tens-of-milliseconds region — much larger than the CPU overhead in actually switching context between one thread and another (which is typically in the few-microseconds region).

On a multi-processor computer, multithreading is implemented with a mixture of time-slicing and genuine concurrency, where different threads run code simultaneously on different CPUs. It’s almost certain there will still be some time-slicing, because of the operating system’s need to service its own threads — as well as those of other applications.

A thread is said to be preempted when its execution is interrupted due to an external factor such as time-slicing. In most situations, a thread has no control over when and where it’s preempted.


Thread Life Cycle Diagram:-


Threads vs Processes

A thread is analogous to the operating system process in which your application runs. Just as processes run in parallel on a computer, threads run in parallel within a single process. Processes are fully isolated from each other; threads have just a limited degree of isolation. In particular, threads share (heap) memory with other threads running in the same application. This, in part, is why threading is useful: one thread can fetch data in the background, for instance, while another thread can display the data as it arrives.

Threading’s Advantage and Disadvantage

Multithreading has many uses; here are the most common:

1. Maintaining a responsive user interface:-
By running time-consuming tasks on a parallel “worker” thread, the main UI thread is free to continue processing keyboard and mouse events.

2. Making efficient use of an otherwise blocked CPU:-
Multithreading is useful when a thread is awaiting a response from another computer or piece of hardware. While one thread is blocked while performing the task, other threads can take advantage of the otherwise unburdened computer.

3. Parallel programming:-
Code that performs intensive calculations can execute faster on multicore or multiprocessor computers if the workload is shared among multiple threads in a “divide-and-conquer” strategy (see Part 5).

4. Speculative execution:-
On multicore machines, you can sometimes improve performance by predicting something that might need to be done, and then doing it ahead of time. LINQPad uses this technique to speed up the creation of new queries. A variation is to run a number of different algorithms in parallel that all solve the same task. Whichever one finishes first “wins” — this is effective when you can’t know ahead of time which algorithm will execute fastest.

5. Allowing requests to be processed simultaneously:-
On a server, client requests can arrive concurrently and so need to be handled in parallel (the .NET Framework creates threads for this automatically if you use ASP.NET, WCF, Web Services, or Remoting). This can also be useful on a client (e.g., handling peer-to-peer networking — or even multiple requests from the user).
With technologies such as ASP.NET and WCF, you may be unaware that multithreading is even taking place — unless you access shared data (perhaps via static fields) without appropriate locking, running afoul of thread safety.

Threads also come with strings attached. The biggest is that multithreading can increase complexity. Having lots of threads does not in and of itself create much complexity; it’s the interaction between threads (typically via shared data) that does. This applies whether or not the interaction is intentional, and can cause long development cycles and an ongoing susceptibility to intermittent and nonreproducible bugs. For this reason, it pays to keep interaction to a minimum, and to stick to simple and proven designs wherever possible. This article focuses largely on dealing with just these complexities; remove the interaction and there’s much less to say!


Threading also incurs a resource and CPU cost in scheduling and switching threads (when there are more active threads than CPU cores) — and there’s also a creation/tear-down cost. Multithreading will not always speed up your application — it can even slow it down if used excessively or inappropriately. For example, when heavy disk I/O is involved, it can be faster to have a couple of worker threads run tasks in sequence than to have 10 threads executing at once. (In Signaling with Wait and Pulse, we describe how to implement a producer/consumer queue, which provides just this functionality.)

Using Code:-


Note:- Use the below Namespace for Threading
      using System.Threading;

Main Thread example :-
namespace DempApp
{
        class MainThreadExample
        {
            static void Main(string[] args)
            {
                Thread th = Thread.CurrentThread;
                th.Name = "MainThread";
                Console.WriteLine("This is {0}", th.Name);
                Console.ReadKey();
            }
        }
}


Output:-










Sub Thread example:-
namespace DempApp
{
        class MainThreadExample
        {
            static void Main(string[] args)
            {
                // create thread start delegate
                // contains the method name to execute by the thread
                ThreadStart ts = new ThreadStart(executeBySubThread);
                // create new thread
                Thread th = new Thread(ts);
                //Call start thread Method
                th.Start();

                // Let sleep first main thread for 1000 NS
                Thread.Sleep(1000);

                for (int i = 10; i > 0; i--)
                {
                    Console.WriteLine("Main Thread Itreation is :" + i);
                    Thread.Sleep(1000);
                }

                Console.WriteLine("Good Bye!!!I'm main Thread");
                Console.ReadLine();

            }

            // this method executed by a separate thread
            static void executeBySubThread()
            {
                for (int j = 0; j < 10; j++)
                {
                    Console.WriteLine("Sub Thread Itreation is : " + j);
                    Thread.Sleep(1000);
                }
                Console.WriteLine("Good Bye!!!I'm Sub Thread");
            }
        }
}

Output:-








































In the abobe post I expalined the Threading concept and I hope this post is helpful to you. Please comments your feedback and questions.

Tuesday 9 June 2015

How to get my public, External or ISP IP address in c# .net ?

Here in this post I wrote c# example to get  public, external and ISP IP address.

Nameapce Required :-

using System.Net;

using System.Text.RegularExpressions;


Your request will be going to below site addres and it will return your IP addres. You must be connected with internet.

http://checkip.dyndns.org


Code Example:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;

namespace DempApp
{
  class GetIpAddress
   {
     static void Main(string[] args)
     {
       try
        {
         string externalIP;
         Console.WriteLine("\nPlease wait...");
         externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
         externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(externalIP)[0].ToString();
         Console.WriteLine("\n Your public IP address:- {0}", externalIP);
          Console.ReadKey();
        }
       catch
        {
          Console.WriteLine("please try Again!");
          Console.ReadKey();
         }

       }
    }

}


Output:-













This very frequently  used code in to track visitor's IP addres.I hope you enjoyed this post. Please comments your feedback and questions.

Sunday 7 June 2015

How to Spilt string sql server ?

In this post I wrote a SQL function to split string in sql server. In most of cases we need to split sql string.


Creating sql function:-

CREATE FUNCTION [dbo].[fnSplitSqlString]
(
    @string nvarchar(MAX),
    @delimiter char(1)
)

 RETURNS @output table
  (
   SplitData nvarchar(MAX)
  )

BEGIN
    DECLARE @start int, @end int
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
    WHILE @start < LEN(@string) + 1 BEGIN
        IF @end =
            SET @end = LEN(@string) + 1
      
        INSERT INTO @output (splitdata) 
        VALUES(SUBSTRING(@string, @start, @end - @start))
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
       
    END
    RETURN
END

Calling above function :-


Select * from [dbo].fnSplitSqlString('SQL$ASP.NET$WCF$JQuery$c#.NET','$')

Output:-



















I hope you enjoyed this post. Please comments your feedback and questions.

Thursday 4 June 2015

What are the WCF specifications ?

In this post I explained what are the specifications does WCF  follow. You can learn more about WCF in my other post by clicking on WCF menu tab.

Web Service standards and specifications are known as "WS-*". These represents a set of protocols and specifications. These are used to extend the capability of web service.

The Web Services Interoperability Organization (WS-I) is an industry group to promote interoperability across the stack of web services specifications. It publishes web service profiles, sample applications, and test tools for help. One of the popular profiles it has published is the WS-I Basic Profile. WS-I is governed by a Board of Directors, and Microsoft is one of the board members.

WCF supports specifications defined by WS-* specifications. WS-* specifications are defined together by Microsoft, IBM, SUN, and many other big companies so that they can expose there services through a common protocol. WCF supports all specifications defined and now we will learn them one by one.

1. Messaging (WS-Addressing): -

SOAP is the fundamental protocol for web services. WS Addressing defines some extra additions to SOAP headers, which makes SOAP free from the underlying transport protocol. One of the good things about message transmission is MTOM, also termed as Message Transmission Optimization Mechanism. They optimize the transmission format for SOAP messages in XML-Binary format using XML optimized packaging (XOP). Because data will sent in binary and optimized format, it will give us a huge performance gain.

2. Security (WS-Security, WS-Trust, and WS-Secure Conversation):-

 All the three

  1. WS-Security, 
  2. WS-Trust, and
  3. WS-Secure Conversation

WS- define authentication, security, data integrity, and privacy features for a service.

3. Reliability (WS-Reliable Messaging): -

Reliability specification ensures end-to-end communication when we want SOAP messages to be transmitted over the network back and forth many times.

4. Transactions (WS-Coordination and WS-Atomic Transaction):

 These two specifications WS-Coordination and WS-Atomic Transaction enable transaction with SOAP messages.

5. Metadata (WS-Policy and WS-Metadata Exchange): -

WSDL is an implementation of the WS-Metadata Exchange protocol. WS-Policy defines more dynamic features of a service, which cannot be expressed by WSDL. We have stressed on the WS-* specification as it is a specification which a service has to follow to be compatible with other languages. Because WCF follows WS-* specifications, other languages like Java, C++ can also exploit features like Messaging, Security, Reliability, and transactions written in C# or VB.NET. This is the biggest achievement of WCF to integrate the above features with other languages.



These are the specificationd followed by WCF. I hope its very useful and you enjoyed this post. Please comments your feedback and questions.