#Google Analytic Tracker

Pages

Dec 16, 2009

Event Handler and Memory Leaks

I have been trying to find the time write a post about event handler that causes memory leak. Finally I find the time to write something about.

.NET has Garbage Collect == No Memory Leaks?

Of course NOT. Even in the modern language like C#, we still have to worry about memory leak.

Possible Causes of Memory Leak

  • Failed to close or clean up unmanaged code
  • Created static object that isn’t being use after its first use
  • Failed to dereference event handler in certain situation. (i.e. you did the += handler, but not the –= handler)

Why you need to do –= Event Handler?

This time, I want to explicitly talk about the important of dereference event handler.  It is a common mistake that developers forget to do.

In most cases, you don’t usually need to perform dereference your event handler. For example:

   1: public class Class1
   2: {
   3:     private EventHandler SomeEvent;
   4:  
   5:     public Class1()
   6:     {
   7:         SomeEvent += SomeEventHandler;
   8:     }
   9:  
  10:     private void SomeEventHandler(object sender, EventArgs args)
  11:     {
  12:         // Some something
  13:     }
  14: }

When Class1 is disposed, the entire class is disposed, therefore you don’t need to worry about dereferencing class1’s event handler.

However, if your event handler is attached to an external object, you should dereference your event handler when your class is disposed. For example, the following causes a memory leak:

   1: namespace EventHandlerTest2
   2: {
   3:     public class Program
   4:     {
   5:         public EventHandler SomeEvent;
   6:  
   7:         static void Main(string[] args)
   8:         {
   9:             Program program = new Program();
  10:             for(int i = 0; i < 10; i++)
  11:                 program.Run();
  12:         }
  13:  
  14:         public Program()
  15:         {
  16:             CreateUselessHelper();
  17:         }
  18:  
  19:         public void Run()
  20:         {
  21:             if (SomeEvent != null)
  22:                 SomeEvent(this, null);
  23:         }
  24:  
  25:         private void CreateUselessHelper()
  26:         {
  27:             //Create a Helper class locally, 
  28:             //will this object stick around after this function exit?
  29:             HelperClass helperClass = new HelperClass(this);
  30:         }
  31:     }
  32:  
  33:     public class HelperClass
  34:     {
  35:         public HelperClass(Program program)
  36:         {
  37:             program.SomeEvent += SomeEventHandler;
  38:         }
  39:  
  40:         private void SomeEventHandler(object sender, EventArgs args)
  41:         {
  42:             Console.WriteLine("I am still around!");
  43:         }
  44:     }
  45: }

In the above code, I created a HelperClass object in the CreateUslessHelper() method call. Typically, you would think that once this method is completed, the HelperClass is garbage collected. Unfortunately, in this case, it does not get garbage collected. When the HelperClass.SomeEventHandler attached to Program.SomeEvent, it causes the Program object to reference HelperClass. As a result, the Helper class will stick around until the Program object get garbage collected.

Solution

An appropriate way to clean up this event handler is simply implement IDispoble in the Helper Class. In the dispose method you simply dereference the event handler.

   1: public class HelperClass: IDisposable
   2: {
   3:     private Program _program;
   4:     public HelperClass(Program program)
   5:     {
   6:         _program = program
   7:         _program.SomeEvent += SomeEventHandler;
   8:     }
   9:  
  10:     private void SomeEventHandler(object sender, EventArgs args)
  11:     {
  12:         Console.WriteLine("I am still around!");
  13:     }
  14:  
  15:     public void Dispose()
  16:     {
  17:         _program.SomeEvent -= SomeEventHandler;
  18:     }
  19: }

Here is a example that I wrote which demonstrates the effect of memory leak if event handlers are not dereferenced.

Memory Leak Example (This is my first time trying to use a file hosting service)

Dec 6, 2009

Upgrading From XP to Windows 7 does not require XP Product Key?!?!

After using Windows 7 Ultimate RC for almost half a year, it was about time to install the production version. I had received the Windows7 Professional Upgrade package from BestBuy from my pre-ordered in July. I got it for about $129CND, which is a 50% off promotion.

Since this was an upgrade, I expected Windows 7 installation would require me to install Windows XP or at least has the Windows XP CD installation disk ready.

After using BootCamp to recreate a new partition, I restarted my Mac and began install Windows 7. By the way, I had to clean up the Mac partition, so that BootCamp could split the partition.

Here was the problem I encountered, when I tried to type in my Product Key for Windows 7, it failed to validate it.  After retyping it a couple of times, I gave up and google for solution.

Fortunately, the first entry of my search results gave me the solution, which was to install Windows 7 without the typing in the product key.

http://social.answers.microsoft.com/Forums/en-US/w7install/thread/a7a018fa-17b8-4a27-9657-52ba4d178e87

So, that what I did. I installed it without the product key. Once I am in the desktop, I used Windows Search and open the Windows Activation screen.

I typed in the product key again, and it passed.

Hmm… strange, why it didn’t ask for my Windows XP Key???

