Rahul Anand's Blog

If my mind can conceive it, and my heart can believe it, I know I can achieve it.

  Home  |   Contact  |   Syndication    |   Login
  5 Posts | 1 Stories | 17 Comments | 7 Trackbacks

News

Article Categories

Archives

Post Categories

Friday, March 02, 2007 #

Today I encountered some weird behavior in .NET Framework.

 

I had written some code like:

 

[CODE]

while (reader.Read())

      {                   

DataRow row = dtSchema.NewRow(); 

// The above line was throwing Out of Memory Exception

for (int i = 0; i < reader.FieldCount; i++)

{

row[i] = reader.GetValue(i);

}

// Some logic which uses this row

}

[/CODE]

 

Since I was not adding this row to the DataTable Rows collection, my assumption was it will not affect the memory allocation of DataTable (dtSchema).

 

But I found that the .NET Framework is trying to reallocate the DataTable whenever you do the NewRow on the DataTable (if .NET thinks this Row can not be accomodated in the current allocation). Ideally .NET Framework should try to reallocate the memory only when you are adding a row to the data table.

 

To fix this problem I modified my code to:

 

[CODE]

DataRow row = dtSchema.NewRow();     

// Create only one row and assign it values, before using, in the loop

 

while (reader.Read())

      {                   

for (int i = 0; i < reader.FieldCount; i++)

{

row[i] = reader.GetValue(i);

}

// Some logic which uses this row

}

[/CODE]

 


Wednesday, March 22, 2006 #

By implementing multithreading in an application we can enable it to perform multiple operations simultaneously. Do remember that multithreading can not make a single processor to process two task in a single clock cycle, but the performance can be improved by utilizing the idle time of the processor. The other major benefit of multithreading in an application is, it drastically improves the response time.

 

Multithreading is not a suitable choice if the tasks at hand are dependent on each other. In this case multithreading will even slow down the processing, as it incurs a cost due to overheads involved with context switches.

 

Implementation of multithreading also adds a lot of complexity which needs to be addressed like deadlock handling, synchronized accesses, code complexity etc.

 

Hence implementing multithreading in an application should be a very thought of decision, so that the performance and response time can be improved.

 

At the design time we need to break down the whole program in smaller tasks which are independent of each other and can be performed simultaneously. These smaller tasks can be performed by different threads to improve performance.

 

Multithreading is almost unavoidable when we write a windows application which perform some real world tasks.

 

Windows Forms uses the single-threaded apartment (STA) model because Windows Forms is based on native Win32 windows that are inherently apartment-threaded. The STA model implies that a window can be created on any thread, but it cannot switch threads once created, and all function calls to it must occur on its creation thread. Outside Windows Forms, classes in the .NET Framework use the free threading model.

 

The STA model requires that any methods on a control that need to be called from outside the control's creation thread must be marshaled to (executed on) the control's creation thread. The base class Control provides several methods (Invoke, BeginInvoke, and EndInvoke) for this purpose. Invoke makes synchronous method calls; BeginInvoke makes asynchronous method calls.

 

When we run a Windows Application it always starts with a main thread that owns the form controls and it may be called the UI thread. Now suppose if we have some long running task execution involved on the click of a button, the UI will become irresponsive till it returns from the click handler method.

 

private void btnGetData_Click(object sender, System.EventArgs e)

{

// Long running task, simulated with a sleep of 1000 seconds

      Thread.Sleep(1000000);

}

 

The obvious solution is to make this long running task a separate method and call that method asynchronously from the click event handler.

 

private void MyLongRunningTask()

{

// Long running task, simulated with a sleep of 1000 seconds

      Thread.Sleep(1000000);

 

}

 

public delegate void LongTaskInvoker();

 

private void btnGetData_Click(object sender, System.EventArgs e)

{

      LongTaskInvoker myInvoker = new LongTaskInvoker(MyLongRunningTask);

/* No callback is required so first argument is null, no callback method is required hence no additional information is needed hence second argument is null */

myInvoker.BeginInvoke(null, null);

}

 

