Naveen's Weblog

Bridge to future

Posts Tagged ‘C#’

Zip/Unzip using System.IO.Package in .Net

Posted by codingsense on December 5, 2010

I was working on a project where it was required to zip and unzip files, these days we were using Cabarc.exe and all was working fine. Now we planned to give unicode support and booom!! cabarc.exe failed to zip the folder, since it does not support unicode characters. I found that there are many open source projects like SharpZipLib, DotNetZip etc. But we did not wanted to use the available open source projects to implement it. So I decided to search for some other option and what I found is System.IO.Package in WindowsBase.dll. It exposes a class called Package which will help to pack and unpack files and folders.

Download Source code (13 Kb)

I have made a separate dll (CustomPackage.dll) that you can use for zipping and unzipping of folders. I have also created a dummy project PackageWrapper to help you understand how to make calls to the dll.

Features:

  • The project supports Unicode characters in folder or files (Limitation with Cabarc.exe)
  • Zipping is done also for all sub directories . While unzipping the same directory structure will be maintained.

Improvements for you to do:
I have used Mime Type as MediaTypeNames.Application.Octet to make it generic and support all kind of files and have used the compression option CompressionOption.NotCompressed since the mime type will not compress. If you know that you will have only similar type of files to zip or unzip then you can select proper Mime type and compression option as CompressionOption.Maximum to compress the files to maximum extent.

For any kind of improvements or clarifications please feel free to comment.

Happy Coding 馃檪
Codingsense

Advertisements

Posted in C# | Tagged: | Leave a Comment »

Get all controls on a windows form without recursion

Posted by codingsense on July 22, 2010

Hi,

Its given in microsoft patterns and practices to consider replacing recursion with looping because each recursive call adds data to the stack. Examine your code and see if your recursive calls can be converted to a looping equivalent.

I was wondering if all the solutions that uses recursion can be replaced without using recursion.
So I took the first step in looking into my project and found a piece of code that was using recursion, a function that would give me all the controls on a windows form. So I decided trying to convert that code without the use of recursive call.

Here is what I came up with,



static IList getControls(Control ParentControl)
{
List<Control> ListOfControls = new List<Control>();
List<Control> ContainerControls = new List<Control>();
ContainerControls.Add(ParentControl);

while (ContainerControls.Count > 0)
{
foreach (Control control in ContainerControls[0].Controls)
{
if (control.Controls.Count > 0)
{
ContainerControls.Add(control);
}
ListOfControls.Add(control);
}
ContainerControls.Remove(ContainerControls[0]);
}
return ListOfControls;
}

Great, the method gave me proper result as compared with the recursive function, what next I wanted to see how it has increased in performance and I used stopwatch to determine the time taken by both of the methods on the same form which was heavily loaded with controls and containers, it had 82 controls on it. The time taken by recursive function was double the time taken by the new method.

So I concluded that replacing recursive method with looping will enhance performance and also helps in consuming less memory.

If you get a better way to overcome recursion then please feel free to comment, till then I will continue my search for more recursive calls in my code.

Happy Learning 馃檪
Codingsense

Posted in C# | Tagged: , | Leave a Comment »

Writing Custom FxCop聽Rules

Posted by codingsense on May 23, 2010

FxCop is a static analysis tool that you can use to write custom rules those need to be followed by the developers. Writing custom rules in FxCop is very easy, in the beginning you will feel it bit complex since there are no much help available on web but once if you have written a rule you will be preety much confident that you can write any rule you desire.

Download FxCop

SInce FxCop analyses IL code from your assemblies, we should know about some of the keywords of IL to write complex rules. Use this sheet as a reference while seeing your IL code through ILDASM.

I have created a template project for Custom FxCop Rules for you which you can use and add your rules. So that you can concentrate more on Custom Rules rather than creating the project.

Download CustomFxCopRules template

If you want to add any new rule you have to follow these steps using the above template.

  1. Create a class and name it with the rule.
  2. Create a new node of that of the classname (not necessary but only for easier reference) in CustomRules.xml with desired settings
  3. Create a constant with the same name and value in Constants.resx file
  4. Write your logic in that class

And to debug the rules that you have written here is a post
http://social.msdn.microsoft.com/forums/en-US/vstscode/thread/b92a0416-bf20-40c1-88a0-8fcff07133dc/

More References:
http://www.binarycoder.net/fxcop/pdf/fxcop.pdfhttp://www.binarycoder.net/fxcop/pdf/fxcop.pdf
http://www.nmentor.com/Blog/Tutorials/1.%20How%20to%20Write%20Custom%20FxCop%20Rule.pdf

