Wednesday, December 29, 2010

Developer Golden Rules

Below are some golden rules for the developer based on my experience with .Net Framework:

· Avoid shared members

· Avoid public variables in classes, always use properties

· Avoid db queries within the loop, always use batch insert, update …

· Avoid raising event on web applications

· Avoid unhandled exceptions

· Avoid MORE handled exceptions with empty catch, it is morphine!

· Avoid imports namespace; try to use fully qualified namespace: system.Lib.Lab.object

· Avoid (or never use) queries in GUI layer, always use BLL for queries

· Respect naming convention for variables and methods, CamelCase for variables (ActiveVar) , and lowerCamelCase for GUI objects (txtUsername), and _VarName for property local variables

· Avoid using modules; it is always reserved for system Architect

· Avoid String manipulation “Bla” & “Foo” always use StringBuilder or String.Format

· Avoid String comparison without .ToLower, make sure the Str has value ( If Not String.IsEmptyOrNull() AndAlso S=”..”)

· Avoid using AJAX without handling OnFailure requests

· Avoid Plaintext with AJAX, always use JSON response for AJAX Requests.

· Avoid Using Stream.ReadToEnd, always read buffer/buffer

Avoid does not mean DO NOT USE: Standard = 98xStandard + 2xUnstandard

Tuesday, December 14, 2010

Connect your Laptop through Alfa GPRS (Lebanon)

Good news for Lebanese Alfa GPRS users:

I was wondering why Alfa GPRS (Prepaid Cards) prevents the access from your computer through your cell phone.
But actually they cannot prevent you, if you are able to connect from your cell phone to the Internet so you should be able to access the Internet from your Laptop. So how Alfa prevent Internet access from the Laptop?


The answer is easy, they just added some funny rules on Alfa firewall/proxy to reject any none-WinCE or cell connection based on some HTTP header sent by the browser.

So to bypass this funny rule all what you have to do is to impersonate your browser as mobile browser.

Summary about Alfa Network:

Alfa GPRS Network assign fake (private) IPs for the GPRS users, subnet: 172.124.x.x, and the users access the internet through Alfa proxy server: 192.168.23.50

Solving the problem:

I have developed a lite FireFox Extension to allow you bypass Alfa Proxy and to browse the Internet from you Laptop by simply connect to your phone via Wifi Router ( HotSpot mode).

  1. Download and Install (drag and drop to firefox) my extension from: AlfaGPRS
  2. Add Alfa proxy server 192.168.23.50 to FireFox: eHow
  3. Make sure your Mobile can access the internet
  4. Turn on your Wifi Router (Hotspot mode) and connect your laptop to your mobile (ie)
  5. Voila! now you will be able to access the internet.

NB:
  1. Try the WAP powered sites rather than full sites, example: m.google.com
  2. When you want to navigate the net with regular internet connection you have to remove the proxy and disable the extension

Known issues:
  • Problem with HTTPS connections
  • Download file size limit 500K (limit by Alfa)


Good Luck :)

Friday, November 12, 2010

GDI+ Leak on .Net Framework

I encountered the following error on many machines for our clients using Windows XP with our .Net application:

System.OutOfMemoryException: Screen-compatible bitmap cannot be created. the screen bitmap format cannot be determined.

After long search and investigation, I figured out that this error is related to GDI+ leak with .Net Framework.
Background:
The GDI (Graphic Device Interface) limits the number of object per processor by 10000 object, if your application reach this number you will encounter such situation.
you can find the limit value in the registry:

\HKEY_LOCAL_MACHINE
\SOFTWARE
\Microsoft
\NT
\CurrentVersion
\Windows
ProcessHandleQuota:REG_DWORD
Sure it is not a good idea to increase this limit, but to solve the problem and the leak.
Finding and solving Memory leak in .net is very easy, actually you don't need so, and you can use the GC, but the GDI leak is another story, you can't COLLECT the GDI leak.
Example of Leaky! code with NotifyIcon in sysTray:
In this code you have to change your NotifyIcon based on your application status (error, checking, online ...)

NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(New Bitmap((ImageList1.Images(0))).GetHicon())
... Code 1
... Test 2
NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(New Bitmap((ImageList1.Images(1))).GetHicon())
...
...
NotifyIcon1.Icon = System.Drawing.Icon.FromHandle(New Bitmap((ImageList1.Images(2))).GetHicon())
...
Every time you assign an icon to your notifyIcon the GDI+ object increased till reach the limit and Voila!
Setting the objects to Nothing or dispose the object dose not solve the problem, the best and optimal solution is the following:
Read all your icons on application startup or form load into List Of Icons, then assign it to your NotifyIcon1,

Dim ListIcons As New List(Of Icon)
Sub Form_Load()

ListIcons.Add(System.Drawing.Icon.FromHandle(New Bitmap((ImgListLogos.Images(0))).GetHicon()))

ListIcons.Add(System.Drawing.Icon.FromHandle(New Bitmap((ImgListLogos.Images(1))).GetHicon()))

ListIcons.Add(System.Drawing.Icon.FromHandle(New Bitmap((ImgListLogos.Images(2))).GetHicon()))

ListIcons.Add(System.Drawing.Icon.FromHandle(New Bitmap((ImgListLogos.Images(3))).GetHicon()))

End

Now change your code to:

NotifyIcon1.Icon = ListIcons(0)
... Code1
... Test
... Foo
NotifyIcon1.Icon = ListIcons(3)
...
This workaround will solve your GDI leak in your application.
To monitor the GDI Leak, go to Task Manager and select View>Cols>GDI Objects