After I did a search, it turned out that I may have by pass the procedure of installing Windows XP. Legally speaking, you still need a legit copy of Windows XP, however, with the by pass, it looks like you may not really need your Windows XP or Vista installation disk.

Just a side note, some local computer stores in Calgary or Edmonton are selling Windows 7 Professional OEM 64bit for about $149, which is reasonably inexpensive. If you are a student, you can get the Windows 7 Home edition for $40CND.

Nov 6, 2009

Why can’t MS make SQL2008 Installation Easier + Failed to register SQLDMO.DLL

I rarely write blog to complain about something. I would rather like to share useful information instead.  But this time, I got so frustrated installing SQL2008.

Originally, I have Windows 7 installed with SQL2008 Server. My company has an product that require a file call “SQLDMO.DLL” to be register. (i.e. regsrv32 sqldmo.dll). Unfortunately I kept getting an –2147024770 error.

After trying everything, I decided to uninstall SQL2008 Server and hopefully by reinstalling it would let me register sqldmo.dll.

Btw, I turned off my firewall and UAC just in attempt to register sqldmo.dll…. but nothing works.

Originally, I installed SQL2008 from a MSDN CD follow by an SP1 installation. My colleague suggested me to install SQL2008Express install from one of our company shared directory. So this is what had happened.

  1. Install SQL2008Express from company network
  2. Complain about Windows 7 compatibility issue, click continue
  3. Go online, cannot find SQL2008Express SP1 upgrade, instead, just the full SQL2008Express SP1 installation
  4. Uninstall install existing SQL Server just to be safe.
  5. Download and install SQL2008Express from here
  6. Installation works, but can’t find SQL Management Studio
  7. Download and install SQL Management Studio from here
  8. The setup screen didn’t say anything bout Management Studio, install, it is the same screen as regular SQL Express installation wizard.
  9. Try install it, failed, because I have SQL installed
  10. Find another download link: http://www.microsoft.com/express/sql/download/
  11. Try to install “Runtime with Management Tools”
  12. Cannot install since I have SQL installed
  13. Try install “Management Tools”
  14. Complain about Windows 7 compatibility issue, ignore it
  15. Finally, I got SQL Management Studio

I downloaded total of over 2GB of installation files, took me to whole morning to do it. Yet, I still cannot register sqldmo.dll.

Solution

Ultimately… it turns out that all I needed to do is install SQLDMO from SQL Server 2005 Backward Compatibility components. Yikes, wasted 1/2 of my day.

Oct 23, 2009

SQL Server – Login failed for user: Error 18456

Every once in awhile I need to reinstall SQL Server. Every time after I reinstall SQL Server and try to run my project executable, I would run into a connection error which says I cannot login with my specific user name.

First – Ensure your Login user is Added

The first thing I would do is to ensure my restored database has the right user.  In fact, it has to be a user from the SQL Server security group, and not the one from the Database security group.

In the following example, the database apts_dev4.0 has a user name call “MentorStreetsDBUser”. Under the DICKYS2\SQLEXPRESS security, it also has “MentorStreetsDBUser”. However, these two users are not the same at all. The one in the database is created by my previous SQLServer. This logon will not work for the newly installed SQLServer.

image

For this part, I simply remove the MentorStreetsDBUser from the database, and add a new user by specifying MentorStreetsDBUser from the SQL Server. This way I should able to logon using this account name. Make sure you give this user the proper security rights.image

But Wait… I still get a Login Fail message with this user!

Second – Ensure SQL Server Authentication Mode is On

This is the part where I usually forgot to do.

Right click on the SQL Server select Property. Click the Security section. You will see the following:

image

In the Server authentication, select both “SQL Server and Windows Authentication mode”

By default, SQL Server only allows Windows Authentication login, and not the SQL Server login. After restart SQLServer, everything will work again.

Conclusion

First, I would hope that SQL Server Management would at least so a warning letting me know that the imported database user would not able to log on.

Second, I think that MS SQL Server should default the Server authentication to both the “SQL Server and Windows Authentication Mode”.

In addition they should provide better error message. I personally don’t see a big security issue if you at least tell the user that "SQL Server Mode Login is not supported”.

Aug 4, 2009

IEnumerable + Linq - Be cautious when you do evaluation the enumerable object

From the last two posts, I emphasised that the evaluation is done each time you access the elements in enumerable object. Because of this, you have to be cautious in the two following situations.

  1. Where clause condition in Linq is modified
  2. Your collections is modified when performing Linq statement

Where Clause has changed example

// keep track on the last record highest mileage
private int _highestMileageRecorded = 10000;

private void GetStatistics(IEnumerable<Vehicle> dataSource)
{
    //Find vehicle with highest mileage compare than the record
    IEnumerable<Vehicle> vehicleExcessHighestMilage
        = from v in dataSource
          where v.Mileage > _highestMileageRecorded 
          select v;

    //Record the highest milage
    _highestMileageRecorded = 
         vehicleExcessHighestMilage.Max(v => v.Mileage);

    Console.Write("Vehicle excesses the records:");   
    foreach (var vehicle in vehicleExcessHighestMilage)
    {
        Console.Write(vehicle.Id + " ");
    }
}