I will put some of the custom rules in coming post which willl cover all the required topics to make you understand FxCop better. If you want any specific rule, then feel free to post a comment to me and I will help you out 馃檪

Happy Learning
Codingsense 馃檪

Posted in C# | Tagged: | Leave a Comment »

Steps to improve maintainability of your code

Posted by codingsense on May 13, 2010

Hi,

I have seen people always telling that maintaining a product is harder. All people want to go into the product or module where its building from the scratch and pass the older code to our juniors. Why?? Is it that we want to learn new things or we don’t want to waste time in the product where we have put lots of effort to ruin it or are we afraid??

Is there any easy way to convert bad code to good code. Its not like moving a magic wand and all code is straight. First of all we should accept that its our mistake, unless we accept our mistake we keep giving hundreds of reasons to run away from it. We should have interest in making it good, plan properly and execute with determination. Here’s my story wherein I have gone through phases from good to bad and bad to good.

Two years back in Feb 2008 I started a product and was playing a role of tech lead. There were lots of eyes and concerns on that product that it will be a game changer for our company. There was a long list of feature given by our functional team, plan to combine 2 existing product functionality into one, easy navigation, and the old saying was repeated “URGENT REQUIREMENT” we were given 1 year for the first product release. So it began, planning of modules, prioritizing the features, coding standards was revisited, right features were assigned to right persons etc etc. Full team was motivated to work in the upcoming successful product.

Days passed… somewhere after 6 months an internal release was planned for the functional team and for the stakeholders. Great all were happy, I and my team got many awards, good recognition in the company and got some suggestions (also known as changes) in the software. All changes were incorporated successfully with the new features parallel development.

All was going great, until after some 5 months (1 month left for delivery) some expert gave (very late) a very good suggestion to the management for future perspective. When we received the document of changes they were huge, major changes had to be done in many of the key classes. All teammates were sort of disappointed that their feature which was done with lot of hard work and interest would be corrupted. They started blaming the management, for such changes. But all cooperated since we were the experts and it would be shame to go against the changes expected.

Now started the actual impact, changes were planned, one change would affects other flow, dependency between the classes was more, poor commented code, testers started doing stress testing on the bug tracking tool by putting more and more bugs in it.All team was under stress, daily pig meetings were started to know the status. Day by day the deadlines were nearing and the features and bugs were poured more on us.

Finally we ended up not giving the full product in the expected deadline. The days of the awards and recognition were gone. For next week I and some senior members were very busy in answering what went wrong and why we dint reach the target. Since there was intense pressure from the marketing team the product was shipped to the customers who were waiting for it. There were still more changes pitching in and we were very much tensed how to incorporate those without creating regression bugs in our product. Many bugs were reported by the customer and fixing them was a huge task for us.

On one evening was not feeling to go home so stayed till night I thougth that I and my team were responsible for the bad code and we are the one going to fix those and show our potential. I started recollecting all the mistakes that were done by me and my team and started documenting it, further found some tips across the web and added those. After analyzing the entire problem I found angle of deviation that we had been from our actual goal and plans and listed all the faults that we had done. Made up my mind to speak to the management in the early morning regarding it and get some time to refactor and restructure the code. The following steps were planned to be executed with intense care.

  • Put proper comments
  • Fix coding standards and project conventions
  • Remove unnecessary code
  • Identifying and breaking up big classes
  • Breaking up big methods
  • Refactor existing code
  • Remove memory leaks
  • Restructuring
  • Write automated unit tests

After following each step properly with collecting the daily status from the team, sharing the problems and discussing some new solutions, finally it took around 7 months to complete the product.
Even though we were late by 8 months we had reached the goal properly and were ready to accept any further changes. From last year the product was sent to the market and there was very good response from our customers and the days of awards had begun again.
From next posts I am planning to go in depth of execution of each of the steps followed.

For any clarification please revert back.

Happy Coding
Naveen Prabhu 馃檪

Posted in C#, Solutions | Tagged: , , | Leave a Comment »

Aborting a suspended thread

Posted by codingsense on March 16, 2010

Hi,

I was working on a multithreading application and found an interesting thing on resuming a thread, so planned to share with you.

When a thread is running if we abort it, it gets stopped. But if a suspended thread is aborted then it gives an exception “Thread is suspended; attempting to abort”.