BeginInvoke() is a simple way to assign some work to a different thread. The .NET Framework allows you to call any method asynchronously by defining a delegate with the same signature as the method you want to call; the common language runtime automatically defines BeginInvoke and EndInvoke methods for this delegate, with the appropriate signatures.

The BeginInvoke method has the same parameters as the method you want to execute asynchronously, plus two additional parameters – a callback delegate and other information in an object (which will be available to callback method). BeginInvoke returns immediately and does not wait for the asynchronous call to complete. BeginInvoke returns an IasyncResult, which can be used to monitor the progress of the call.

Now this seems to work fine until we have a scenario where we want our asynchronous task to keep updating the UI about its processing. For instance suppose we have a long running task which calls a Web Service to get some data and then it do some calculations. The requirement is to keep the UI updated about the processing stage of this task, say we want to show messages like “Connecting to web service”, “Data returned by web service”, “Calculation stage 1”, “Calculation stage 2”,…..“finished processing”.

In an attempt to implement this requirement we will modify the MyLongRunningTask as follows:

  

private void MyLongRunningTask()

{

      // Long running task, simulated with a sleep of 1 second in a loop

      ShowWorkerMessage("Starting Long Running Task");

      for(int i = 0; i<1000; i++)

      {

            Thread.Sleep(1000);

            ShowWorkerMessage("Task Stage: "+ i.ToString());

      }

      ShowWorkerMessage("Completed Long Running Task");

}

 

public void ShowWorkerMessage(string message)

{

      lblMessage.Text = message;

}

 

Now here we are in trouble with this code – can you guess what? Yes it is a violation of statement – “The STA model requires that any methods on a control that need to be called from outside the control's creation thread must be marshaled to (executed on) the control's creation thread”. With BeginInvoke() call the MyLongRunningTask() executes on a different thread than UI thread. So it can not directly call ShowWorkerMessage() as it in turn updates the window form control (lblMessage). To solve this issue we modify the MyLongRunningTask() implementation as follows.

 

private void MyLongRunningTask()

{

// Long running task, simulated with a sleep of 1 second in a loop

      this.Invoke(ShowWorkerMessage,

            new object[]{"Starting Long Running Task"});

      for(int i = 0; i<1000; i++)

      {

            Thread.Sleep(1000);

            this.Invoke(ShowWorkerMessage,

                  new object[]{"Task Stage: "+ i.ToString()});

      }

      this.Invoke(ShowWorkerMessage,

            new object[]{"Completed Long Running Task"});

}

 

Since we have multiple call to ShowWorkerMessage() it would be better if we put the invocation related complexity in side the ShowWorkerMessage() method implementation. For this we modify the ShowWorkerMessage() as follows and revert the changes made in MyLongRunningTask() to previous version.

 

public void ShowWorkerMessage(string message)

{

if(lblMessage.InvokeRequired)

{

      this.Invoke(ShowWorkerMessage,

new object[]{message});

}

else

{

lblMessage.Text = message;

}

}

 

InvokeRequired  is a read only property defined under the base class ‘Control’, gets the value indicating whether the caller must call an invoke method when making method calls to that control because the caller is on a different thread than the one the control was created on.

 

In case the work to be done is complex in implementation, it is better to make a different class for worker thread which abstracts the implementation details of work to be processed.

 

public class WorkerThread

{

 

      private Thread _thread = null;                             

 

      public WorkerThread()

      {

            _thread = new Thread(new ThreadStart(Run));

            _thread.IsBackground = true;             

      }

 

      public void Start()

      {

            _thread.Start();

      }

 

      public Thread WorkerThread

      {

            get{ return _thread; }

      }

 

      private void Run()

      {

            // Implementation of work to be done                 

      }

}

 

private void btnGetData_Click(object sender, System.EventArgs e)

{

      WorkerThread myWorker = new WorkerThread();

myWorker.Start();

}

 

There are situations when the main UI thread wants to get some information about some critical event in the worker thread so that it can update some controls that it owns. Here we can use the framework’s delegate/event /handler architecture.

 

Suppose our worker thread raises an event when it has completed the work assigned, then the main thread can subscribe to it and display the completed message and enables the button for further processing.

 

