Who is calling me? The *69 service in .NET


I recently started a project where I needed to add logging to an existing codebase. One of the requirements is that the class and method is logged along with the severity, message, exception (if there is one), date and time, etc, etc, etc. It started out not looking pretty with constants added to each class for just the class name or writing out typeof(blah).FullName and the method name each and every time, a message needed to be logged. I figured there had to be better way and parsing Environment.StackTrace (type string) didn’t seem to be it.

Looking through System.Diagnostics, I stumbled upon the StackTrace class and from there it was a skip, hop and a jump to figure out how to unwind the stack and get the information I needed.

StackTrace stackTrace = new StackTrace(1); //skip at least this frame
for (int frameCounter = 0; frameCounter < stackTrace.FrameCount; frameCounter ++)
{
    StackFrame frame = stackTrace.GetFrame(frameCounter);
    MethodBase method = frame.GetMethod();

    Console.WriteLine("Class: {0}, Method: {1}",
        method.DeclaringType.FullName,
        method.Name);
}

Simples.

Read more

Windows 7 Taskbar Thumbnail Buttons and .NET

If you have been using Windows 7, then you have probably noticed the 3 little buttons just below the Windows Media Player thumbnail if you hover over it in the taskbar.  In this sample, I’m going to demonstrate how you can easily add your own – and when I say easy I mean easier than boiling water – that easy.

Read more

Interop’ng with Vista (Displaying Battery Power in the Taskbar) – Part 2

After a couple of days of developing (see previous post: Interop’ng with Vista (Displaying Battery Power in the Taskbar), I had a fairly working little utility to display the current battery status for my laptop working.  But what it had in functionality it lacked in appearance.

myMojo Battery Power Monitor v1

Every once in a while, I would pick up the project again, do some more Googling and try out a few more things but without any lock on getting the transparency to work.  All directions pointed at implementing the IDeskBand2 interface rather than the IDeskBand, though any attempt would just crash explorer when I would add the utility to the Taskbar.  So more Googling, more attempts later, I finally stumbled upon an excellent post which worked - http://cgeers.wordpress.com/2008/02/16/internet-explorer-toolbar/

I still implemented IDeskBand interface though overriding the OnPaintBackground using Geer’s supplied code

#region Transparency
[DllImport("uxtheme", ExactSpelling = true)]
public extern static Int32 DrawThemeParentBackground(IntPtr hWnd, IntPtr hdc, ref Rectangle pRect);
protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (this.BackColor == Color.Transparent)
            {
                IntPtr hdc = e.Graphics.GetHdc();
                Rectangle rec = new Rectangle(e.ClipRectangle.Left,
                    e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height);
                DrawThemeParentBackground(this.Handle, hdc, ref rec);
                e.Graphics.ReleaseHdc(hdc);
            }
            else
            {
                base.OnPaintBackground(e);
            }
        }
#endregion

and voila!

myMojo Battery Power Monitor v2

(and changing the progress bar to a set of images helped a lot)

Read more

Worst New Feature in C# 3.0 – Implicity Typed Variables

I’m all for adding new language constructs but this one I just can’t endorse – implicity typed variables.

Now, I’m not a fan of scripting languages (perl, javascript, python, etc) and am pretty sure I’m not I’m ever going to use this.

So instead of writing

string url = "http://mymojo.ca";
int port = 80;

you can write

var url = "http://mymojo.ca";
var port = 80;

 And everything is suppose to just work as if it were strongly typed.

var x = 5;
var y = "11";
var z = x + y;

And what is the result of z - 16 or “511″?

Read more

WPF: One Small Step For UI; One Giant Leap Back For Data Binding?

I’ll admit it, I’m new to WPF.  But have been spending the last week or so (when I have time) to go through Hands On Labs or any other tutorials I can find to get up to speed.  And now that I’m done, I wanted to apply my new found knowledge and build something without a helping hand – something simple, WPF for display, using LINQ in the data access layer and SQL for the backend data store. 

SQL and LINQ was the easy part.  Business entities all setup, data access layer is work and then…

stopped.  WPF – ugh!  To be fair some of the issues aren’t specific to WPF.

  • Expression Blend doesn’t play nicely with Visual Studio 2008 C# Projects
  • Auto-complete for XAML files works sometimes – which is frustrating if you are trying to learn it
  • Lack of online samples (or samples that are questionable in terms of code, i.e., not using a using statement when working with a database connection) that have full CRUD 

But really, my biggest beef is with Visual Studio 2008.  What I was really hoping for was ASP.NET 2.0 data-binding in WPF – little handles over a control, wizards to setup which binding source to be used, auto-generation of SQL commands, etc, etc, etc.  Prototyping was dead easy and you could have a small data bound web application up in minutes.  Sadly this is all missing – not only making the barrier to entry to WPF that much higher.

Might just have to break down and get a book…

And if you haven’t grabed the Visual Studio 2008 and .NET Framework 3.5 Training Kit yet:

http://www.microsoft.com/downloads/details.aspx?FamilyID=8BDAA836-0BBA-4393-94DB-6C3C4A0C98A1&displaylang=en

Read more