Posts

Showing posts from November, 2015

Asynchronous programming with delegates and CallBack

Asynchronous programming with delegate and CallBack   using System; using System.Runtime.Remoting.Messaging; using System.Threading; namespace Memory_understanding {     public delegate int myfundelegate(int num);     class myMethod     {         public int myfun(int a)         {             Thread.Sleep(10000);             return a*a;         }     }     class program     {         public static void Main()         {             myMethod a = new myMethod();                         myfundelegate d = new myfundelegate(a.myfun);             IAsyncResult async = d.BeginInvoke(25, new AsyncCallback(mycallBackAsyn), "This from Main Thread");             Console.WriteLine("Processing the operations");             Console.ReadLine();         }         private static void mycallBackAsyn(IAsyncResult ar)         {             AsyncResult a = (AsyncResult)ar;             myfundelegate del = (myfundelegate)a.AsyncDelegate;             int result = del.EndInvoke(ar);       

Implementing IConvertible Interface

using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Memory_understanding {     /// <summary>     /// understanding the reflection property.     /// </summary>     class person : IConvertible     {         private int roll_number;         private string name;         private double marks;         public int Roll_number { get; set; }         public string Name { get; set; }         public double Marks { get; set; }         public void display()         {             Console.WriteLine("Student : " + Name + " Roll No " + Roll_number);         }         public void display2()         {             Console.WriteLine("This isto understand the reflection property");         }         public TypeCode GetTypeCode()         {             throw new NotImplementedException();         }         public bool ToBoolean(IFormatProvider provider) { throw new NotImp

Under Standing Reflections

Understading reflection property Reflection is the ability to find the information about types contained in an assembly at runtime. Reflection is the ability to find out information about objects, the application details (assemblies), its metadata at run-time. using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Memory_understanding {     /// <summary>     /// understanding the reflection property.     /// </summary>     class person     {         private int roll_number;         private string name;         public int Roll_number { get; set; }         public string Name { get; set; }         public void display()         {             Console.WriteLine("Student : " + Name + " Roll No " + Roll_number);         }         public void display2()         {             Console.WriteLine("This isto understand the reflection property");         }    

Static constructors

 The Static constructor runs nly for the first tym when the class is instantiated .So we can see the value of the count_static is not increasing ,but the value of count_normal is incremented when more and more instance of class is created .A static constructor does not take access modifiers or have parameters and can't access any non-static data member of a class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Memory_understanding {     class StaticConstructor     {         static int a;         static int count_static;         static int count_normal;         private int b;         static StaticConstructor()         {             a = 12;             count_static++;         }         public StaticConstructor(int c)         {             b = c;             count_normal++;         }         public void display()         {             Console.WriteLine(b);             Console.WriteLine(a);            

Generic Delegate Action Func and Predicate

Generic Delegate Action Func and Predicate using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Memory_understanding {  //Generic Delegate with Action, Func and Predicate     class genericdelegate     {                 static string getmessage(string hello)         {             return hello+"This is func delegate";         }         static void getmessageAction(string hello)         {             Console.WriteLine("This is Action delegate"+hello);         }         static bool getmessagePredicate(string hello)         {             if (hello == "Hello Predicate")                 return true;             else                 return false;         }         public static void Main()         {             //Func delegate will return a value and the last argument in the argument list is the return type                         Func<string, string> fun = new Func<string, str

Multicast Delegate

Multicast Delegate ///-------------------understading Delegate //mutlicastDelegate -----------------------///     class multicastdelegate     {         delegate string function1(string display);         static function1 fun = getMessage;         static string getMessage(string display)         {             return display + " this is Multicast delegate from getMessage";         }         static string getMessage2(string display)         {             return display + " this is Multicast delegate from getMessage2";         }         static string getMessage3(string display)         {             return display + " this is Multicast delegate from getMessage3";         }         public static void Main()         {             string t = fun.Invoke("Hello");             Console.WriteLine(t);             //1st way of muticasting a delegate of 2nd form             fun += getMessage2;             string t2 = fun.Invoke("Hello");            

Delegate SingleCast

SingleCast Delegate using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Memory_understanding { //    ---------------understanding Delegates /Multicast/SingleCast/Generic ---------------/// //    singlecast delegate     class singlecasedelegate     {         delegate string function1(string display);         static function1 fun = displaymyfunction;         static string displaymyfunction(string display)         {             return display+" this is single delegate";         }         public static void Main()         {             //invokation of single delegate             string t=fun.Invoke("Hello");             Console.WriteLine(t);             //2nd way of invocation             function1 fun1 = new function1(displaymyfunction);             string t1 = fun1("Hello ");             Console.WriteLine(t1);             Console.ReadLine();         }     }

Asyn and Await to write to a File .

Using Async and Await (Asynchronous programming to ) write to files in a synchronous manner one .by creating 4 child threads (Task) existing from the threadpool.  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.IO; namespace CLRprof {     class asyncawait     {         public Task Longrunningprocess(string filename,string text)         {             return Task.Run(() =>             {                 Monitor.Enter(this);                 using (StreamWriter writer = new StreamWriter(filename, true))                 {                     writer.Write(text);                     writer.Close();                     Monitor.Exit(this);                     Thread.Sleep(1000);                     Console.WriteLine("Finished the Longrunning process");                 }             });         }         public async void CallProcess()         {             await Longrunningpro

Threading with Synchronous Join operation

Threading to write to a file using three thread with Monitor as locking for the critical section and Thread.Join to make the entire process synchronous (though this kills the purpose of threading ) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.IO; namespace Memory_understanding {     class writer     {         //private static Mutex mutex = new Mutex();         public void writer_method(string inputline,string thread_name)         {             Monitor.Enter(this);             using (StreamWriter str = new StreamWriter("sharedfile.txt",true))             {                                 str.WriteLine(inputline);                 str.Close();                 Console.WriteLine("Finished :" + thread_name);                 Thread.Sleep(10000);                 Monitor.Exit(this);             }         }     }     class ThreadWriter     {         public static

Multithreading with Monitor for thread Synchronization and writing to a file

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.IO; namespace Memory_understanding {     class writer     {         //private static Mutex mutex = new Mutex();         public void writer_method(string inputline)         {             Monitor.Enter(this);             using (StreamWriter str = new StreamWriter("sharedfile.txt",true))             {                                 str.WriteLine(inputline);                 str.Close();                 Console.WriteLine("Finished");                 Thread.Sleep(8000);                 Monitor.Exit(this);             }         }     }     class ThreadWriter     {         public static void Main()         {             writer wri = new writer();             Thread t1 = new Thread(() => wri.writer_method("I will be writing from Thread1"));             t1.Start();             Thread t2 = new Thread(() => wr

POC on loading an Excel with multiple sheets to a DatagridView and saving the excel to a SQLserver DB Table

C# WinForm: POC on loading an Excel with multiple sheets to a DatagridView and saving the excel to a SQLserver DB Table Requirement: To Load an Excel to a DataGridView and make the number of sheets available to a drop down list. Depending upon the selection of the item from the dropDown list, the excel sheet will be displayed in the DataGridView .Then Upload the excel to the database. C# Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; namespace PracticeWindowsForm {     public partial class editable_Grid_iew : Form     {         private string ExcelConnString_03 = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Extended Properties='Excel 8.0;HRD={1}'";         private string ExcelConnString_07 = &q

Understanding CallBack function on Thread.

Understading Callback method wher a function is executed not on the main thread but on a child thread that it created .  Similary there are different ways of executing function created on the child thread. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Memory_understanding {     public delegate void sumofnumberscallback(int sumofnumber);     class A     {         int target;         public A(int num)         {             this.target= num;         }         public void printNumber(int n)         {                         for (int i = 0; i < n; i++)             {                 Thread.Sleep(1000);                 Console.WriteLine(i);             }         }         public void printNumber_2()         {             for (int i = 0; i < target; i++)             {                 Thread.Sleep(1000);                 Console.WriteLine(i);             }         }     }     clas

POC on gridView with Button element as Column value

To develop a code for the below requirement: 1)Create a GridView to display Employee table (first_name ,Last_name, Id) 2)For each row in the grid ,place a button "Exclude"inside the grid 3)On click of  the button "Exclude" put include the Emplyee name in a excluded list. 4)Write a function which can exclude and include the name of the employees from the Grid. 5)On submit button click show the number of Employess included and excluded. ASPX Code <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee_Example.aspx.cs" Inherits="WebApplication1.Employee_Example" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title> </head> <body>     <form id="form1" runat="server">     <div>      <asp:GridView ID="employeeGrid" runat="server" CellPadding