The above code does not work! It will never print out any vehicle id.

The reason is that the variable _highestMileageRecorded has overwritten before I perform the foreach. Remember, this IEnumerable result depends on the current state of Linq statement.

Collection has changed example

Consider the following code: The following code try to correct vehicle that contains incorrect mileage data. Notice that a cache object return a IEnumerable of vehicle that has incorrect mileage. The cache also update itself if any vehicle data changes.

[Test]
public void Test()
{
    List<Vehicle> oVehicleList = new List<Vehicle>();
    GenerateRandomSales(oVehicleList);
    ClearIncorrectMiliage(oVehicleList);
}

private void ClearIncorrectMileage(IEnumerable<Vehicle> dataSource)
{
    VehicleCache vehicleCache = new VehicleCache();
    vehicleCache.LoadData(dataSource);

    IEnumerable<Vehicle> incorrectMileageVehicle = 
        vehicleCache.GetIncorrectMileageVehicle();
    
    foreach (var vehicle in incorrectMileageVehicle)
    {
        //Fix the data
        //Note: Exception occurs 
        vehicle.UpdateMileage(0);
    }
}

private class VehicleCache
{
    List<Vehicle> m_oIncorrectMileageList = new List<Vehicle>();

    public VehicleCache()
    {
        Vehicle.Updated += VehicleUpdated;
    }

    void VehicleUpdated(Vehicle vehicle)
    {
        if (vehicle.Mileage < 0)
            m_oIncorrectMileageList.Add(vehicle);
        else
            m_oIncorrectMileageList.Remove(vehicle);
    }

    public IEnumerable<Vehicle> GetIncorrectMileageVehicle()
    {
        return m_oIncorrectMileageList;
    }
   
    public void LoadData(IEnumerable<Vehicle> oDataSource)
    {
        m_oIncorrectMileageList.AddRange(oDataSource.Where(v => v.Mileage < 0).Select(v => v));
    }
}

If you read run this piece of code, you will most likely get a System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

Because the cache try to update itself whenever the data change, it also causes trouble when the code modify the data in a foreach statement. In fact, this could easily occur in a multi-thread application where the cache get modified when other modules looping though the cache. Be sure you lock your collection, or convert the IEnumerable to an list or array before performing operations that affect the cache.

Although the above example seems obvious, it may not be obvious enough in a real application where a developer may not know other module behaviour. Imagine developer A wrote the Cache module, and you got an exception when you use its IEnumerable result. Without checking the code, you may never realized you are modifying the cache indirectly.

Did you increase or decrease your application performance using IEnumerable in Linq (Part 2)?

Last time I talked about how you can improve your application performance by using IEnumerable so that no calculation is performed until you need it.

Bad Performance

Now, consider the following code:

private void GetStatistics(List<Vehicle> totalSaleList)
{
    IEnumerable<Vehicle> toyota
        = from v in totalSaleList
          where v.Make == Make.Toyota
          select v;

    int totalSales = toyota.Count();

    int thisYearSale = 
        (from v in toyota
         where v.Year == 2009
         select v).Count();

    double avgMilage = 
        (from v in toyota
         select v.Millage).Average();

    Console.WriteLine("Total Sales: {0}. This year sale: {1}. Average milage: {2}", totalSales, thisYearSale, avgMilage);
}

Notice that in the above example, I kept the variable "toyota" as an Enumerable. When I try to calculate the “totalSales”, “thisYearSale” and “avgMilage”, the querying always need to re-evaluate the first Linq statement. This is obvious a waste of cpu power. 

Better Performance

Ideally, you should do the following:

private void GetStatistics2(List<Vehicle> totalSaleList)
{
    Vehicle[] toyota
        = (from v in totalSaleList
          where v.Make == Make.Toyota
          select v).ToArray();

    int totalSales = toyota.Length;

    int thisYearSale =
        (from v in toyota
         where v.Year == 2009
         select v).Count();

    double avgMileage =
        (from v in toyota
         select v.Mileage).Average();

    Console.WriteLine("Total Sales: {0}. This year sale: {1}. Average mileage: {2}", totalSales, thisYearSale, avgMileage);
}

By converting the variable “toy ota” into an array, the above code does not need to re-evaluate “toyota” each time when we access the enumerable.

Even Better Performance

If performance is a must, you should try to combine calculations in as less number of loop as possible. Remember, each time you call Linq extension method (i.e. Max(), Min(), Average()), it actually has to loop though your enumerable object to calculate the result.

