Saturday, April 30, 2011

Free decompiler from telerik

Still in beta. Hope they introduce more features such as extracting Xaml,resources etc... soon

http://www.telerik.com/products/decompiling.aspx

Thursday, April 21, 2011

Welcome to SQLCMD

Last couple of weeks I was mainly working in the backend side.Its nothing but SQL server 2008.We had to develop a bunch of scripts which will help the production DBAs to accomplish their tasks with less effort.Since the production environment is unknown and will vary to country to country we cannot put dbo.<table name> in any of our scripts.So we decided to put some tokens in the scripts like #schema#,#DB# etc…the DBA needs to replace these tokens in his environment before running the scripts.
Things went fine till the scripts were reviewed by Microsoft consultant Dimitri Furman .He suggested to use SQLCMD variables.
What is this SQLCMD variables? Do we really need to use that? But after having a script file of 100MB we automatically preferred the sqlcmd mode because SQL Server Management Studio is not suitable for executing such a big sql file.It will definitely crash.

Reasons why we selected SQLCMD
  • We have more files which needs to share the variables.Replacing in all the files is tedious.
  • We have large files which cannot be executed in SSMS.

SQLCMD is the command prompt type interface to execute sql queries.You can start the shell using the executable located in the below location.

<drive>:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE

In the first look there is no change.But when we come to authoring sql files there can be changes.Main advantage is you can have scripting variables which will replace their values at run time both in the command mode and in the sql files.You can compare this to classic ASP or PHP programming which replaces the server script before sending to the browser.

Ok Lets see what is this scripting variable by looking at an example.

:setvar PersonName Joy
:setvar email joymon@gmail.com
INSERT INTO Person ([Name] ,[Id] ,[EMail])
VALUES('$(PersonName)', newId(), '$(email)')
GO


PersonName & email are variables and their current values are Joy & joymon@gmail.com respectively.When we refer them using the $() they will get replaced with the values.So here the values got replaced between the single quotes ‘’ and the query run smoothly.
Another advantage is we can call another script file from this using the :r keyword.See the example below.

r: "c:\queries\SQLQuery2.sql"


If we set a variable in one file and that file is calling another file the variables will be available in the second sql script also.If we took the above example,the variable $(PersonName) can be used in SQLQuery2.sql as well.There are lot many advantages, if you explore the possibilities of SQLCMD.Refer the below links for more details.

http://msdn.microsoft.com/en-us/library/ms188714.aspx

http://www.sqlbook.com/SQL-Server/SQLCMD-command-line-utility-13.aspx
Running the sql files with SQLCMD variables in SSMS
You can even run the sql files with SQLCMD variables in SSMS.For that change the query mode to SQLCMD.See the below screenshot.

Tuesday, April 12, 2011

Class AutoResetEvent

The class AutoResetEvent is used in threading to make one thread wait for a signal, till a specified time.The program execution will continue, if any of the above said situation occurs.ie either on the timeout or on signal arrival.The signal here means calling a method on AutoResetEvent class.

The above said is theory.Lets see how to use this class in practical scenarios.

   1: AutoResetEvent autoEvent = new AutoResetEvent(false);


   2: bool _canContinue = autoEvent.WaitOne(4000);


   3: if (_canContinue) 


   4: { 


   5:                     LogMsg(string.Format("Somebody interupted.So resume"),ConsoleColor.Yellow); 


   6: } 


   7: else 


   8: { 


   9:                     LogMsg(string.Format("Nobody interupted.So exit"), ConsoleColor.Yellow); 


  10: }



After the line WaitOne the thread wait for 4000 seconds or till a signal occurs.If the signal comes the return value will be true.Else false.So if we write the above line in a loop we can continue or exit the loop based on the request.Below is the code to signal or interrupt the thread.

autoEvent.Set();


It is obvious that the autoEvent should have visibility outside the method as well in order to invoke the method.

Practical scenario

You need to write a scheduler which processes queue of objects in a separate thread.The objects to the queue are expected to arrive in a bulk manner.ie the scheduling will happen continuously for a short span of time and rest of the time it will be idle.So once the thread is started and completed one cycle of processing queue, you cannot exit the thread.Rather you need to wait for some time say 4 secs for more items into the queue.But if any object comes to the queue before or with in that 4 secs  you need to process those.Else exit the thread after 4 secs.

This scenario has happened in our project and our lead architect came with AutoSetEvent solution.Uploaded  a sample which process a list of string in this way.

Another useful classes in threading

ManualResetEvent ,WaitHandle

For better understanding see the Threading sample available in your VisualStudio installation folder. It is located in Language Samples\Threading.It explains threading using the producer-consumers problem.

Monday, April 4, 2011

Temporary Post Used For Theme Detection (7738f75c-72ba-44f9-a2e7-0bfdf2ee67c8 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

This is a temporary post that was not deleted. Please delete this manually. (7ec1ce8b-8b61-412b-8178-e773b93cc734 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)