Monday, November 8, 2010

Keeping The WinForm UI responsive while doing background processing using delegates.

This can be achieved with Threading in windows forms
1) Never invoke any method or property on a control created on another thread other than Invoke, BeginInvoke, EndInvoke or CreateGraphics, and InvokeRequired.
Each control is effectively bound to a thread which runs its message pump. If you try to access or change anything in the UI (for example changing the Text property) from a different thread, you run a risk of your program hanging or misbehaving in other ways. You may get away with it in some cases, but only by blind luck. Fortunately, the Invoke, BeginInvoke and EndInvoke methods have been provided so that you can ask the UI thread to call a method for you in a safe manner.
2) Never execute a long-running piece of code in the UI thread.
If your code is running in the UI thread, that means no other code is running in that thread. That means you won't receive events, your controls won't be repainted, etc. This is a very Bad Thing.

So, if you have a piece of long-running code which you need to execute, you need to create a new thread to execute it on, and make sure it doesn't directly try to update the UI with its results. The interesting bit is - invoking a method on the UI thread in order to update the UI.

Each control is effectively bound to a thread which runs its message pump. If you try to access or change anything in the UI (for example changing the Text property) from a different thread, you run a risk of your program hanging or misbehaving in other ways. You may get away with it in some cases, but only by blind luck. Fortunately, the Invoke, BeginInvoke and EndInvoke methods have been provided so that you can ask the UI thread to call a method for you in a safe manner.
There are two different ways of invoking a method on the UI thread, one synchronous (Invoke) and one asynchronous (BeginInvoke). They work in much the same way - you specify a delegate and (optionally) some arguments, and a message goes on the queue for the UI thread to process. If you use Invoke, the current thread will block until the delegate has been executed. If you use BeginInvoke, the call will return immediately. If you need to get the return value of a delegate invoked asynchronously, you can use EndInvoke with the IAsyncResult returned by BeginInvoke to wait until the delegate has completed and fetch the return value.
MethodInvoker is just a delegate which takes no parameters and returns no value (like ThreadStart),


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace DelegateUIResponsive
{
public class ClsDelegateUIResponsive : System.Windows.Forms.Form
{
delegate void UpdateStatusDelegate (string Msg);
private System.Windows.Forms.Label lblStatus; //Displays status on the window.

private System.ComponentModel.Container components = null;

private ClsDelegateUIResponsive()
{
InitializeComponent();
MethodInvoker mi = new MethodInvoker(StartThread); // New thread.
mi.BeginInvoke(null, null);
}

[STAThread]
static void Main()
{
Application.Run(new ClsDelegateUIResponsive());
}

private void StartThread()
{
for (int i = 0; i < 10; i++)
{
ShowProgress("The counter is : " + i.ToString());
Thread.Sleep(1000);
}
}

private void ShowProgress(string Msg)
{
// We're not in the UI thread, so we need to call BeginInvoke
if (InvokeRequired)
{
BeginInvoke(new UpdateStatusDelegate(ShowProgress), new Object[]{ Msg });
return;
}
this.lblStatus.Text = Msg;
}
}
}

Tuesday, October 12, 2010

Sorting Algorithms Compared

Time
Sort Average Best Worst Space Stability Remarks
Bubble
sort
O(n^2) O(n^2) O(n^2) Constant Stable Always use a modified bubble sort
Modified
Bubble sort
O(n^2) O(n) O(n^2) Constant Stable Stops after reaching a sorted array
Selection
Sort
O(n^2) O(n^2) O(n^2) Constant Stable Even a perfectly sorted input requires scanning the entire array
Insertion
Sort
O(n^2) O(n) O(n^2) Constant Stable In the best case (already sorted), every insert requires constant time
Heap
Sort
O(n*log(n)) O(n*log(n)) O(n*log(n)) Constant Instable By using input array as storage for the heap, it is possible to
achieve constant space
Merge
Sort
O(n*log(n)) O(n*log(n)) O(n*log(n)) Depends Stable On arrays, merge sort requires O(n) space; on linked lists, merge
sort requires constant space
Quicksort O(n*log(n)) O(n*log(n)) O(n^2) Constant Stable Randomly picking a pivot value (or shuffling the array prior to
sorting) can help avoid worst case scenarios such as a perfectly
sorted array.

