papadi Development Blog

.NET and Software Development by Dimitris Papadimitriou 
Filed under

VB

 

For Silverlight starters...

For Silverlight 2 starters I recommend this free tutorial from silverlight.net:

http://silverlight.net/learn/tutorials.aspx

There is code available both in C# and VB.

Filed under  //   .NET   C#   Silverlight   Tutorials   VB   Web  

Comments [0]

Εργαλείο για Performance Profiling εφαρμογών Compact Framework (και όχι μόνο)

Πριν περάσω στο θέμα, λίγα λόγια για το Profiling. Πόσες φορές στο παρελθόν (ίσως και παλιά στα χρόνια της VB 6) δεν βάλατε ένα timer στην αρχή και στο τέλος μιας συνάρτησης για να μετρήσετε πόσο χρόνο κράτησε η κλήση της; Αυτό ακριβώς πετυχαίνετε με το Profiling, χωρίς όμως να γράψετε ούτε μια γραμμή κώδικα! Ναι! Και το αποτέλεσμα είναι εντυπωσιακό! Έχετε χρόνους κλήσης για κάθε μια μέθοδο του κώδικά σας ξεχωριστά!
Υπάρχουν δυο βασικές κατηγορίες Profiling. Το Performance και το Memory Profiling. Με το πρώτο μετράμε χρόνους κλήσεων και με το δεύτερο την κατανάλωση μνήμης.

EQATEC Profiler
Ο EQATEC Profiler είναι ένα δωρεάν εργαλείο με το οποίο μπορείτε να κάνετε Performance Profiling. Το εργαλείο λειτουργεί δίνοντας του αρχικά το deployment folder της εφαρμογής σας. Επιλέγετε ποια dll θέλετε να "μετρήσετε" και το εργαλείο παράγει ένα δεύτερο deployment folder που το ονομάζει Profiled. Χρησιμοποιήστε τα αρχεία αυτά αντί για τα κανονικά και αυτό είναι όλο. Στο κλείσιμο της εφαρμογής σας θα δημιουργηθεί ένα xml report με το αποτέλεσμα του profiling. Ανοίξτε αυτό το report με τον ειδικό viewer που συνοδεύει τον EQATEC Profiler για να δείτε τα αποτελέσματα (βλ. εικόνα). Γράφοντας μάλιστα και λίγο προαιρετικό κώδικα μπορείτε να παίρνεται reports και όσο η εφαρμογή σας τρέχει, πατώντας για παράδειγμα ένα πλήκτρο της.
Το εκπληκτικό είναι ότι με αυτό τον profiler μπορείτε να μετρήσετε compact και desktop εφαρμογές ή ακόμα και web applications και windows services. Και μάλιστα μπορείτε να το κάνετε ακόμα και στο περιβάλλον του χρήστη χωρίς να χρειάζεται να εγκαταστήσετε εκεί κάποιο ειδικό εργαλείο. Αρκεί να στείλετε στο χρήστη να εγκαταστήσει την ειδική 'Profiled' έκδοση που παράγει ο EQATEC Profiler. Ο χρήστης θα χρησιμοποιήσει την εφαρμογή σας κανονικά, στην mobile συσκευή του ή στο desktop του και θα σας στείλει πίσω το xml report που θα δημιουργηθεί!

Παρενέργειες και απόδοση

Οι παρενέργειες που προκαλεί αυτό ο Profiler είναι ότι αυξάνει το μέγεθος της εφαρμογής σας περίπου 20% και μειώνει την απόδοσή της κατά περίπου 30%. Νούμερα αρκετά ικανοποιητικά για το αποτέλεσμα που έχει κανείς. Η EQATEC ισχυρίζεται ότι έχει κάνει Profiling στην εφαρμογή World Wind της Nasa, μια που το εργαλείο αυτό μπορεί αν χρησιμοποιηθεί και για εφαρμογές για τις οποίες δεν έχετε τον κώδικα!

Συν και πλην