private void GetStatistics3(List<Vehicle> totalSaleList)
{
    Vehicle[] toyota
        = (from v in totalSaleList
           where v.Make == Make.Toyota
           select v).ToArray();

    int totalSales = toyota.Length;

    int thisYearSale = 0;
    int totalMileage = 0;
    foreach (var vehicle in toyota)
    {
        if (vehicle.Year == 2009)
            thisYearSale++;
        totalMileage += vehicle.Mileage;
    }

    double avgMilage = totalMileage * 1.0 / totalSales;

    Console.WriteLine("Total Sales: {0}. This year sale: {1}. Average milage: {2}", totalSales, thisYearSale, avgMilage);
}

Conclusion

When you need to performance more calculation on the same enumerable object, it would be wise to convert it to a list or an array first before further processing.  This will reduce the time of re-evaluate the enumerable result.

Did you increase or decrease your application performance using IEnumerable in Linq (Part 1)?

I love Linq, there is no doubt about that. I pretty much use Linq whenever is appropriate. Linq simply makes my code much more readable and easier to understand. Another reason is the power the extension methods the Linq library provide for IEnumerable<T> object.

In an older post, I mentioned a bit about IEnumerable and how it works. This time, I am going to talk a bit more about how you can use IEnumerable to help improve your application.

Good Performance using IEnumerable?

Consider the following code:

private bool IsToyotaTheHighestSale2(List<Vehicle> totalSaleList)
{
    int toyotaSale =  (from v in totalSaleList
          where v.Make == Make.Toyota
          select v).Count();

    int hondaSale = (from v in totalSaleList
          where v.Make == Make.Honda
          select v).Count();

    int bmwSale = (from v in totalSaleList
          where v.Make == Make.BMW
          select v).Count();

    //Compare the result
    if (toyotaSale > hondaSale)
        if (toyotaSale > bmwSale)
            return true;

    return false;
}

In the above example, we use Linq to get a count of all the car sales, follow by compare toyta sale with honda and bmw sale.

Now, let check the following:

private bool IsToyotaTheHighestSale2(List<Vehicle> totalSaleList)
{

    IEnumerable<Vehicle> toyota 
        = from v in totalSaleList
          where v.Make == Make.Toyota
          select v;

    IEnumerable<Vehicle> honda 
        = from v in totalSaleList
          where v.Make == Make.Honda
          select v;

    IEnumerable<Vehicle> bmw 
        = from v in totalSaleList
          where v.Make == Make.BMW
          select v;

    //Count all the list first
    int toyotaSale = toyota.Count();

    //Compare the result
    if (toyotaSale > honda.Count())
        if (toyotaSale > bmw.Count())
            return true;

    return false;
}

Comparing IsToyotaTheHighestSale() with IsToyotaTheHighestSale2(), which one would perform faster? The answer is the IsToyotaTheHighestSale2().

Why? When you use Linq and get an IEnumerable there is actually no calculation, until you act on the IEnumerable. In IsToyotaTheHighestSale2(), if toyota sale is not higher than honda sale, the function does not need to calculate bmw sale. This is why the latter function run faster in general case.

Of course, above is only a very simple example. Imagine that you need to execute a function tens of thousands of times, and your Linq statement is a lot more complicated than the above example. Acting on the Enumberable later in the logic would save you running unnecessary code.

Conclusion

You can improve your application performance by acting on the IEnumerable object right when you need it, instead of getting the result in the early stage of your logic.

However, there are another cases when you should not use IEnumerable, which I will cover in the next blog.

Aug 3, 2009

PreCode Setup Testing

This blog is merely my PreCode setup test. It took me a long time to setup the PreCode because I did realized the preview function on Blogger, and the Live Writer Preview does not execute JavaScript. I kept wondering why the code setup doesn’t work until I actually try to publish a post.

Here are two examples on how the Syntax Highighter works:

JavaScript Example:

 function doNothing()
 {
   var a = 1;
   var b = 2;
   var nothing = a*b;
   // alert(nothing);
 }

C# Example:

public static string GetHelloMessage()
{
    var a = "Hello";
    var b = "World";
    
    var someArray = (from p in SomeArray
                     where p is Dat
                     select p).ToArray();

    return string.Format("{0} {1}", a, b);
}

What is PreCode

Precode is a code snippet highlighter plug-in for Windows Live Writer. It uses an open source JavaScript syntax highlighter that format your code in HTML.

This plug-in works by adding a tag <pre> for the code that you want to insert.

http://www.codeplex.com/precode

Screenshot:

image

After you insert your code, you will end up with something like the following:

<pre class="brush: csharp; gutter: false; toolbar: false; smart-tabs: false;">public static string GetHelloMessage()
{
    var a = &quot;Hello&quot;;
    var b = &quot;World&quot;;
    
    var someArray = (from p in SomeArray
                     where p is Dat
                     select p).ToArray();

    return string.Format(&quot;{0} {1}&quot;, a, b);
}</pre>

Since Precode uses Javascript, when you add test your setup in Windows Live Writer, or Blogger, it won’t show the formatted text. You will have to publish your blog to see if it works or not.

Note: I couldn’t get the PreCode to work properly in WindowsXP. The “Surround With” dropdown appears behind the main form. Maybe this is a WPF issue, but who knows, unless someone look into this.