The below sample demonstrates it, first the thread is started, then suspended and then aborted. Run the application and check the error.




using System;
using System.Threading;

namespace ConsoleApplication1
{
class Program    
{
static void Main(string[] args)
{
Thread thread = new Thread(new ThreadStart(DoSomething));
thread.Start();

for (int Index = 0; Index < 10; Index++)
{
switch (Index)
{
case 4:
thread.Suspend();
Console.WriteLine("Thread suspended");
break;

case 6:
thread.Abort();
Console.WriteLine("Thread aborted");
break;

}

//Main Thread is made to sleep                
Thread.Sleep(300);
}

Console.Read();
}

private static void DoSomething()
{
while (true)
{
Thread.Sleep(300);
Console.WriteLine("I am busy doing work");
}
}
}
}


To overcome this exception we can use Resume to invoke the suspended thread and then abort.
Replace the case 6 with below code snippet.




case 6:
try                        
{
thread.Abort();
}
catch (ThreadStateException)
{
thread.Resume();
}
Console.WriteLine("Thread aborted");
break;


now running the application aborts the thread as expected.
From this sample we can learn that if we use resume on a suspended thread then it starts again, but if we have signalled for abort on that thread and try resume then it will stop the thread in proper manner.

Happy Learning
Codingsense 馃檪

Posted in C# | Tagged: | Leave a Comment »

Rounding off to nearest in C#

Posted by codingsense on February 4, 2010

Hi,

Here I am posting a simple post but its very useful and I saw lot of questions arising on the same. So made a sample to share with you.
There are 2 types of rounding off in the following sample
1) Rounding off to nearest 10, 100, 1000 and other multiples of 10
2) Rounding off to nearest decimal place

Rounding off to nearest 10, 100, 1000 and other multiples of 10



public static double RoundToNearest(double Amount, double RoundTo)
{
double ExcessAmount = Amount % RoundTo;
if (ExcessAmount < (RoundTo / 2))
{
Amount -= ExcessAmount;
}
else            
{
Amount += (RoundTo - ExcessAmount);
}

return Amount;
}


Rounding off to nearest decimal place




public static decimal RoundToNearestDecimal(decimal Amount, int DigitsToRound)
{
return Math.Round(Amount, DigitsToRound, MidpointRounding.AwayFromZero);
}


Sample usage of the above code





Console.WriteLine("Amount = 123456789");
Console.WriteLine(RoundToNearest(12345678, 10));
Console.WriteLine(RoundToNearest(12345678, 100));
Console.WriteLine(RoundToNearest(12345678, 1000));
Console.WriteLine(RoundToNearest(12345678, 10000));
Console.WriteLine(RoundToNearest(12345678, 100000));

Console.WriteLine();

decimal deci = Convert.ToDecimal(123.456789);
Console.WriteLine("Amount = 123.456789");
Console.WriteLine(RoundToNearestDecimal(deci, 1));
Console.WriteLine(RoundToNearestDecimal(deci, 2));
Console.WriteLine(RoundToNearestDecimal(deci, 3));
Console.WriteLine(RoundToNearestDecimal(deci, 4));
Console.WriteLine(RoundToNearestDecimal(deci, 5));


Sample output for the above code

Happy Learning
Codingsense 馃檪

Posted in C# | Tagged: , | 4 Comments »

GetDaylightChanges with TimeZoneInfo

Posted by codingsense on February 3, 2010

Hi,

A good feature given by microsoft in .Net 3.5 is adding new classes like TimezoneInfo, AdjustmentRule and TransitionTime.
Earlier we were able to get only the daylight changes of the timezone that is currently set on our machine using TimeZone.CurrentTimeZone and if we wanted other daylightchanges then we had to do lot of manipulation using Kernel.dll and access the registry values for each entry but now its readily available for us.

I had a requirement few days back to get the timezones and their next 5 years daylight start and end date and time, it just happened so easily with the given features.

The main thing that I had to implement is changing from transition time to DateTime and here is the function that I made to acheive the same.