Εντάξει, δωρεάν εργαλείο είναι, κάτι θα του λείπει! Ο EQATEC Profiler μετράει τους χρόνους κλήσεων των δικών σας συναρτήσεων και όχι των System.*. Που σημαίνει ότι δεν μπορείτε να έχετε αναλυτικά χρόνους εκτέλεσεις της κάθε γραμμής κώδικα ξεχωριστά. Μόνο τους χρόνους των μεθόδων σας. Για αναλυτικότερο profiling θα πρέπει να χρησιμοποιήσετε ένα εργαλείο όπως το εκπληκτικό dotTrace της JetBrains (δημιουργός του επίσης εκπληκτικού ReSharper). Αυτό όμως ούτε δωρεάν είναι ούτε παράγει Profiled εκδόσεις της εφαρμογής σας. Που σημαίνει ότι πρέπει να είναι εγκατεστημένο στον υπολογιστή που θα γίνει το Profiling και μάλιστα η εφαρμογή να ξεκινήσει από το περιβάλλον του. Και φυσικά δεν μπορεί να χρησιμοποιηθεί για mobile εφαρμογές καθώς βέβαια δεν μπορεί να εγκατασταθεί σε Windows Mobile. Εκεί είναι που έρχεται ο EQATEC Profiler και μας λύνει τα χέρια!

Filed under  //   .NET   Greek   Mobile   Tools   VB  

Comments [0]

Throw command difference in C# and VB.NET

Have you ever noticed that throw command behaves somewhat differently in VB.NET and C#? See the following two code parts. It's the same thing written in VB.NET and C#. DummyMethod throws an exception (line 12) that is caught by a try/catch statement in the Main method. Line 7 writes the exception into the console window.

 1: static void Main(string[] args) {
 2:  try
 3:  { DummyMethod(); }
 4:  catch (Exception ex)
 5:  { Console.WriteLine(ex);
 6:  }
 7:  Console.ReadKey();
 8: }
 9:  
 10: private static void DummyMethod() {
 11:  try
 12:  { throw new Exception("Dummy Exception"); }
 13:  catch (Exception ex)
 14:  { throw ex;
 15:  }
  16: }
 1: Sub Main()
 2:  Try
 3:  DummyMethod()
 4:  Catch ex As Exception
 5:  Console.Write(ex)
 6:  End Try
 7:  Console.ReadKey()
 8: End Sub
 9:  
 10: Private Sub DummyMethod()
 11:  Try
 12:  Throw New Exception("Dummy Exception")
 13:  Catch ex As Exception
 14:  Throw ex
 15:  End Try
 16: End Sub

If you execute both pieces of code in Release configuration you see in your console window that the exception was thrown by line 14. Now remove the underlined ex variable from line 14, so that Throw command is left alone. If you execute the code now (still in Release configuration) C# code will write that the exception was thrown by line 14 again, but VB.NET code will write that exception was thrown by line 12!

Click here to download:
ExceptionTest._zip (9 KB)

Filed under  //   .NET   C#   VB  

Comments [0]

Catch... When

Here is a tiny detail in the try/catch syntax of VB.NET you not be aware of - I wasn't until I recently discovered it in the documentation by accident :

Try
Catch ex As Exception When [Expression]
End Try

I recently needed this for a generic function which I was calling both synchronously and asynchronously (separate thread). I wanted this function to handle the exception by displaying a dialog box in case it was executed asynchronously only. So I just added a parameter to that function called 'handleException' and used it in the Catch...When statement.
Elegant!

Filed under  //   .NET   VB  

Comments [0]

IndexedDictionary. Access a dictionary using item index.

.NET Dictionary object can hold a collection of keys and values. It allows you to iterate through these values using enumeration and you can also retrieve a value by using it's associated key.
But what if you want to create a collection of items and access them both using their associated key or numeric index in the collection? I created a VB.NET class that I named IndexedDictionary that does exactly that.
The class implements the generic IDictionary interface. Internally it contains an instance of a generic dicrtionary and also an instance of a List which holds the numeric position of each element in the dictionary.

Here is an example on how to use this:

 1: Dim myData As New IndexedDictionary(Of String, String)
 2: myData.Add("key0", "item0")
 3: myData.Add("key1", "item1")
 4:  
 5: Console.WriteLine(myData(1)) ' returns 'item1'
 6: Console.WriteLine(myData("key1")) ' returns 'item1'

Click here to download:
IndexedDictionary._zip (2 KB)

Filed under  //   .NET   VB  

