Search This Blog

Friday, December 21, 2012

BackgroundWorker VB.NET

VB.NET > BackgroundWorker

Executes an operation on a separate thread.

DoWork event occurs when RunWorkerAsync is called.
RunWorkerCompleted event occurs when the background operation has completed, has been canceled, or has raised an exception.

Example:

Imports System.ComponentModel
Imports System.Threading
Module Module1

  Public Sub backWork()
    Dim worker As New BackgroundWorker()
    AddHandler worker.DoWork, AddressOf DoWork
    AddHandler worker.RunWorkerCompleted, AddressOf bg_RunWorkerCompleted
    worker.RunWorkerAsync()
  End Sub

  Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
    ' some work
    Thread.Sleep(5000)
  End Sub

  Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
    MsgBox("Background worker completed", vbInformation, "Background worker")
  End Sub

End Module





Thursday, December 20, 2012

ArrayList Class C#

C# > Collections > ArrayList

Implements the IList interface using an array whose size is dynamically increased as required.

Example

using System;
using System.Collections;
public class SampleArrayList  {

public static void Main()
{
   ArrayList m_l = new ArrayList();
   m_l.Add("a");
   m_l.Add("b");
   m_l.Add("c");
   m_l.Add(1);
   Console.WriteLine(" Count: {0}", m_l.Count);
   Console.WriteLine(" Capacity: {0}", m_l.Capacity);
   Console.Write( " Elements:" );
   PrintElements(m_l);
  }
  public static void PrintElements(IEnumerable m_l)
 {
    System.Collections.IEnumerator _enum = m_l.GetEnumerator();
    while (_enum.MoveNext())
           Console.Write("\t {0}", _enum.Current);
    Console.ReadLine ();
 }
}






Array C#

C# > Types > Array

An array is a data structure that contains a number of variables called the elements of the array.
C# arrays are zero indexed.
All of the array elements must be of the same type.
Array elements can be of any type, including an array type.

Single-Dimensional Arrays

int[] m_i = new int[3];
string[] m_s = new string[5];
int[] m_i = new int[] { 1, 2, 3 }; // Array Initialization

Multidimensional Arrays

int[,] m_i = new int[3,2]; // two-dimensional array of 3 rows and two columns:
int[,,] m_i_1 = new int [5,1,2]; //  array of three dimensions, 5, 1, 2
int[,] myArray = new  int[,] {{1,2}, {3,4}, {5,6} }; // Array Initialization

Jagged Arrays

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes.

int[][] m_j = new int[2][];

m_j[0] = new int[2];
m_j[1] = new int[3];


Loop all rows DataTable C#

C# > Data > DataTable > Loop all rows

foreach (DataRow row in DataTable1.Rows)
{
     m_v = row["ColName"].ToString();
}




HTTP (Hypertext Transfer Protocol)

HTTP (Hypertext Transfer Protocol) is the set of rules for transferring files (text, graphic images, sound, video, and other multimedia files) on the World Wide Web.
When a user opens their Web browser, the user is indirectly making use of HTTP.
HTTP is an application protocol that runs on top of the TCP/IP protocols.






Tuesday, December 18, 2012

Find locked tables SQL Server

SQL Server > Dynamic Management Views and Functions > sys.dm_tran_locks

Returns information about currently active lock manager resources in SQL Server. Each row represents a currently active request to the lock manager for a lock that has been granted or is waiting to be granted.

Example:

Find locked tables SQL Server

select
   object_name(P.object_id) as TableName,
   resource_type,
   resource_description
from
   sys.dm_tran_locks L
   join sys.partitions P on L.resource_associated_entity_id = p.hobt_id





Static Constructors C#

C# > Class > static constructor

A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Static constructors have the following properties:

  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.

  • A static constructor cannot be called directly.

  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
     public class Log
    {
         // Static constructor:
         static Log()
        {
            System.Console.WriteLine("The static constructor invoked.");
         }
         public static void Write(string text)
        {
           System.Console.WriteLine("The Write method invoked.");
        }
   }
   class Program
   {
       static void Main(string[] args)
      {
          Log.Write("text"); // constructor invoked
          Log.Write("text"); // constructor not invoked.
      }
  }
}