We define a delegate for that purpose, and an event under the worker thread class:

 

public delegate void CompletedEventHandler(object sender, EventArgs e);

 

public event CompletedEventHandler WorkCompleted;

 

Following the design recommendations we implement a protected OnEvent method which actually raises the event.

 

protected void OnWorkCompleted(EventArgs e)

{

      if(WorkCompleted != null)

            WorkCompleted(this, e);

}

 

Now the main class can subscribe this event and do the necessary implementation in its event handler method.

 

private void btnGetData_Click(object sender, System.EventArgs e)

{

      WorkerThread myWorker = new WorkerThread();

myWorker.WorkCompleted +=

new CompletedEventHandler(mainThread_WorkCompleted);

myWorker.Start();

}

 

private void mainThread_WorkCompleted(object sender, EventArgs e)

{                

      stStatus.Text = "Done";

      btnStart.Enabled = true;

}

 

The delegate/event/handler architecture gives us another way to pass the message from worker thread to main thread so that it can update its controls.

 

I had taken reference lines from MSDN article where it was relevant to this article.

 

I would like to mention other useful links which talks about similar issues:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp

    http://weblogs.asp.net/justin_rogers/articles/126345.aspx

 


Tuesday, August 16, 2005 #

Regular Expression is a language independent feature supported by many languages, notably PERL, Java, JavaScript, C# etc. The support for Regular Expression is extensive under PERL and thus there is a term coined PCRE (Perl Compatible Regular Expression).

.NET has followed the similar pattern writing syntax.

 

The Base Class Library includes a namespace (System.Text.RegularExpressions) where a set of classes have been exposed to utilize the power of regular expressions.

 

Summarizing the widely used classes to utilize the power of regular expressions under C#:

 

Use static methods of Regex class or instance method to match a pattern or replace a pattern. After successful match the result of a regular expression is a collection (MatchCollection) of Match objects. Within each Match object is a collection (GroupCollection) of Group objects. Each Group object within the GroupCollection represents either the entire match or a sub-match that was defined via parenthesis. Within each Group object is a collection (CaptureCollection) of Capture objects. Each Capture object contains the results from a single subexpression capture.

 

I will try to explain each of them with some example for better understanding.

 

Regex class provides several static methods to enable you check for match or get matches without even instantiating the Regex object.

 

Escape: Escapes all meta-characters within a pattern string.

Unescape: Un-escapes any escaped meta-characters within a pattern string.

 

IsMatch: A Boolean value is returned depending on whether the pattern is matched in the string or not.

Match: A match instance is returned for the first string matched as defined by pattern.

Matches: A collection of matches are returned as MatchCollection

Replace: Replaces the first occurrence of the pattern in the string.

Split: Split the strings over pattern to get an array of strings.

 

Except Escape and Unescape all of the above methods are also available as instance members of Regex class. The static methods are provided to allow an isolated, single use of a regular expression without explicitly creating a Regex object.

 

Let’s write some sample code to see how regular expression works in C#:

 

Sample1:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match more than once occurrence of 'a'

string pattern = "a+";

if(Regex.IsMatch(content, pattern))

Console.WriteLine("Pattern Found");

else

      Console.WriteLine("Pattern Not Found");

 

Output:  Pattern Found

 

 

Sample2:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match one or more than one occurrence of 'a', using ^ and $ enforces that

// whole string must be matched

string pattern = "^a+$";

if(Regex.IsMatch(content, pattern))

      Console.WriteLine("Pattern Found");

else

      Console.WriteLine("Pattern Not Found");

 

Output:  Pattern Not Found

 

 

Sample3:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match all digits (at least one) which is preceded by one or more than one

// occurrence of 'a' and optionally followed by 'b'

string pattern = @"a+(\d+)b*";

MatchCollection mc = Regex.Matches(content, pattern);

string spacer = "";

if(mc.Count > 0)