public DateTime GetDateTime(int Year, TimeZoneInfo.TransitionTime transactionTime)
{
//Create a datetime to begin with 1st of the transition month           
DateTime dt = new DateTime(Year, transactionTime.Month,
1, transactionTime.TimeOfDay.Hour,
transactionTime.TimeOfDay.Minute, transactionTime.TimeOfDay.Second);

//If the dayofweek of 1st is same as the transition day then exit            
//otherwise             
if (dt.DayOfWeek != transactionTime.DayOfWeek)
{
//If transition dayofweek is greater than 1st dayofweek then we need to move further                
//Eg : Transition dayofweek is tuesday and 1st day of week is monday then we need to move 1 day ahead to point to                 
//the transition day                
if (dt.DayOfWeek < transactionTime.DayOfWeek)
{
dt.AddDays(transactionTime.DayOfWeek - dt.DayOfWeek);
}
else                
{
//else its not in the 1st week so we move 7 days ahead and move back again
                    dt = dt.AddDays(7 - (dt.DayOfWeek - transactionTime.DayOfWeek));
}
}

//Since we are already pointing to the first week of the transition date            
//Add remaining no of weeks to the datetime            
return dt.AddDays((transactionTime.Week - 1) * 7);
}


Sample source code 6Kb

By using the new classes we can get all the required information about the timezones accross the globe. Hope you found it useful.

Happy Learning,
Codingsense 馃檪

Posted in C# | Tagged: | 6 Comments »

Customize a slider in WPF, Step by Step Tutorial

Posted by codingsense on February 1, 2010

Hi,

Today let us explore how to customize a slider control in WPF. I have seen a lot of explanation and looked many articles on net about the slider, but i got the the better idea about it only when I implemented it in my own way.
Let me share the same step by step method with you all.

Download Source (7.8 Kb)

So lets begin, First create a WpfApplication in VS2008 and insert a slider control in the grid layout.Open the designer and click on the XAML view. You will see the below code.

<Grid>
<Slider Name="CusomSlider" Height="25" VerticalAlignment="Top" Margin="46,65,37,0" />路路路路
 </Grid>


Now we have to override the Style property of the slider to give it the look we want. We will name the style as MyCustomStyleForSlider and override the style method as below code,

<Slider Name="CusomSlider" Style="{StaticResource MyCustomStyleForSlider}" Height="25" VerticalAlignment="Top" Margin="46,65,37,0" />


Slider control is made up of many subcontrols, those include Track which inturn include Track.IncreaseRepeatButton, Track.DecreaseRepeatButton, Track.Thumb.
Thumb is the main sub control that makes the rectangle shape that moves left and right. The same thumb can also be found in horizontal and verticle scrollbars. So lets insert the following default template of all the controls that all together make the slider control.

<Style x:Key="MyCustomStyleForSlider" TargetType="{x:Type Slider}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Slider}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"><Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" MinHeight="{TemplateBinding MinHeight}"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TickBar x:Name="TopTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Top" Height="4" Grid.Row="0"/>
<TickBar x:Name="BottomTick" Visibility="Collapsed" Fill="{TemplateBinding Foreground}" Placement="Bottom" Height="4" Grid.Row="0"/>
<Border x:Name="TrackBackground"  
 BorderThickness="1" CornerRadius="1"
 Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >
<Canvas Margin="-6,-1">
<Rectangle Visibility="Hidden" x:Name="PART_SelectionRange" Height="4.0"
 Fill="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"
 Stroke="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}"
 StrokeThickness="1.0"/>
</Canvas>
</Border>
<Track x:Name="PART_Track" Grid.Row="1">
<Track.DecreaseRepeatButton>
<RepeatButton Command="{x:Static Slider.DecreaseLarge}"/>
</Track.DecreaseRepeatButton>
<Track.IncreaseRepeatButton>
<RepeatButton Command="{x:Static Slider.IncreaseLarge}"/>
</Track.IncreaseRepeatButton>
<Track.Thumb>
<Thumb x:Name="Thumb" Background="Black"/>                                        
</Track.Thumb>
</Track>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Now run the application and check how it looks and works, click on its right and left and a small dot i.e Thumb without any style moves right and left.


Now lets apply style to the thumb and name it as CustomThumbForSlider, below is the implementation.

<Style x:Key="CustomThumbForSlider" TargetType="{x:Type Thumb}">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Ellipse Fill="#FF8F4040" Stroke="#FF000000" Height="15" Width="15"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


and override the above style to the thumb.

<Thumb x:Name="Thumb" Style="{StaticResource CustomThumbForSlider}" Background="Black"/>


After overriding the Thumb style lets have a look of the slider control,

Great, i.e The important thing that makes the slider control is the IncreaseRepeatButton and DecreaseRepeatButton, when clicked on ends they decrease in the size to the amount of the Tickbar value making the other button grow in size and that gives us the illusion that the thumb is moving. Nice move right ok lets get furthur.

