Saturday, December 26, 2015

Task VS Thread

Threads run in a shared memory space


Processes run in separate memory spaces. each process has its own memory space and at least 1 thread, know as primary thread.

Thread
  1. Able observe its state. (specify name for debugging, Show in UI)
  2. Able to set thread-level properties (stack size, apartment state, or culture)
  3. Maintain an object owned by a single thread. ( Only used by that thread)
  4. Problem : costly, Each thread consumes a non-trivial amount of memory for its stack

ThreadPool
  1. Is a wrapper around a pool of threads, no create its own OS thread.
  2. Use for Short Operations & no result return.
  3. Use to avoid overhead of creating too many threads. 
  4. Able to execute at some point.
  5. Able control the size of the pool, 
  6. Not able to tell the pool start running the work.
  7. It will full , if too many long-running tasks.
  8. Not able to get the result (which item has been completed.)
Task
  1. Is lightweight object for managing a parallelizable unit of work. (work something in Parallel).Parallel means the work is spread across multiple processors to maximize computational speed.
  2. Does not create its own OS thread.(Executed by a TaskScheduler, run on threadPool)
  3. Unlike the ThreadPool, Task able to find out when it finishes, to return a result.
  4. Call ContinueWith() to run more code once the task finishes, and pass you the task's result.
  5. Call Wait() to synchronously wait for a task to finish. Like Thread.Join(), this will block the calling thread until the task finishes. Synchronously waiting for a task is bad idea; it prevents the calling thread from doing any other work, and lead to deadlocks if the task ends up waiting (even asynchronously) for the current thread.
  6.  Parallel.For*() methods, PLINQ, C# 5 await, and modern async methods in the BCL, are all built on Task.
Conclusion
Task is almost always the best option; it provides a much more powerful API and avoids wasting OS threads.

Sunday, December 20, 2015

Task vs parallel

Parallel is faster than Task.

Task
task = new Task(() => DoSomething());
task.Start();
task.Wait();

------------------

Action action = new Action(() => SendSMSAsync(DoSomething()));

Parallel.Invoke(action);   

Sunday, December 13, 2015

Run application in cmd

cmd : start "" d:\applicationfolder\application.exe

Write REST Web API

Attribute routing - More Control the Web API URL.

URL : /customers/1/orders
[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }
(1)Enabling Attribute Routing 
(2)Convention-based
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();  (1)

        // Convention-based routing.
        config.Routes.MapHttpRoute(        (2)
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}



Tuesday, December 8, 2015

Web Request

string url = "PenyuApi.aspx?user=" + user + "&pass=" + pasword;

WebRequest request = WebRequest.Create(url);
request.Timeout = 30000; //5 sec call
WebResponse response = request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
string Contents = stream.ReadToEnd();