How to set your Blogger to use PreCode

There is two parts:

1. Download and install PreCode

2. Setup your Syntax Highlighter

It took me awhile to get the JavaScript right. I am not an expert in JavaScript. However, this is what I did:

In the blogger settings, goto Layout Tab –> Edit HTML. In your Edit Template, add the following code in your html <head> section and save your template:

<script language='JavaScript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'>
</script>
<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shCore.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCSharp.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushXml.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushAS3.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJScript.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCss.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCpp.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJava.js'/>

<script language='javascript'>
$(document).ready(function () {
  $(&quot;pre br&quot;).after(&quot;\n&quot;).remove();
  SyntaxHighlighter.config.clipboardSwf = &#39;http://alexgorbatchev.com/pub/sh/2.0.320/scripts/clipboard.swf&#39;;
  SyntaxHighlighter.all();
});
</script>

Notice that in the above Javascript, I am referencing the js files from alexgorbatchev.com domain. You can definitely put these js files somewhere else. In fact, you can modify these javascript files to have additional keywords.  In the above example, I didn’t include all the available syntax highlighters. Go to the following link for the others.

http://alexgorbatchev.com/wiki/SyntaxHighlighter:Brushes 

References

Thanks to the following links, I was able to set up my blog using Precode:

http://www.codeplex.com/precode

http://ersinbasaran.blogspot.com/2009/07/code-syntax-highligthting-on-bloggercom.html

Jul 2, 2009

More Windows 7 Experience

Wow, I haven’t write any useful blog for awhile. Originally, I was thinking about writing about Linq. However, I realized there are too many Linq related article out there.

Anyways, after using Windows 7 RC for quite some time on my Mac, one major positive thing I notice is the compiling speed of my work (VS) solution, compare with XP. Of course, XP limited you 2GB of addressable memory. Even with the 3GB hack in the boot.ini, which for some reasons killed my video on my new Core Duo 2.8GHz with 3GB machine from Dell, my Mac 2.4GHz with 2GB with Windows 7 still compile a lot faster. :)

Issues:

Well, so far, I encounter 2 freezes, with no blue screen, and I can’t find any system error message in the log file.

And here is the latest issue I found, see if you can see the problem below

image

I found that funny, notice the start menu is off.

Apr 17, 2009

Windows 7 - Problem Steps Recorder (psr.exe)

This tool maybe a godsend for the software developers.  It enables the end users record their problems. This helps a lot because customers aren’t usually good when expressing their software problem:

image_thumb[19]

Here is how it works, you click on the “Start Record” button, and you try to reproduce the problem of your application. Isn’t it simple! Once you get to the problem, you can click “Stop Record” to end the record. You can also “Pause” if you need to do something else in the middle. It will create a zip file that contains a .mht (MIME HTML). That’s easy!

Once you open the .mht in your web browser, you would see something like this:

image

Basically, the .mht files contains screenshots for each user actions. Notice that there is a green border around the Windows Live Writer, because this the current window user is interacting.

Each screenshot comes with a description of what the user was doing. For example:
Problem Step 9: User left click on "OK (push button)" in "Options"

At the end of the file, you will see this detail description:
image

The details contains all of your user actions. This definitely going to help us developers a lot!

Windows 7 - Application Compatibility with Shims

One of the biggest concerns when user migrate from one OS version to a new version is program compatibility. Since I am not a Vista user, I don’t know what is would be like if I migrate from XP to Vista.

However, today, I learned a great tool that come with Window 7. It is called the Application Compatibility Toolkits. You should able to find this tool under the “Microsoft Application Compatibility Tools” folder.

image

Microsoft has come up with a lot of “Shim” to help old applications run in Windows 7.

In addition, Windows 7 has this mini-database that contains a great number of applications (5832 apps when I looked at the 6.1.7000 build) that have tested, and verified that can be run in Windows 7.

Here is an screenshot of the Shims Windows 7 has for “Command and Conquer – Red Alert 2”. It is interesting to the see the applications that Windows 7 team has chosen to test so far.  The shims are displayed on the right panel (i.e. EnulateSlowCPU, VirtualzeHKCRLite, etc…)

image

I bet the list will continue to grow before Windows 7 is released and afterward. However, if you happen to have an application that doesn’t run properly in Windows 7, you can run Standard User Analyzer tool to figure out what you need to get your application running.

image

Once you specified the execution path for your application, you can click the Migration menu item. You should see something like the following:

image 

The analysis will show a list of suggested shims that you can apply to your application if you start your application.

There are 353 Shims (Compatibility Fixes) in the build 7000.

image

In addition, it comes with 46 Compatibility Modes, each contains a set of Compatibility Fixes you can apply to your application.

image

Once Windows 7 come out, we will see how good these shims really work.