Comments [0]

ASP.NET on J2EE-enabled servers - Microsoft.VisualBasic.jar!

I downloaded and installed Grasshopper just to try it out! Installation was very smooth, without any of the difficulties I came upon every time I tried to install a non-Microsoft development tool. Well, ok, us, Microsoft oriented developers, may be a little spoiled! I just clicked on File-New-Project and there it was! VB for J2EE and C# for J2EE Projects! Amazing. My "Hello World" sample project was up and running on Tomcat (included in Grasshopper 50MB installation package) within 5 minutes! Right clicking on solution explorer allows to adding a reference to .NET and Java files! But one of the most astonishing things I have even seen in my humble carrier is this:

Microsoft.VisualBasic.jar !!!

Now I'm convinced!!! Anything is possible!
Unfortunately, since Grasshopper is based on Mono Project, it is available only for .NET Framework 1.1 and Visual Studio 2003. I read somewhere in Grasshopper forum that version for .NET Framework 2.0 is expected by the end of this summer.

About Grasshopper
Grasshopper, the freely available Visual MainWin® for J2EE™, Developer Edition, is a plug-in to the Visual Studio .NET IDE that enables .NET developers to port existing ASP.NET and server applications to J2EE servers. Developers can also use .NET skills to develop, debug, and deploy server and Web applications from within the Visual Studio .NET development environment, and run applications natively on the J2EE platform. Grasshopper is designed for small user group deployments.

Filed under  //   .NET   C#   VB   Visual Studio  

Comments [0]

Setting and restoring cursor for a time consuming operation in a single line of code

For the proper handling of the Hourglass during a time consuming operation the following code is required (more or less):

C# VB.NET
this.UseWaitCursor = true;
try
{  // DO JOB
   this.UseWaitCursor = false; }
catch (Exception ex)
this.UseWaitCursor = false;
    throw ex; }
Me.UseWaitCursor = True
Try
   ' DO JOB
   Me.UseWaitCursor = False
Catch ex As Exception
   Me.UseWaitCursor = False
   Throw ex
End Try

What if we wanted to be more accurate and restore the cursor to it's original state, which could be 'true' and not 'false'? Then we should declare a local variable, hold the original state and restore it later.
How would you like to use the following code!? Isn't it much more elegant?

C# VB.NET
using (new WaitCursorHandler(this))
{
   // DO JOB
}
Using WaitCursorHandler(Me)
   ' DO JOB
End Using

Simply add one of these files into your project (code requires .NET 2.0).

Filed under  //   .NET   C#   VB   Windows Forms  

Comments [0]

Most of things require much lesser code in .NET. Some things NOT!!!

Intro | After a few years dealing with .NET I can say that I'm very please with the fact that I have been working with Microsoft Development tools in my carreer. And this includes starting from GW-Basic, moving to VB5 and 6 and now 7 and 8 and a little C#.
Less Code | Most of things require much lesser code to implement. The code is almost completely managed and there are much fewer chances of using third part components or WIN32 API to get thigs done.
However | There are some other things however that require more code than before. And the question is why? For instance, to detect if the user clicked on the X button of a window to close it was 5 lines of code, including the event declaration, in VB6:

[code language="vb"]
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbFormControlMenu Then
        Cancel = True
    End If
End Sub
[/code]

Now, the code in NET Framework 2 is the following, as David Kean indicated to me in this thread http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=63488 (its in C#, but the same applies to VB8). It about 10 lines of code, without the brackets, which include a class level variable declaration:

[code language="c#"]
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Windows.Forms;

namespace MyApplication
{
    public class Form1 : Form
    {
        private const int WM_SYSCOMMAND = 0x0112;
        private const int SC_CLOSE = 0xF060;
        private bool _UserClose;

        public Form1()
        {
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (_UserClose)
            {
                MessageBox.Show("The user clicked the X button.");
            }
            base.OnClosing(e);
        }

        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND && ((int)m.WParam & 65520) == SC_CLOSE)
            {
                _UserClose = true;
            }
            base.WndProc(ref m);
            _UserClose = false;
        }
    }
}
[/code]

Filed under  //   .NET   C#   Desktop   VB   Visual Basic 6   Windows Forms  

Comments [0]