Now we need to hide the Increase and decrease repeat buttons so lets create their styles.

<Style x:Key="SliderRepeatButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


Ok apply the styles to the repeat buttons and run the application, you will see that we have only the thumb that moves left and right on clicking but we dont see any track. So now we shall give border control a proper background and brush so that we get a good look at the starting and ending of the slider control. Lets create a style for both of these properties and apply them to the border.

<SolidColorBrush x:Key="HorizontalSliderTrackNormalBackground" Color="#FFE7EAEA"/>
<LinearGradientBrush x:Key="HorizontalSliderTrackNormalBorder" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFAEB1AF" Offset="0.1"/>
<GradientStop Color="White" Offset=".9"/>
</LinearGradientBrush>
<Border x:Name="TrackBackground" 
 Background="{StaticResource HorizontalSliderTrackNormalBackground}"
 BorderBrush="{StaticResource HorizontalSliderTrackNormalBorder}"                                        
 BorderThickness="1" CornerRadius="1"
 Margin="5,0" VerticalAlignment="Center" Height="4.0" Grid.Row="1" >


Now run the application, you can see that we have customized the design of our slider control.

Change the ellipse control or the border styles to whatever you desire and you will be able to create your own design for the slider control.
Change some of the properties of tickbar, thumb and repeat buttons and see how they behave, which will give you more hands on with slider controller.

References:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.thumb.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.tickbar.aspx

Happy Learning,
Codingsense 馃檪

Posted in C#, WPF | Tagged: , , , | 17 Comments »

ListBox with Tooltip for each item

Posted by codingsense on January 27, 2010

Hi,

Currently I had a requirement to show tooltip for each item of the listbox control becuase, if any item lenght was exceeding the width of the listbox then it was not fully shown and i dint wanted to enable the horizontal scroolbars. So just googled for the solution and got some which would display the tooltip at the bottom of the mouse pointer itself, I found it bit annoying to the user and decided to show the tooltip starting from the right end of the listbox.

Here is the control that I created using the normal Listbox control.

Download Source Code (9.34Kb)

In this control I Overridden 3 events of the Listbox control:

  • OnMouseEnter : Here the tooltip will be Initialized
  • OnMouseLeave : Here the tooltip will be disposed
  • OnMouseMove : Here the tooltip will be shown

Here is MouseMove event,

protected override void OnMouseMove(MouseEventArgs e)
{

base.OnMouseMove(e);

//Get the index of the item on which mouse is hovering
int ListBoxItemIndex = this.IndexFromPoint(e.X, e.Y);

//If an item is present at that index then only the tooltip can be showed
if (ListBoxItemIndex > -1 && ListBoxItemIndex <= this.Items.Count)
{

//Get the string of the item
string Tip = this.Items[ListBoxItemIndex].ToString();

//If already tip of that item is shown then exit
if (Tip != CurrentTip)
{

//Get the rectangle of the item
Rectangle rect = GetItemRectangle(ListBoxItemIndex);

//Show the tooltip starting at the right border of the listbox
// and at the height of the rectangle

ItemToolTip.Show(Tip, this, this.Width, rect.Y);

//Make the tip as current tip
CurrentTip = Tip;
}
}
}


For any clarifications please revert back.

Happy Learning,
Codingsense 馃檪

Posted in C# | Tagged: | 1 Comment »

Performance difference between For and Parallel.For

Posted by codingsense on October 17, 2009

Hi,
I was really happy when i heard of the concept of Parallel.For that is introduced in new version of .Net. I tried it in framework 4.0 in visual studio 2010.
Here is the sample that i ran to see how the performance will vary on my system ( Core 2 duo).

class Program
    {
        static void Main(string[] args)
        {
            long[] a = new long[1000000];

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            Parallel.For(0, 1000000, delegate(long i)
            {
                a[i] = i * i;
            });

            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);

            watch.Start();

            for (long I = 0; I < 1000000; I++)
            {
                a[I] = I * I;
            }

            watch.Stop();
            Console.WriteLine(watch.ElapsedMilliseconds);
            Console.Read();
        }
    }

What i saw is always there is a differnce in value depending on the work load on each core. But to my surprise i found that in all the 10-15 cases that i ran, for loop took less time to execute than Parallel.For.
I think there might be some other things that needs to be done to make use of Parallel.For to make it more efficient than For or there might be some specific tasks where Parallel.For should be used.
Will try to find it out and get back to you soon.

Naveen Prabhu

Posted in C# | Tagged: | Leave a Comment »