For developers, one big reason why most of the programs failed to run in Vista, or possible Windows 7 is because they hardcoded Windows Version number check for enable/disable program features. It may not even run if the version is not correct.  MS suggests we should avoid checking windows version number when developing your applications. Another thing to note is that Windows 7 version number is 6.1, not 7. That’s because MS hopes user can run Vista (6.0) programs under Windows 7. So, be prepare to update your application to run in Windows 7!

UAC in Windows 7

From what I know about Windows Vista, you can only turn the UAC (User Access Control) either on or off.  I definitely believe most of the developers and advance user would have turn it off completely. Now I am thinking about this Apple commercial

Fortunately, Windows 7 has fine tuned the option in UAC. There are now 4 levels of UAC you can set.

image

1. Always notify me when:

  • Programs try to install software or make changes to my computer
  • I make changes to Windows settings

2. Default - Notify me only when program try to make changes to my computer:

  • Don’t notify me when I make changes to Windows settings

3. Notify me only when programs try to make changes to my computer (do not dim my desktop):

  • Don’t notify me when I make changes to Windows settings
  • Not diming the desktop might allow programs to interfere with the User (i.e. automatically click Yes for your dialogue box)

4. Never notify me when:

  • Programs try to install software or make changes to my computer
  • I make changes to Windows settings

After using Windows 7 for a couple of days, the default settings feel like the security set up in Mac OS X.

Administrative Account Changes

In the past, we all NEED to be in the Administrative group to do pretty the simplest task in Windows. Now, it is a bit different.

When you are in Administrative group, you run as Standard User.

The idea is that administrator should not need administrative right during normal use of Windows. So what’s the different if you are in Administrative group?

  1. Standard User – when you need to administrative right, you will required to type in an admin password.
  2. Administrative User – You just need to click Yes/No to confirm that you want to get your administrative right.

This is probably more security than simply having administrative right all the time. You will be more conscience when the program asks you for administrative right. Saying that, us developers will need to more work. Our software should requires the minimal right to run your program, unless your program needs administrative right.

Apr 16, 2009

My First Windows 7 Experience

For awhile, I had been avoiding of trying out new OS. Windows XP works so well that I rarely care about other OSs. Until last year, one of my colleges, who is more or less a Mac fanatic, talked me into getting a Mac.  After many months of indecision, I finally brought a MacBook Pro, and got to experience what is like to use a Mac.

Mac OS X has many nice features that I wish to see in XP, but at the same time, I struggled to understand some of the UI design  philosophies behind Mac OS X. I will talk about this concern in another time.

Of course, there is Windows Vista. Its bad reputations on the medias deter me from try it.  I tried it one time, and the UAC pops up for pretty much for everything I do. Even opening your Control Panel would require your permission.

This week, I got a rare opportunity learn about Windows 7 in a 3 days training session. Yes, this is for software developers! There are hand-on-labs to do and you get to try to do some programming with the Windows 7 features.

So far, I like Windows 7 beta a lot. Here are some of the things that I like.

  • Taskbar
  • Libraries and Federated Search
  • Background Services and Tasks

In addition, the session also covers Power Management, Multi-Touch and Ink, and the obvious the Windows Ribbon (Office Ribbon).

Snipping Tool

But first, I have to mention about this tool that I find in Windows 7 (Ultimate), the Snipping Tool.

image

This tool obviously does more than your typical “Alt-Print Screen”. You can do capture in these 4 modes:

  • Free-form snip – allow you to draw around an area
  • Rectangle snip – a rectangle screen capture
  • Windows snip – allow you to select a windows and do a screen capture on it
  • Full screen snip

In addition, you can draw and highlight the captured screenshot.

image

Taskbar

One major improvement that I find in Windows 7 compare with XP is the Taskbar

image

  • Icon display only, no text, it saves you space
  • Activate your program right from the task bar. There is no quick launch.
  • All related windows are hidden in a single program icon, it only shows the running and pinned programming.

So, how do you see your opened Windows? You can simple hover your mouse pointer over the executing program on the taskbar and it will display all it related windows in a thumbnail.

image

In terms of programming, you can dictate what you want to display.

Preview

The taskbar get even better, than you put your mouse on the program thumbnail, all the other windows will become glass, leaving only your selected window.

image

In addition, you can close your windows with the little red cross button on the preview screen.

Notice that if you have multiple windows open, you can see the icon look like a stack:

image

Other things including the following also make the taskbar attractive:

  • Thumbnail Toolbars
    image
  • Progress bar icon
    image
  • Jump List- right click on the icon
    image

The item in the Jump List and Thumbnail can be customized though Windows 7 API.

Ribbon

Ribbon from Office 2007 has comes to Windows7. Both Ribbons look very similar. My question is why not just rip the Ribbon out from Office.

Windows 7 Paint:

image

Windows 7 Word Pad:

image

Oh you can’t use Ribbon in Windows XP. Your application will not be able to run under XP if you have Windows Ribbon. For developers, this make our life a lot tougher. You either need have 2 different version of your program, or buy a third party ribbon that works in Windows XP.

Libraries

