Pages

Friday, March 26, 2010

Threading and Perception

Today I thought to tell you something on one of the most complex part of .NET Framework I.e. Threading. I was discussing some of the issues with one of our team member on threading today;
from taking inspiration on that conversion; here are the facts of .NET Threading....

.NET Thread object is "ACTUALLY" not created by .NET; its Operating level thread we are just getting a decorated version of that thread. When we talk about Thread; we must remember the thread pool. The thread pool is just a wrapper class of OS Level thread pool which is optimized for faster allocating/deal locating the memory and thread data. While taking back used thread from user application the same class is also responsible to cleared off the data. This is the internal operation; as a user we don't need to care about it.

The second fact is: some of the blogs (NOT MSDN) mentioned that when you write the code something like:

Thread t = new Thread(DoSomething);

Its actually not a part of ThreadPool and its creating new thread; which may be right in a way. But this is not the fully truth. If we believe on the blog for an instance; we may not get duplicate thread ever. Now here i am no going in depth of Allocation thread memory, execution etc. but let me tell you one point; even if you write above statement in .NET; it serves the thread object from Pool only. I know you will not believe this fact.

I have created an example which works on above assumption have a look once.. its interesting to see. I am creating thread using above code and some times i am getting new thread (Displayed in White Font) and some times i get duplicate thread (Displayed in Yellow Font). It proves that internally .NET is keeping a pool of thread.

The second point; In example;I have also set some data on fresh thread each time. Which I am trying to recover when I get the duplicate thread object. And look at the result; The Thread Slot is still there; but .NET erased the data.

In summery you can say that:

1. Thread object never created/destroyed from .NET Managed Env. user always gets the thread object from pool only.
2. The data you associates with thread (In terms of Thread Slots and ThreadStatic variables) are not persisted on the thread when .NET collects the unused thread for pooling.

When Thread Slots are getting cleared off; there is no question to persist the value of ThreadStatic variables.

Now talking about WCF execution; like this example when you open the WCF Service Channel/Port; it takes one of the available thread from same pool and starts the execution of the code using the thread. You can never get the older data associated with associated with thread earlier. So its perfectly safe to use ThreadStatic variables.

This was about console application; lets take ASP.NET application in consideration. .NET takes thread from ThreadPool to execute the request. This thread is persisted in ThreadPool even though the values of ThreadStatic variables are not getting persisted.

Check-out my ASP.NET example and observe thread static behavior in PageLoad method.


In short; make the best use of ThreadStatic without any fear.

Thursday, March 25, 2010

Google says Good Bye to China

Google has executed its first step towards closer of their China Operations (Google Search, Google News and Google Images mainly).

Users visiting Google.cn are now being redirected to Google.com.hk, where google offers uncensored search in simplified Chinese, specifically designed for users in mainland China and delivered via our servers in Hong Kong.

Google.cn is now Google.com.hk


--
Kaushal Patel

Tuesday, March 09, 2010

Playing with Generic List

Generic List a new feature provided by .NET 2.0 and above which can be declared as follow: List<string> lstString = new List<string>();
Creating Generic Object using Reflection
Now; there is nothing now in this. And we all know that the generics are type safe. That means you can not pass Child object's referece (Without type casting) where the base class is expected. In case if you need to create Generic List object dynamically depending upon your business scenarios you can use Reflection API to create generic object.

Lets see how to do this:

public static object CreateGenericObject(Type genericType, Type innerType, params object[] args)

{

System.Type speType = genericType.MakeGenericType(new System.Type[] { innerType });

return Activator.CreateInstance(speType, args);

}


And your client code would be as follows:


object geneticListObject = CreateGenericObject(typeof(List<>),typeof(int));


Now lets see one more feature of .NET 2.0 i.e. Inline functions (anonymous methods):


Lets assume you have a generic collection of objects (lets assume student); and you want to search on Student Name starting with "K". You dont need to make another call to the server for this. Here is the code for the same:


List<Student> localStudentList = mainStudentList.FindAll(delegate(Student student)
{
return student.StudentName.StartsWith("K");
});


the above code will return the list of students (in localStudentList object) whose name starts with K.To extends this you can write various AND and OR conditions. Isn't this good?



--
Kaushal Patel
Contact Me Twitter


Wednesday, March 03, 2010

BCL new features of the .NET Framework 4.0 Beta 2

Visual Studio 2010 and .NET Framework 4 Beta 2 are now available to download. .NET 4 Beta 2 contains various newly introduced BCL features and enhancements in addition to what was included in the earlier version of .NET Framework 4.0 Beta 1. Many of these enhancements were added in large part due to specific feedback and suggestions reported by customers through Microsoft Connect.

New BCL Features in .NET Framework Beta 2.0

  1. Complex Number
  2. Location
  3. Stream Copy (Stream.CopyTo)
  4. IObservable<T>
  5. Enum.HasFlag
  6. String.Connect & String.Join
  7. String.IsNullOrWhiteSpace
  8. TryParse (In Guid, Version and Enum)
  9. Addition Environment.SpecialFolder, Environment.Is64BitProcess and Environment.Is64BitOperatingSystem
  10. Path.Combine with params support
  11. ObservableCollection<T> is now part of System.DLL
  12. Int Pointer operations (IntPtr and UIntPtr Subtraction operators)
  13. StringBuilder.Clear
  14. ServiceInstaller.DelayedAutoStart
  15. StopWatch.Restart


-- Kaushal Patel
Contact Me Twitter

Task Parallel Library Sample is out now!!!

Microsoft .NET Framework 4.0 TPL (Task Parallel Library) sample is out now. This sample has been created in .NET Framework 4.0 Beta 2
Click here to get the newest feature of .NET Framework 4.0