Wednesday, October 9, 2019

what is LOCK and Monitor

Lock:


When working with a multithreading application it is very important for developers to handle multiple threads for a critical section of code.

Monitor and lock is the way to provide thread safety in a multithreaded application in C#. Both provide a mechanism to ensure that only one thread is executing code at the same time to avoid any functional breaking of code.



C# Lock keyword ensures that one thread is executing a piece of code at one time. The lock keyword ensures that one thread does not enter a critical section of code while another thread is in that critical section.

Lock is a keyword shortcut for acquiring a lock for the piece of code for only one thread.


Monitor


Monitor provides a mechanism that synchronizes access to objects. It can be done by acquiring a significant lock so that only one thread can enter in a given piece of code at one time. Monitor is no different from lock but the monitor class provides more control over the synchronization of various threads trying to access the same lock of code.

Using a monitor it can be ensured that no other thread is allowed to access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object.

The Monitor class has the following methods for the synchronize access to a region of code by taking and releasing a lock,
  • Monitor.Enter 
  • Monitor.TryEnter
  • Monitor.Exit.
Monitor locks objects (that is, reference types), not value types. While you can pass a value type to Enter and Exit, it is boxed separately for each call.

Thread

What is threading.


To define Threading in a one line, means parallel work or code execution. To perform any multiple task simultaneously means Threading.

For example executing Microsoft PPTX and Excel simultaneously in a desktop, laptop or any device is known as Threading.

Work or a code task is always been executed in a two ways i.e. Synchronous or Asynchronous way.


Synchronous way means where work multiple jobs are executed one after the other. Here Work 2 have to wait till Work 1 is completed same way the others.

Asynchronous:

Asynchronous means multiple work has been executed simultaneously like doing multitask at a same time.



For creating threading application there some important methods which used regularly for implementing threading.

1 : Thread Join

2 : Thread Sleep

3 : Thread Abort


Thread Join:


Thread.Join() make thread to finish its work or makes other thread to halt until it finishes work. Join method when attached to any thread, it makes that thread to execute first and halts other threads. Now on the same we will see a simple example where we apply Join method to thread.


Use of Thread Sleep:


Thread.Sleep a method used to suspend current thread for a specific interval of time. Time can be specified in milliseconds or Timespan. While in a Sleep mode a method does not consumes any CPU resources so indirectly it save memory for other thread processes.

On the same we will create a simple example where in the for loop while printing output we will make a thread to sleep for 4000 milliseconds i.e. 4 secs for per print and total we are going to print 6 outputs. To count it properly we have used .NET Diagnostics namespace which allows us to use Stopwatch and TimeSpan to count elapsedTime.



Use of Thread Abort

As name implies "Abort" so same way Thread.Abort helps to end or abort any thread to process it further. It raises ThreadAbortException in the thread for process of termination.


Types of Threads in C#

here are two types of Thread in Csharp i.e.
 1. Foreground Thread 
 2. Background Thread


Foreground Thread

As we know that Main method is also runs on single thread, So in a Main method when we attach any other method to a thread it means we are making a multithread application. Foreground threads are those threads which keeps running until it finishes his work even if the Main method thread quits its process. To make you understand more better let me show you. Lifespan of foreground threads does not depends on main thread.



Background Thread


Background thread is just opposite of foreground thread here background thread quits its job when main thread quits. Here lifespan of background threads depends on main thread. In order to implement background thread in a program we need to set property called IsBackground to true.





A property of type System.Threading.ThreadState, used to get the value containing the state of the thread.

Start()

Starts the execution of the thread.

Abort() 

Allows the current thread to stop the execution of the thread permanently.

Suspend() 

Pauses the execution of the thread temporarily.

Resume() 

Resumes the execution of a suspended thread.

Join() 


Make the current thread wait for another thread to finish.








Monday, October 7, 2019

server.transfer and response.redirect


Server.Transfer                                    Response.Redirect

Redirection Redirection is done by the server. Redirection is done by the browser client.

Browser URL Does not change.                 Changes to the redirected target page.

When to use Redirect between pages of the same server. Redirect between pages on different server and domain.

Response.redirected:

We can bookmark the page because the full address is shown in a browser URL.
An extra round trip happens to the server.
It is used in HTML, ASP and ASP.Net pages to navigate from one page to another.

server.transfer:

We cannot bookmark the page because the full address is not shown in a browser URL.
An extra round trip does not happen to the server; because of this it saves server resources.
It is only used to navigate within an ASP or ASP.Net page, not within HTML pages.


=================================================================================================================

Response.Redirect(URL,true) vsResponse.Redirect(URL,false) ?
Response.Redirect(URL,false): Client is redirected to a new page and the current page on the server will keep processing ahead.

Response.Redirect(URL,true): Client is redirected to a new page but the processing of the current page is aborted.


=====================================================================================================================