Monday, October 11, 2010

GridView, Columns, BoundField, TemplateField, ItemTemplate

GridView class displays the values of a data source in a table where each column represents a field and each row represents a record. The GridView control enables you to select, sort, and edit these items.
When the AutoGenerateEditButton property is set to true, a column (represented by a CommandField object) with an Edit button for each data row is automatically added to the GridView control. Clicking an Edit button for a row puts that row in edit mode. When a row is in edit mode, each column field in the row that is not read-only displays the appropriate input control, such as a TextBox control, for the field's data type. This allows the user to modify the field's value.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="SalesOrderID,SalesDetailID" DataSourceID="DataSource1">

BoundField: Displays the value of a field in a data source. This is the default column type of the GridView control. To specify the field to display in a BoundField object, set the DataField property to the field's name.
Use the DataFormatString property to specify a custom display format for the values that are displayed in the BoundField object. If the DataFormatString property is not set, the field's value is displayed without any special formatting.


<columns>
 <asp:boundfield datafield="CustomerID" readonly="true" headertext="CustID"/>
 <asp:boundfield datafield="CompanyName" convertemptystringtonull="true"
   headertext="Customer Name"/>
<asp:BoundField DataField="ListPrice" HeaderText="ListPrice" 
   SortExpression="ListPrice" DataFormatString="{0:C}" />
</columns>


TemplateField: Displays user-defined content for each item in the GridView control according to a specified template. This column field type enables you to create a custom column field.



<Columns>   
<asp:TemplateField>
     <ItemTemplate>
            <img src='<%# Eval("pic_square") %>' alt="" />
     </ItemTemplate>
</asp:TemplateField>
       <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
                 <%# Eval("name") %>
            </ItemTemplate>
      </asp:TemplateField>
<asp:TemplateField>
     <ItemTemplate>
         <asp:LinkButton ID="lnbSend" runat="server" CommandArgument='<%# Eval("uid") %>' OnCommand="lnbSend_Command">Send Message</asp:LinkButton>
     </ItemTemplate>
</asp:TemplateField>
</Columns>

Why use TemplateField instead of BoundField?

The GridView is composed of a set of fields that indicate what properties from the DataSource are to be included in the rendered output along with how the data will be displayed. The simplest field type is the BoundField, which displays a data value as text. Other field types display the data using alternate HTML elements. The CheckBoxField, for example, renders as a check box whose checked state depends on the value of a specified data field; the ImageField renders an image whose image source is based upon a specified data field. Hyperlinks and buttons whose state depends on an underlying data-field value can be rendered using the HyperLinkField and ButtonField field types, respectively.
While the CheckBoxField, ImageField, HyperLinkField, and ButtonField field types allow for an alternate view of the data, they still are fairly limited with respect to formatting. A CheckBoxField can only display a single check box, whereas an ImageField can display only a single image. What if a particular field must display some text, a check box, and an image, all based upon different data-field values? Or what if we wanted to display the data using a Web control other than the CheckBox, Image, HyperLink, or Button? Furthermore, the BoundField limits its display to a single data field. What if we wanted to show two or more data-field values in a single GridView column?
To accommodate this level of flexibility, the GridView offers the TemplateField, which renders using a template. A template can include a mix of static HTML, Web controls, and data-binding syntax. Furthermore, the TemplateField has a variety of templates that can be used to customize the rendering for different situations. For example, the ItemTemplate is used by default to render the cell for each row, but the EditItemTemplate template can be used to customize the interface when editing data.
To summarize here are the applications of TemplateFields
1. Combining two or more data-field values into one column
2. Expressing a data-field value using a Web control instead of text. Example: Displaying hire date using calendar control by setting the VisibleDate and SelectedDate properties to hiredate data field.
3. Used in displaying metadata about the GridView's underlying data. In addition to showing the employees' hire dates, for example, we might also want to have a column that displays how many total days they've been on the job.
4. Another use of TemplateFields arises in scenarios in which the underlying data must be displayed differently in the Web page report from the format in which it's stored in the database. Imagine that the Employees table had a Gender field that stored the character M or F to indicate the sex of the employee. When displaying this information in a web page we might want to show the gender as either "Male" or "Female", as opposed to just "M" or "F".

