Thursday, July 3, 2008

Multi Thread Programming in .NET

What is multi thread programming?

Multi thread programming allows you to do two or more tasks at the same time. As a windows user you have experienced doing two or more tasks together for example while you are listening to music you are using word at the same time you may printing a picture as well. It means that your computer has the ability of doing some tasks simultaneously. Multi thread programming gives you a chance to do this with different parts of the code.

What is process?

Process is a space assigned by CPU allows the code to be run in that space.

What is Thread?

Each process has at least one Thread which is a reference for CPU to run next line in that process

A better definition for Multi threads programming:

Create different thread in current process to run two different parts of the code simultaneously.

Why do we need Multi thread programming?

Each process has at least one thread that run the code called main thread. Sometimes we cannot hang main thread because it is reflecting the hang issue to user. Let me give you an example. Let's say we have a windows application and in part of our code we need to run an external application which may take 1 minute to run. But we cannot spend 1 minute to run this application because the main thread is responsible to render user interface as well so we may create a new thread to do this task for us and leave the main thread to take care of user interface. You see sometimes without multi thread programming our code is not efficient.

How multi thread programming works in .net?

Thanks to .net framework where you can find so many ways to implement multi thread programming. Even you can find this awesome technology in ado.net and UI and... I am going to start from very basic one which is the simple Thread class.

First rule in multi thread programming is that you can run part of your code in another thread as long as your code is defined in a method. It means that if you need to run part of your code in new thread you need to define that code in a method. So a new thread can be run over a method. In .Net there are two classes that help you to do this first is Thread class and second ThreadStart delegate. By ThreadStart you define the address of the method you need to run in a new Thread. By Thread class you start the Thread. Take a look at this code:

ThreadStart ts = new ThreadStart(myfunction);

Thread t = new Thread(ts);

t.Start();


In above code first myfunction address is assigned to ts delegate then a new object of Thread is created and in constructor the ts is passed to thread object so when Start is called it will start the new thread for ts which is actually myfunction.

I was always asked by students where do we use this how we can use this in a web based application it may not be useful for a web based application. But the reality is that you can use this even in a web based application in next post I show you how! I was assigned a small project that I had to use multi thread programming

Thursday, June 19, 2008

Passing viewstates of a page to another page

Download the source code

Sometimes we need to pass information of one page to the next page to do this there are different approaches one of them is that you can pass just a querystring then lookup data in your data source. However sometimes there are no data sources so you need to have all states of one page in another page. So you may use Session object to pass this information. However, this may not be the best way because you are using Session object which is shared between different pages for a user. There is another way which is PostBackUrl.(In next posts I will compare State Management in asp.net)

How to pass viewstate by PostBackURL

After Visualstudio 2005 all our buttons has a nice properties called PostBackUrl which can be assigned to a page then state of this page (the page that has button with PostBackUrl) will be passed to Next page.

Take a look at this:

In this example we have a textbox and a dropdownlist and we need to see state of these two controls in next page. Also we have a button that its PostBackUrl is set to a page. When user click on this button the state is passed to next page to access these states you need to use PreviousPage object. Remember if someone tries to access this page by giving this page address into browser or user is redirected from other redirection method Then PreviousPage object is null. So to be in safe side always check this object. Take a look to this code:

if (PreviousPage != null)

{

DropDownList lst = PreviousPage.FindControl("DropDownList1") as DropDownList;

if (lst != null)

LabelDropdown.Text = lst.Text;

TextBox txt = PreviousPage.FindControl("TextBox1") as TextBox;

if (txt != null)

LabelTextbox.Text = txt.Text;

}

As you see in the code we are checking PreviousPage object so we are sure that previous page state is passed to this page it means that redirection method was PostBackUrl.

Also we have another method to make controls type safe which is defining PreviousPageType Directive (in html code) but the issue with this approach is that you can define one page as your previous page. Also if user access to this page with any other of redirection method then an error will be generated. So I highly recommend you use the previous approach and never user PreviousPageType directive.

Let's see the steps:

1- Define a page with a button which its PostBackUrl is filled with a page ( we call it target page)

2- In Target page Use PreviousPage.FindControl to access controls in previous page.

3- Always check PreviousPage object to be sure it is not null

Download the source code