Saturday 29 July 2017

Multithreading in c#



Multithreading
A Thread is defined as the execution path of a program. 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.
Following are the various states in the life cycle of a thread:
·        The Unstarted State: It is the situation when the instance of the thread is created but the Start method is not called.
·        The Ready State: It is the situation when the thread is ready to run and waiting CPU cycle.
·        The Not Runnable State: A thread is not executable, when:
    • Sleep method has been called
    • Wait method has been called
    • Blocked by I/O operations
·        The Dead State: It is the situation when the thread completes execution or is aborted.
·        The following program demonstrates main thread execution:
using System;
using System.Threading;

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

Removing Browser Cache using this code



Removing Browser Cache using this code


We can also achieve this by disabling browser caching in code behind write the following lines of code in Page_Init event or Page_Load event and don’t forgot to add namespace 
using System.Web; becauseHttpCacheability related to that namespace.


protected void Page_Init(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();

}

We need to place this code in a page wherever we need to disable browser back button. If we use above caching method to disable browser back button that will display webpage expired message like this.