{

      Console.WriteLine("Printing matches...");

      for(int i =0; i < mc.Count; i++)

      {

            spacer = "";

            Console.WriteLine();

            Console.WriteLine(spacer+ "Match["+i+"]: "+ mc[i].Value);                    

            Console.WriteLine(spacer+ "Printing groups for this match...");

            GroupCollection gc = mc[i].Groups;

            for(int j =0; j < gc.Count; j++)

            {

                  spacer = " ";

                  Console.WriteLine(spacer+ "Group["+j+"]: "+ gc[j].Value);                                

                  Console.WriteLine(spacer+ "Printing captures for this group...");

                  CaptureCollection cc = gc[j].Captures;

                  for(int k =0; k < cc.Count; k++)

                  {

                        spacer = "  ";

                        Console.WriteLine(spacer+ "Capture["+k+"]: "+ cc[k].Value);                              

                  }

            }                            

      }

}

else

{

      Console.WriteLine("Pattern Not Found");

}

 

Output:

Printing matches...

 

Match[0]: aaa123b

Printing groups for this match...

 Group[0]: aaa123b

 Printing captures for this group...

  Capture[0]: aaa123b

 Group[1]: 123

 Printing captures for this group...

  Capture[0]: 123

 

Match[1]: aaa123

Printing groups for this match...

 Group[0]: aaa123

 Printing captures for this group...

  Capture[0]: aaa123

 Group[1]: 123

 Printing captures for this group...

  Capture[0]: 123

 

 

Here the first two samples are simple enough to understand. In ‘Sample 1’ I am just checking whether one or more than one consecutive ‘a’ (i.e. at least 2 ‘a’s) occurs in the given string.  And similarly in ‘Sample 2’ I am checking whether the given string starts and ends with ‘a’ and may contains more ‘a’s (which is false as we have other characters also in the given string).

 

Now let us examine the third sample, here you might be thinking why .NET has so many different classes Match, Group, and then Capture. With this example the difference between these are not very evident. Here we find Match[0], Group[0] and Capture[0] all containing the same value.

 

Let us see what MSDN says about these:

 

Match: The Match class represents the results of a regular expression matching operation.

Group: The Group class represents the results from a single capturing group.

Capture: The Capture class contains the results from a single subexpression capture.

 

In simple words Match produces everything that is matched by given pattern, when regular expression search is performed on the given string. There can be multiple occurrence of pattern in the given string, in that case you can get the collection of such matches by calling the Matches API provided by Regex class.

 

The Group is a part of given pattern enclosed by the ‘(‘ and ‘)’. So a group contains the part of matched string which is matched by the subpattern enclosed under brackets. Exception to this the Group[0] always contains the whole match (same as the value of Match).

 

The Capture is a part of string matched by the group expression i.e. the string matched by a subexpression of group expression. To understand it better I will slightly modify the ‘Sample 3’ to make single digit match as a subexression of the group.

 

Sample4:

 

string content = "123abbbabbaaa123baaaabbbbcccaaa123cccbbb123";

// Match all 1,2 or 3 (at least once) which is preceded by one or more than one

// occurrence of 'a' and optionally followed by 'b'

string pattern = @"a+(1|2|3)+b*";

MatchCollection mc = Regex.Matches(content, pattern);

string spacer = "";

if(mc.Count > 0)

{

      Console.WriteLine("Printing matches...");

      for(int i =0; i < mc.Count; i++)

      {

            spacer = "";

            Console.WriteLine();

            Console.WriteLine(spacer+ "Match["+i+"]: "+ mc[i].Value);                    

            Console.WriteLine(spacer+ "Printing groups for this match...");

            GroupCollection gc = mc[i].Groups;

            for(int j =0; j < gc.Count; j++)

            {

                  spacer = " ";

                  Console.WriteLine(spacer+ "Group["+j+"]: "+ gc[j].Value);                                

                  Console.WriteLine(spacer+ "Printing captures for this group...");

                  CaptureCollection cc = gc[j].Captures;

                  for(int k =0; k < cc.Count; k++)

                  {

                        spacer = "  ";

                        Console.WriteLine(spacer+ "Capture["+k+"]: "+ cc[k].Value);                              

                  }

            }                            

      }

}

else

{

      Console.WriteLine("Pattern Not Found");

}

 

Output:

 

Printing matches...

 

Match[0]: aaa123b

