Friday, October 29, 2010

Threading

Threading enables concurrent processing, you can perform multiple tasks simultaneously.

Multithreading is the ability of operating system to concurrently run programs or tasks in separate threads.
Multithreading requires OS those can perform it, and we written programs in such manner that uses this ability of OS.
It’s a fast computation because it runs multiple tasks simultaneously in different threads.
Basically it’s a game of operating system, tasks not run simultaneously os allots time slots to each thread and runs threads for those allotted time slots, user response is very slow compare to system so user feels like processes running concurrently.
How threading works
Application domains run in processes, and each application domain starts with a single thread and then a thread can call other threads. Operating system divided processor time among threads, it allotted time slots to threads to be executed slots depends on priority, os and thread size.
When a thread switches from running state to suspended os saves context (information) for it, and loaded context of thread to be executed.
Thread pool class – To manage group of threads.
Timer - Enable calling to delegates after a certain amount of time.
Mutex - Mutex class is used to protect simultaneous access of a shared resource. State of Mutex object is set to 'signaled' when it is not owned by any thread or 'nonsignaled' when owned by any thread, so treads can own it one by one.

Pausing and resuming threads
Method Thread.Sleep(miliseconds) immediately suspend a thread for given milliseconds and other thread uses this time slice.
Thread.Sleep(Timeout.Infinite) - Sends to sleep for infinite time.
Thread.Abort() - Aborted the thread.
Thread.Interrupt() - Interrupt the thread.

Overview
1. Threads enables concurrent processing.
2. System.Threading provides classes and interfaces for it, ex to create, start, abort, suspend, synchronization of multiple threads etc.
3. Threads share application resources.

Using System.Threading;

Thread thrd = new Thread(new ThreadStart(obj.Method));
thrd.start();


Friday, October 22, 2010

Asynchronous Calling to a method

We can call a method asynchronously by using beginInvoke and endInvoke methods of delegate,
we pass method's parameters to beginInvoke method then we have got to pass return value of beginInvoke method to endInvoke method, endInvoke method returns actual result(return type of method).
Asynchronous call to a method runs method in a separate thread.
Once you ask the framework to call something asynchronously, it needs a thread to do the work. It can not be the current thread, because that would make the invocation synchronous (blocking). Instead, the runtime queues a request to execute the function on a thread from the .NET Thread Pool. You don’t really need to code anything for it, all of it happens in the background. But, just because it is all transparent doesn’t mean you should care about it. There are a few things to remember:
  • FlushToDisk is executed on a separate thread, a thread that belongs to the .NET Thread Pool.
  • A .NET Thread Pool normally has 25 threads in it (you can change that limit), and each time  FlushToDisk is called, it is going to be executed on one of these threads. You can't control which one.
  • The Thread Pool has its limits! Once all the threads are used, an async method invocation is queued until one of the threads from the pool is freed. This is called Thread Pool Starvation, and normally when it comes to that, performance is compromised.

class DataCache {
public int FlushToDisk(string fileName) {
// simulate a long operation
Thread.Sleep(4000);
return 0;
}
}

class Program {
// define a delegate for the long operation
delegate int CacheFlusher(string fileName);
static void Main(string[] args) {
// create the object and then the delegate
DataCache cache = new DataCache();
CacheFlusher flusher = new CacheFlusher(cache.FlushToDisk);
// invoke the method asynchronously
IAsyncResult result = flusher.BeginInvoke("data.dat", null, null);
// get the result of that asynchronous operation
int retValue = flusher.EndInvoke(result);
Console.WriteLine(retValue);
}
}


Method # 01
// begin execution asynchronously
IAsyncResult result = flusher.BeginInvoke("data.dat", null, null);
// wait for it to complete
while (result.IsCompleted == false) {
// do some work
Thread.Sleep(10);
}
// get the return value
int returnValue = flusher.EndInvoke(result);


Method # 02
// call the delegate asynchronously
IAsyncResult result =
flusher.BeginInvoke("data.dat", null, null);
// wait for the call to finish
result.AsyncWaitHandle.WaitOne();
// get the return value
int returnValue = flusher.EndInvoke(result);


Method # 03
static void Main(string[] args) {
// create the object
DataCache cache = new DataCache();
// create the delegate
CacheFlusher flusher = new CacheFlusher(cache.FlushToDisk);
// call the delegate asynchronously
flusher.BeginInvoke("data.dat", new AsyncCallback(CallbackMethod), flusher);
// wait to exit
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
static void CallbackMethod(IAsyncResult result) {
// get the delegate that was used to call that
// method
CacheFlusher flusher =
(CacheFlusher) result.AsyncState;
// get the return value from that method call
int returnValue = flusher.EndInvoke(result);
Console.WriteLine("The result was " + returnValue);
}

Thursday, October 14, 2010

Sql - Function Vs Stored Procedure

Functions

1. Can be used with Select statement.
2. Not returns output parameter but returns table variables.
3. You can join UDFs.
4. Can not be used to change server configuration.
5. Can not be used with XML FOR clause.
6. Can not have transaction within function.


Stored Procedures

1. Used with EXEC or EXECUTE.
2. Returns output parameter.
3. You can not join SPs.
4. Can be used to change server configuration.
5. Can be used with XML FOR Clause.
6. Can have transaction within SP.

Sunday, October 10, 2010

Using Scenario - Abstract Class Vs Interface

Abstract class

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated.

The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

Interface

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the declaration of methods.

As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.


Using Scenario - Abstract Class Vs Interface

1. If you want to create multiple versions of your component, means need to create some new version in future then use abstract class because you can update abstract class and all classes derived from it updated automatically. While If you need to update interface it's not allowed, you will have to create whole new interface.

2.
If your base functionality will be useful for a wide disparate or unrelated range of classes then create interface, because abstract classes should be created for something closely related classes.

3.
If you have requirement of common, implemented base functionality then create abstract class because abstract class allow implementation also of members while in interface you can not implement any member.
If you need to have some code, data, access modifiers in functionality then use abstract class, in interface all members are public by default.

4.
If you are designing small base functionality then create interface for large base functionality use abstract class.

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...