Another feature that I like is the Library feature. The concept is similar to Unix’s links. This is like a virtual folder where you can put whatever you want in it.

The idea is that you can use the library to “collect” other folders’ contents. For example, if I have a number of folders storing pictures, I can add these locations to the library and I can get a single view of all my pictures. This is similar to your Windows Media Player, or iTune where it scans all your music, videos and pictures into an organized folder view.

Libraries and Preview functions:

image

You can also create your own libraries:

image

One thing to note, my college and I have some concerns where the users may get confuse on how the folder system works. Note that if you delete the file from the library, it will delete it from your harddrive, which is not the same as a link.  We will have to wait out the public feel about this feature.

UAC

One thing I notice that that Windows 7 is not as annoying as Vista. UAC only shows up when I try to run program in administrative mode, or when I install new programs. Opening your Control Panel doesn’t need your UAC permission, yeah!

Boot Time

Another thing I should mention is Windows boot time significantly faster because:

Less start up background service – Service that not needed are removed.

Delayed Auto Start service – Service that doesn’t need to start at boot time.

Service can start up by Trigger – A service doesn’t start up unless an event occurs.

These strategies help Windows 7 Start up faster.

Software Development

I never do any COM and unmanaged programming (C++) in Windows. Fortunately, Windows 7 SDK will comes with managed code for me to do Windows 7 programming in C#.

However, this SDK is not the final product from Windows 7, so use it at your own risk. .NET 4.0 should include the API needed to program in Windows 7.

Conclusion

There is a lot I want to talk about Windows 7, but I will probably stop here for now.  In some ways, I feel that MS took a number of UI design idea from Apple.  However, if it works, why not use it.  One of the things I learn from the training is that programming in C++ (unmanaged) is a lot more troublesome compare with C# (managed). Am I glad I am working in C#!

Mar 13, 2009

The Data Binding, the Invoke, and the Deadlock

When it comes to Windows Application programming, there are a number of things to be considered.

  1. Data Binding – the ability for the windows control to automatically refresh its UI when the underlying data changes. This feature saves developer’s time from writing code to update the UI when the underlying data change.
  2. UI Thread Invoke – When an UI needs to be updated, the updating code has be execute by original thread that create the UI (i.e. UI Thread, or usually the Main Thread). For example:
private void OnDataRowChanged(DataRow oDataRow)
{
if (this.InvokeRequired)
{
this.Invoke(new OnDRChangedDelegate(OnDataRowChanged), oDataRow);
return;
}

//Do Your UI Update ...
}
  1. Deadlock – In a multiple threaded application, when thread A waiting to acquire a resource and never able to get it because thread B is holding this resource, while it is waiting for thread A to release its resource. You will end up having two threads waiting and your program hangs. This can happens for more than two threads locking each other resources.


    image

 

The Problem

In a multi-threaded application, there are many threads interacting with the application data. In my case, it is a DataSet. A DataSet is an ADO.NET (old) technology that acts like a in memory database. It contains tables, columns, constraints, relationships and etc. To ensure the program is thread safe, we have to lock the DataSet before modifying the data using the lock keyword in C#.

This all sounds good until we start doing data blinding. In my case, I have a number of grids (Infragistic UltraGrid) blinded to the DataSet. In the beginning, everything look great. Data are loaded by thread to the data model and grids automatically updated.

Here is the model that causes problem:

image

Each time when our server updates a piece of data to a client DateSet, it creates a .NET remoting thread (represent by the green), and update the DataSet. To ensure we are thread safe when updating the DataSet, we have a lock to prevent more than one thread updating (writing) the DataSet.

Unfortunately during QA, we occasionally see the UltraGrid display a big red X. The entire gird basically crashes and it no longer able to display data.  The exceptions we got includes NullReferenceException, IndexOutOfRangeException and etc.  Initially, we thought that was a Infragistic bug in the UltraGrid, but in the end, we realized it is another type of Cross Thread operation. For example: InvalidOperationException was unhandled (Cross-thread operation not valid: Control ‘XXX’ accessed from a thread other than the thread it was created on.)

To prevent cross thread operations, we decided to ensure we called the ISynchronizeInvoke.IsInvokeRequired when we updates the DataSet.

Revised model (Deadlock on Invoke and Lock DataSet):

image

For the first couple of runs, the application seems to be executing correctly using the revised model. However, we got the application to hang.

From the diagram, it is probably obvious  that there is a deadlock from the pictures. However, it isn’t obvious when the order of locking and invoking calls are in different modules.  Because we didn’t standardize how we should do Invoke call and lock the data set first, we end up having random deadlocks on the Invoke call, and at the lock statement block.

Where is the dead lock?

In case the diagram is not obvious to some of the readers, here is what happening. Assuming you have an UI Controls created by only one thread, called the UI Thread (Main Thread), when Thread 1 called ISynchronizeInvoke.Invoke(), the UI Thread is locked to execute the Thread 1 request. At the same time, Thread 2 has locked the DataSet which prevents other threads updating it. When the Main Thread try to lock DataSet, it has to wait for Thread 2 to exit the lock block. At the same time, Thread 2 is waiting for the UI Thread to complete so that it can do the Invoke(). Since 2 threads are waiting for each other resources, we have a dead lock.

