The thread pool gives you an easy and safe way for creating multithreaded applications, and even though the stateless nature of the Internet isn’t the best place for multithreading, it can still be the right thing to do for certain scenarios.

I’ve seen a lot of examples on how to use the thread pool and none of them takes advantage of the Boolean return value of the QueueUserWorkItem method. This is important because if the thread pool fails to create a new thread you will not be notified about it. Here is a short example of a button’s click event handler that creates a new thread to do some work.

protected void btnSave_Click(object sender, EventArgs e)

{

  if (System.Threading.ThreadPool.QueueUserWorkItem(ThreadProc))

  {

    Response.Write("Processing is started successfully.");

  }

  else

  {

    Response.Write("An error occured.");

  }

}

 

private void ThreadProc(object stateInfo)

{

  DoSomething();

}

By using the Boolean return value we can now catch the failure and notify the user or maybe try again.

Recently, one of my readers asked me how to block certain IP addresses from accessing his ASP.NET website. It was a good question that could be answered in multiple correct ways. My answer was a plug ‘n play HttpModule that could be reused in any ASP.NET application. When an IP address is blocked it stops the response and sends a “403 Forbidden” header.

Even though it’s almost impossible to block someone from accessing your website, this is a simple way to make it much harder to do. For the regular web users this is probably enough to keep them out.

Implementation

Download the IpBlockingModule.cs below and add it to the App_Code folder. Then add the following line to the <system.web> section of the web.config.

< httpModules >

  < add type = " IpBlockingModule " name = " IpBlockingModule " />

</ httpModules >

Then add the IP addresses you want to block, separated by commas, to the appSettings in web.config.

< appSettings >

  < add key = " blockip " value = " 44.0.234.122, 23.4.9.231 " />

</ appSettings >

Download

IpBlockingModule.zip (0,76 KB)