Thursday, June 9, 2011

Becareful with Tasks inside for loop

When we write such a for loop;

for (int i = 0; i < 10; i++)
{
      Task.Factory.StartNew(() =>
      {
            Console.WriteLine(i.ToString());
      });
}

We expect to get an unordered numbers between 0-9. However this is not the case. When you run this code, you will get such a result; (or similar but not as expected.)

10
10
10
10
10
10
10
10
10

May be this is obvious for some of you guys but there are many people out there trying to figure out what's wrong. Lets take a look to the next example to understand the problem easier.

for (int i = 0; i < 10; i++)
{
       Task.Factory.StartNew(() =>
       {

            Thread.Sleep(1000);
            Console.WriteLine(i.ToString());
       });
}

What we do here is initializing 10 tasks that will wait for a second and then print the value of i. That means value of i will be already 10 at the time our tasks reaches to the code Console.write(). This is exactly the same reason why first loop doesn’t work as expected because for loop finishes loop iteration before any tasks even starts.

An easy solution to fix this problem would be as follows;

for (int i = 0; i < 10; i++)
{

      int temp = i;
      Task.Factory.StartNew(() =>
      {
           Console.WriteLine(temp.ToString());
      });
}

Now, we have the expected values.

0
2
3
4
5
6
1
8
9
7

Tuesday, March 15, 2011

Assigning a hostheader to https in IIS.7.0

1) Go to cmd window.

2) Go to directory C:\Windows\System32\Inetsrv\

3) Type

appcmd set site /site.name:"<IISSiteName>" /+bindings.[protocol='https',bindingInformation='*:443:<hostHeaderValue>']

Tuesday, July 13, 2010

Reverse lookup Asp-Routing

When we use asp routing, it is easy to get the routed path from current request. 

var routedPath = HttpContext.Current.Request.Path;

But how to get the original path is a bit tricky. Here, how you do it.

var originalPath=   ((PageRouteHandler) System.Web.HttpContext.Current.Request.RequestContext.RouteData.RouteHandler).VirtualPath;

Tuesday, February 16, 2010

Simple task Scheduling using Global.asax

The article below describes very simple task scheduling by just using Global.asax.

http://www.mikesdotnetting.com/Article/129/Simple-task-Scheduling-using-Global.asax

However, I had changed the approach slightly because I didn't  want to rely on Session_Start() event and execute my code unnecessarily. Below you find another simple example with Timer control.

private static Timer ScheduledTaskTimer = new Timer();

void Application_Start(object sender, EventArgs e){

      ScheduledTaskTimer.Elapsed += new ElapsedEventHandler(ScheduledTaskTimer_Elapsed);

       ScheduledTaskTimer.Interval = 1 * 60 * 60 * 24;

    ScheduledTaskTimer.Enabled = true;

}

static void ScheduledTaskTimer_Elapsed(object sender, ElapsedEventArgs e)

        {

            //your scheduled task logic goes here.

        }

Technorati Tags: ,,,

Tuesday, October 6, 2009

Raising Events From a User Control

Code behind of a user control:

public event System.EventHandler CertainEventOccured;

Fire it when this event occurs like follows;

if(this.CertainEventOccured != null){

        this.CertainEventOccured(this, new EventArgs());

}

Code behind of the page:

Wire up the the event like;

this.MyControl.CertainEventOccured += new EventHandler(MyControl_CertainEventOccured);

And finally define the function that will run when this event is fired.

private void MyControl_CertainEventOccured(object sender, EventArgs e) {

       //your logic goes here

}

Friday, June 19, 2009

Tuesday, January 20, 2009

Thursday, November 27, 2008

deneme 1 2

Monday, November 24, 2008

Loading Images Asynchronously Inside a GridView

1) Add a template field for gridview.

<asp:TemplateField>

<HeaderTemplate>Picture</HeaderTemplate>

<ItemTemplate>

<img border="1" src="images/cursor.jpg" onerror="this.src=images/error.jpg" onload="RetrievePicture(this,'<%# Eval("pic_id")%>');"/>

</ItemTemplate>

</asp:TemplateField>

2) Javascript function to call.

<script type="text/javascript" language="javascript">

function RetrievePicture(imgCtrl, picid)

        {

            imgCtrl.src = 'ShowImage.ashx?id=' + picid;

        }

</script>