Solving The Problem

This model may not be the best solution, but it is simple to understand and it works:

image

In this model, all I did is to ensure all threads call Invoke() before locking the DataSet.

This design ensures we only have one thread access to the Lock. In our case it is the UI Thread. Whenever called the Invoke() on a Windows Form, your execution is passed to the UI Thread.

Now, you may wonder, why do I still need to lock the DataSet when there the Invoke there there.  From this simplistic diagram, we don’t.

But, you may have N number of Threads that didn’t call the Invoke for whatever the reason. An application can be complicated. To be safe, always lock the DataSet before applying changes.

 

 

 

 

 

How to Find a Deadlock?

You probably don’t want to ship a software that hangs in front of your customs, so you want to find these deadlocks before your customers find them for you.  Well, there is no easy way to determine where are the deadlocks in your application. Here is a couple of suggestions that may help:

  • Peer Code Review – have someone double check your check before check in the code. Check for possible deadlock by examining where lock, monitor class, invoke class are used.
  • Use a Deadlock Monitor – using a custom locking mechanism to determine if there is a possible dead lock.
  • Have a lot of tests – this can be unit tests, integration tests and your manually user tests. Whenever you see the application stop responding, report it and try to solve it.

Find Your Dead Lock using VS.

If you do find your application stops responding, what can you do? First, determine if your application is actually “working” by checking its CPU usage. If it is around 100% for one of your core, your application may be working hard or got simply running an infinite loop. This is NOT a deadlock!

In this case, your best tool would be using Visual Studio! Assuming you have all the debug files (.pdb) and source code for your program:

  1. Debug->Attach To Process, and attach to your non-responding application.
  2. Click on the pause button or Debug->Break All to stop the application
  3. Debug->Windows->Threads

A deadlock usually happen between two threads that hold on two resources, so you need to check where your executing threads has stopped. In the following example, I have a number of threads running, but I am only concerning my owns. These are the last two items in the Threads grid below.

image

  1. Double click on the Main Thread and the Worker Thread to see their executing locations.

For example:

image Main Thread executing location

  1. Notice the “green” highlight represent were is your thread executing.
  2. If you hit F5 (run) to un-pause the thread, and hit pause again, your thread execute would most likely remind at the same place.

image Worker Thread executing location

So, at least we know these two threads are trying to get some resources. However, how can you tell if they are holding each other requesting resources?

In this example, we know that the Main Thread waiting, so the Worker Thread have to wait at the “this.Invoke()” line. However, how do you know if the Worker Thread lock the m_oDataModel?

The only way to find out is to trace the executing code through the call stack. (If someone have a better way to find a dead without going though the code, please let me know!!!)

  1. Double click on the Work Thread
  2. Debug->Windows-> Call Stack

image

By tracing through the stack, I soon discover that the one of the call stack got a lock on m_oDataModel. Luckily in my case, I only have 2 visible stacks for this thread. If you have more, you will have to walk though them.

image

Ah, from the above, I see that I called lock(m_oDataModel), follow by calling the AddDataWorker() method.

Once you find out there is a dead lock, you will have to resolve it. Either by fixing the order of the locks or provide a better architecture that prevent developers run into this situation.

Sample Deadlock Code

Sorry that I don’t have a web space to store code file. I can only post the source code as it is. In this example, I have a form that has one button. If you click on the button, a dead lock will occur.

public partial class Form1 : Form
{
private DataSet m_oDataModel = new DataSet();
public Form1()
{
InitializeComponent();
m_oDataModel.Tables.Add("MyTable");
m_oDataModel.Tables["MyTable"].Columns.Add("SomeKey");
m_oDataModel.Tables["MyTable"].Columns.Add("ItemName");
}

private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(AddData));

//Similate doing something
Thread.Sleep(1000);

lock(m_oDataModel) //Wait for lock forever (deadlock)
{
m_oDataModel.Tables["MyTable"].
Rows.Add(Guid.NewGuid(), "Some Data");
}
}

private void AddData(object oState)
{
lock(m_oDataModel)
{
AddDataWorker();
}
}

private void AddDataWorker()
{
if (this.InvokeRequired)
{
//Wait for Main Thread to be free forever (deadlock)
this.Invoke(new Action(AddDataWorker));
return;
}

//Update a textbox
this.textBox1.Text = m_oDataModel.Tables["MyTable"].
Rows[0]["ItemName"].ToString();
}
}

Conclusion

As you get more experience and understand the structure of your application, the easier it will be for you to identify a deadlock. Deadlock isn’t just cause by the “lock” keyword in C#. It can be an Invoke() call, a read/write lock on a file, an database transaction lock and etc.  For those who read though the article to this end, thanks for reading and I hope this post helps you solving your deadlock issue.