Sunday, October 10, 2010

Multi threading and Semaphores in C#

using System;
using System.Threading;

class SemaphoreDemo
{
static Sempahore semaphore = new Semaphore(3,3);
public static void Main()
{
for(int i=0; i<10; i++)
{
new Thread(SemaphoreDemo.DoSomething).Start(i);
}
}
static void DoSomething(object id)
{
Console.WriteLine(id+" wants to access the semaphore");
semaphore.WaitOne();
Console.WriteLine(id+" has succeeded to access the semaphore");
Thread.Sleep(1000);
Console.WriteLine(id+" is about to leave the semaphore");
semaphore.Release();
}
}

Wednesday, September 22, 2010

Partial-Page Output Caching

Partial-Page Output Caching

1. Create a new website

2. Switch to design mode on the default aspx page.

3. Insert a table with 4 rows, 2 columns.

4. Type “Time Now:” in first column of row 1.

5. Add a Label control to the second column of row1.

6. Create a custom label control

1. Create a folder controls in the solution explorer

2. Right click on the controls folder and select Add-> new item.

3. Select Web User Control and name it customLabel

4. Get rid of the code behind files.

5. Open CustomLael.ascx file

6. Drop label control in the design mode and Type in this script and OutputCache directive in to the file

<%@ Control Language="C#" ClassName="customLabel" %>

<%@ OutputCache Duration="6" VaryByParam="*" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = System.DateTime.Now.ToString("hh:mm:ss");

}

script>

<div>

<asp:Label ID="Label1" runat="server" Text="Label1">asp:Label>

div>

7. Drag and drop this control into second row second column of the table in default aspx page.

8. Type “Page attribute for control:” in the second row first column.

9. Create another custom control in the controls folder and name in customLabel1 but this time do not delete code behind files

1. Drop a label control into the file

2. In the code behind add the PartialCaching attribute and label text value.

[PartialCaching(12)]

public partial class customLabel1 : System.Web.UI.UserControl

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = DateTime.Now.ToString("hh:mm:ss");

}

}

10. Drag and drop this control into third row second column of the table in default aspx page.

11. Type “Class attribute for control:” in the third row first column.

12. Compile and execute.

13. You will notice that every time your refresh, the first label control updates but the second label control updates every 6 seconds and third label control updates every 12 seconds.

14. Now add a page level directive on the default aspx page

<%@ OutputCache Duration="3" VaryByParam="*" %>

15. Save and execute. You will notice that the first label control updates every 3 seconds, the second label control updates every 6 seconds and third label control updates every 12 seconds.

16. Sometimes will need to create areas on the page that can be updated dynamically and then integrated into a cached page. If you have a chunk of code or a method that you want to execute irrespective what the page level cache is set for, there is a new control called Substitution control to do that.

Use the Substitution control to specify a section of an output-cached Web page where you want to display dynamic content. The Substitution control offers a simplified solution to partial page caching for pages where the majority of the content is cached. You can output-cache the entire page, and then use Substitution controls to specify the parts of the page that are exempt from caching. Cached regions execute only once and are read from the cache until the cache entry expires or is purged. Dynamic regions execute every time that the page is requested. This caching model simplifies the code for pages that are primarily static, because you do not have to encapsulate the sections to cache in Web user controls. For example, this caching model is useful in a scenario where you have a page that contains static content, such as news stories, and an AdRotator control that displays advertisements. The news stories do not change frequently, which means that they can be cached. However, every time that a user requests the page, you want to display a new advertisement. The AdRotator control directly supports post-cache substitution and renders a new advertisement every time that the page posts back, regardless of whether the page is cached.

17. In the fourth row second column drag and drop the substitution control. Click on properties and type GetRealTime for MethodName property.

18. In the fourth row first column type “Substitution control:”

19. Switch to source mode and at the top of the page add this script

<script runat="server">

private static string GetRealTime(HttpContext context)

{

return "Uncached time is " + DateTime.Now.ToString();

}

script>

20. Save and execute. You will notice that the time changes for every refresh in the substitution control while the first three label controls refresh every 3, 6, and 12 seconds respectively.