Printing groups for this match...

 Group[0]: aaa123b

 Printing captures for this group...

  Capture[0]: aaa123b

 Group[1]: 3

 Printing captures for this group...

  Capture[0]: 1

  Capture[1]: 2

  Capture[2]: 3

 

Match[1]: aaa123

Printing groups for this match...

 Group[0]: aaa123

 Printing captures for this group...

  Capture[0]: aaa123

 Group[1]: 3

 Printing captures for this group...

  Capture[0]: 1

  Capture[1]: 2

  Capture[2]: 3

 

You must be getting confused over the strings matched by Group[1] in this example. Again quoting from MSDN:

 

Because Group can capture zero, one, or more strings in a single match (using quantifiers), it contains a collection of Capture objects. Because Group inherits from Capture, the last substring captured can be accessed directly (the Group instance itself is equivalent to the last item of the collection returned by the Captures property).

 

Soon I will add more in this posting.....

 

For an overview of Regular Expression >> Basics of Regular Expression

 


Friday, August 05, 2005 #

I just wanted to share few design considerations on exception handling I learnt a few days back.

 

One very important point is whenever you re-throw an exception do what you really want to do. What I mean is there are different ways to re-throw an already caught exception:

 

You can either write:

throw ex;

Or, just write

                throw;

 

The two statements differ significantly on how the stack trace will get affected. In first case when you write “throw ex;” statement the information about your method is added onto stack trace as an origin of exception and the actual origin of exception, got cleared from the stack trace. In other case with “throw;” statement you are just re-throwing the same exception without any modification to the origin of exception in the stack trace output.

 

Not sure, but my finding is stack trace holds one entry of exception (outermost exception caught) per method entry, hence even with “throw;” statement you will lose some information about the exact point of error.

 

The following statement explains the behavior of stack trace more specifically:

 

The stack trace begins at the statement where the exception is thrown and ends at the catch statement that catches the exception.

 

Another possible way is to recreate the exception by adding information and passing original exception as inner exception to the exception constructor. You may need this if you want to add some specific information about the exception.

 

Quoted from MSDN:

You can explicitly throw an exception using the throw statement. You can also throw a caught exception again using the throw statement. It is good coding practice to add information to an exception that is re-thrown to provide more information when debugging.

 

try

   {

                // FileNotFoundException can occur

   }

catch(FileNotFoundException e)

   {

            throw new FileNotFoundException("filename.ext not found in c:\\devdirectory",e);

   }

finally

   {

            // Cleanup

   }

 

In this case information about all exception points can be extracted by recursively getting the stack trace of inner exceptions or by implementing a while loop till the InnerException returns null. The ToString() method will also display all exceptions.

 

So, next time when you are going to re-throw some exception do what you really want to do.

 


Wednesday, August 03, 2005 #

A few days back I was just coding for a basic TCP/IP Client to send and
receive buffer across network.

I encountered a problem in receiving data through receive method.

I am sharing this in this blog as it may help others, who are novice is
.NET socket programming.

I searched all over net for samples and found all over (including MSDN).
Blocking mode was enough for me so I coded my receive as:



Byte[] bytesReceived = new Byte[256];
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);



But later I noticed that if in last iteration the bytes come to be less than
256 i.e. when I have received all data the next receive call causes the
execution to block as receive has nothing to receive.

Realizing that I changed my code to:



Byte[] bytesReceived = new Byte[256];
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes != 256);



It worked fine for all my real scenarios. But I kept thinking that it will
also cause problem if the bytes to be received is multiple of 256 chunks. In
that case it will also block execution when last time it calls receives
method.

Finally I got something better:



Byte[] bytesReceived = new Byte[256];
while (s.Available)
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}



I thought it would be appropriate to change my do-while loop to while. But I
did a mistake there. As sometimes s.Available returns 0 even if receive is
able to receive data. And so in all such cases my loop to receive data got
bypassed.
As I analyzed this and guessed reason was the data chunk was not available
when s.Available is called but since receive is a blocking call it waits for
data arrival and finally returns with data.

So, finally I changed the code to following and it worked fine.



Byte[] bytesReceived = new Byte[256];
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (s.Available);