Posts

Design Patterns

Image
            Singleton Design Pattern Some types have not any reason to instantiate more then once in a project. For example creating a database connection every time take some costs or creation of logger in a project also consume memory of object creation each time. To avoid this we need to make a type as singleton by doing this we can instantiate it only one time and use it anywhere throughout the application or module.                      Sometimes we have a task to read some data from file, so we can create a single instance of object that read data from file and share it through the project to consumers.                      Let's start with creating a singleton class. To achieve this we have created an interface that contains one method of SingletonContainer class.  public interface ISingletonContainer     {          int GetPopulation(string name);     } we will create a class that read the population data from file in a constructor and returns pop

Threading and Task Processing Library in C#

Image
Threading Need of Thread Let us think your computer have only on CPU or a single core processor capable of running one operation at  a time, if CPU has to process a long running task, till the time no other task would run until the current task completes, while this task run all other application will remain unresponsive which forces you to restart the system to fix the issue.              Here thread came into existence. In current windows each application have it's own processes that execute in threads, so if an application crashes only process associated with that application will affect. Context Switching:  Windows manage execution of threads to ensure their work properly, each thread is allowed to execute for a certain period of time, after this period the thread is paused and windows switches to other thread. This is called context switching. However windows has to manage the time between switching, so that it saves the state of current thread to process again in i
Generate PDF from HTML in c# You can use itextSharp and itextSharp.xmlWorker libraries to generate and download PDF from HTML, simply add reference of both the DLLs that are open source.The code is given below:             Byte[] bytes;             var ms = new MemoryStream();             var doc = new Document(PageSize.A4);             var writer = PdfWriter.GetInstance(doc, ms);             doc.Open();             var html = @"<strong>hi</strong>";             var sr = new StringReader(html);             var htmlWorker = new HTMLWorker(doc);             htmlWorker.Parse(sr);             doc.Close();             bytes = ms.ToArray();             HttpResponse response = HttpContext.Current.Response;             response.Clear();             response.AppendHeader("content-disposition",@"attachment;filename=""myfile.pdf""");             response.BinaryWrite(bytes);             response.End(); To know more
Copy to clipboard using Zeroclipboard and jquery The ZeroClipboard library provides an easy way to copy text to the clipboard using an invisible  Adobe Flash  movie and a  JavaScript  interface. The "Zero" signifies that the library is invisible and the user interface is left entirely up to you. This is achieved by automatically floating the invisible movie on top of a   DOM   element of your choice. Standard mouse events are even propagated out to your DOM element, so you can still have rollover and mousedown effects.      The zeroclipboard cdn you can find from  here   The script.      $(document).ready(function () { $("#btnCopy").click(function () {  var clip = new ZeroClipboard($("#btnCopy"), { moviePath: "http://davidwalsh.name/demo/ZeroClipboard.swf" }); clip.setText($("#spn").text());     }); }); The html.        <span id="spn">hi</span>   <input type="button" id="btnCop