Thursday, July 3, 2014

HTTPClient Class - Asynchronous and Synchronous Usage

HttpClient 

HttpClient is actually available as a NuGet package that you can download today. But a lot of the simplicity of using HttpClient comes from the new language features of C# 5. Combine these two and you got a very simple way of requesting and posting data.


Example for Synchronous -GET:

      using (var client = new HttpClient())
                {
                    var response = client.GetAsync(this.EndPoint + apiParameters).Result;
                    if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Accepted)
                    {
                        //  performing a synchronous call
                        var responseContent = response.Content;
                        // synchronously reading the result
                        string responseString = responseContent.ReadAsStringAsync().Result;
                        return responseString;
                    }
                }


Example for Asynchronous -GET:

public async TaskGetAsync(string uri)
{
    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync(uri);

    //throw an exception if not successful
    response.EnsureSuccessStatusCode();

    string content = await response.Content.ReadAsStringAsync();
    return await Task.Run(() => JsonObject.Parse(content));

}

Wednesday, June 11, 2014

Multithreading 2

Multithreading Implementation
The following section plays with the numerous System.Threading namespace static and instance-level members and properties.
Obtaining Current Thread Information’s
To illustrate the basic use of Thread type, suppose you have console application in which CurrentThread property retrieves aThread object that represents the currently executing thread.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Threading;
 
namespace threading
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("**********Current Thread Informations***************n");
            Thread t = Thread.CurrentThread;
            t.Name = "Primary_Thread";
 
            Console.WriteLine("Thread Name: {0}", t.Name);
            Console.WriteLine("Thread Status: {0}", t.IsAlive);
            Console.WriteLine("Priority: {0}", t.Priority);
            Console.WriteLine("Context ID: {0}", Thread.CurrentContext.ContextID);
            Console.WriteLine("Current application domain: {0}",Thread.GetDomain().FriendlyName);
 
            Console.ReadKey();
        }
 
    }
}
After compiling this application, the output would be as following;
Simple Thread Creation
The following simple example explains the Thread class implementation in which the constructor of Thread class accepts a delegate parameter. After the Thread class object is created, you can start the thread with the Start() method as following;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Threading;
 
namespace threading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t = new Thread(myFun);
            t.Start();
 
            Console.WriteLine("Main thread Running");
            Console.ReadKey();
        }
 
        static void myFun()
        {
            Console.WriteLine("Running other Thread");
        }
    }
}
After running the application, you got the following output of the two threads as:
The important point to be noted here is that, there is no guarantee what output come first meaning, which thread start first. Threads are scheduled by the operating system. So which thread comes first can be different each time.
Background Thread
The process of the application keeps running as long as at least one foreground thread is running. If more than one foreground thread is running and the Main() method ends, the process of the application keeps active until all foreground threads finish their work.
When you create a thread with the Thread class, you can define if it should be a foreground or background thread by setting the property IsBackground. The Main() method set this property of the thread t to false. After setting the new thread, the main thread just writes to the console an end message. The new thread writes a start and an end message, and in between it sleep for two seconds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
using System.Threading;
 
namespace threading
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t = new Thread(myFun);
            t.Name = "Thread1";
            t.IsBackground = false;
            t.Start();
            Console.WriteLine("Main thread Running");
            Console.ReadKey();
        }
 
        static void myFun()
        {
            Console.WriteLine("Thread {0} started", Thread.CurrentThread.Name);
            Thread.Sleep(2000);
            Console.WriteLine("Thread {0} completed", Thread.CurrentThread.Name);
        }
    }
}
When you compile this application, you will still see the completion message written to the console because the new thread is a foreground thread. Here, the output as following;
If you change the IsBackground property to start the new thread to true, the result shown at the console is different as follows:
Concurrency issues
Programming with multiple threads is not an easy task. When starting multiple threads that access the same data, you can get intermediate problems that are hard to resolve. When you build multithreaded applications, you program needs to ensure that any piece of shared data is protected against the possibility of numerous threads changing its value.
Race Condition
A race condition can occurs if two or more threads access the same objects and access to the shared state is not synchronized. To illustrate the problem of Race condition, let’s build a console application. This application uses the Test class to print 10 numbers by pause the current thread for a random number of times.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Using System;
using System.Threading;
 
namespace threading
{
    public class Test
    {
        public void Calculation()
        {
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(new Random().Next(5));
                Console.Write(" {0},", i);
            }
            Console.WriteLine();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Test t = new Test();
            Thread[] tr = new Thread[5];
 
            for (int i = 0; i < 5; i++)
            {
                tr[i] = new Thread(new ThreadStart(t.Calculation));
                tr[i].Name = String.Format("Working Thread: {0}", i);
            }
 
            //Start each thread
            foreach (Thread x in tr)
            {
                x.Start();
            }
            Console.ReadKey();
        }
    }
}
After compiling this program, the primary thread within this application domain begins by producing five secondary threads. Each working threads told to call the Calculate method on the same Test class instance. So you have taken none of precaution to lock down this object’s shared resources. Hence, all of five threads start to access the Calculation method simultaneously. This is the Race Condition and the application produce unpredictable output as following;
Deadlocks
Having too much locking into an application can get your application into trouble. In a deadlock, at least two threads wait for each other to release a lock. As both threads wait for each other, a deadlock situation occurs and thread wait endlessly and your computer eventually hanged.
Here, the both of methods changed the state of the two objects obj1 and obj2 by locking them. The methods DeadLock1() first lock obj1 and next for obj2. The method DeadLock2() first lock obj2 and then obj1.So lock for obj1 is resolved next thread switch occurs and second method start to run and gets the lock for obj2. The second thread now waits for the lock of obj1. Both of threads now wait and don’t release each other. This is typically deadlock.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Threading;
 
namespace threading
{
    class Program
    {
        static object obj1 = new object();
        static object obj2 = new object();
 
        public static void DeadLock1()
        {
            lock (obj1)
            {
                Console.WriteLine("Thread 1 got locked");
                Thread.Sleep(500);
                lock (obj2)
                {
                    Console.WriteLine("Thread 2 got locked");
                }
            }
        }
 
        public static void DeadLock2()
        {
            lock (obj2)
            {
                Console.WriteLine("Thread 2 got locked");
                Thread.Sleep(500);
                lock (obj1)
                {
                    Console.WriteLine("Thread 1 got locked");
                }
            }
        }
 
        static void Main(string[] args)
        {
            Thread t1 = new Thread(new ThreadStart(DeadLock1));
            Thread t2 = new Thread(new ThreadStart(DeadLock2));
 
            t1.Start();
            t2.Start();
 
           Console.ReadKey();
        }
 
    }
}

by http://resources.infosecinstitute.com/multithreading/