iTunes skin for ASP.NET GridView and ListView

Posted by Matt | Filed under

Matt Berseth has created a cool skin for ASP.NET ListView and GridView based on iTunes.  More information can be found here.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Multi-core Support and Parallel Processing

Posted by Matt | Filed under , , , , ,

I have been playing around with multi-core support and parallel processing recently because we want to add multi-core support to our application.  So I’ve been investigating some tools like Intel’s Threading Building Blocks and Microsoft’s Parallel Patterns.

The nice thing about the above frameworks is that they use structures and templates to make multi-threaded programming “look” like single-threaded programming.  They’re pretty good at easing a developer into the world of multi-threading.

For example, both have a parallel_for function which can be used in place of a traditional for loop.  The framework does the dirty work of splitting the loop iterations into separate threads (as necessary).  To the single-threaded programmer, it doesn’t have a very foreign feel to it.

The biggest difference between the two is that Intel’s solution requires the creation of classes to do the loop contents, whereas Microsoft’s solution makes use of lambda functions to put the loop contents inline with the function call.  Microsoft’s solution is less “preparation” and generally looks better to the reader.

Another difference is that Intel’s solution is not free for commercial use.  It’s free for non-commercial use, however.  Microsoft’s solution is not available yet unless you want to use the Visual Studio 2010 CTP.  I’m not prepare for that yet because I’m not willing to move compilers yet, especially this early in VS2010’s development.

So, based on the documentation from both toolkits, I’ve been trying to come with with my own threading framework.  I’ve managed to create my own ParallelFor function similar to Intel’s (I don’t like underscores in indentifiers, instead I like multi-case identifiers).  It works pretty well and it’s good enough to deal with multiple cores.

However, using a simple Fibonacci example based on this video from Channel9, my framework falls apart.  For the 20th Fibonacci number, I get the following results on a dual core processor:

  • Single threaded: 110 ms
  • My framework: 66,812 ms
  • Intel’s framework: 63 ms

I think the memory allocations are severely slowing things down.  Also, I run out of stack in larger Fibonacci numbers, like 31, which run fine under Intel’s and Microsoft’s frameworks. 

Intel’s framework is not easy to use outside of the parallel_for functions, etc.  Microsoft’s framework at least looks easier to use when dealing with pure tasks.

After all this, I just hope that Microsoft releases their Parallel Patterns framework early for Visual Studio 2008.  It looks easier to use and it should be free.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Windows Azure

Posted by Matt | Filed under , ,

I have a new toy to play with today:  Windows Azure.

Azure is a development platform or operating system that allows for cloud computing.  In other words, clustering and fail-over handling.  So you can develop your website, put it in the “cloud” and Azure will automatically deal with the task scheduling and farming required to properly handle the scaling required for your application.

It’s currently in CTP for now (Customer Technology Preview).  You can download the SDK and VS2008 tools here and here.  Both are required to start creating applications using Visual Studio 2008.

Windows Azure will allow you to host your application on their servers, so they’ll handle any hardware failures.  However, they’ll charge you (prices still to be determined).

You need to register in order to get a token to create a hosted app.  I am still waiting for my token…

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Data binding to more than one object

Posted by Matt | Filed under , , ,

Most UI objects in WPF have a DataContext property.  Using this property, you can have that control, or other controls data bind to the object stored in the DataContext property.  By default, this DataContext property is passed down the control hierarchy by default.  This becomes very useful when nesting controls.

However, there is only one DataContext property on any given control.  Sometimes, you may be creating a UserControl and you need to have 2 or more data bound properties.  One way to do this is to encapsulate all your data inside a single class and have an instance of that class be stored in the DataContext.  This may not be convenient though.

It’s possible to add additional property members to your UserControls that can then be data bound.  For example, we have a data class that we are going to use.

public class Data
{
    public string Name { get; set; }
    public int Age { get; set; }
}

We need to add a property member to our UserControl to store the data.

public partial class DataControl : UserControl
{
    // ...

    public Data User
    {
        get { return (Data)GetValue(UserProperty); }
        set { SetValue(UserProperty, value); }
    }

    public static readonly DependencyProperty UserProperty =
        DependencyProperty.Register("User", typeof(Data),
        typeof(DataControl), new UIPropertyMetadata());
}

Now the UserControl has a member called ‘User’ which can be referenced via data binding.  In the Xaml for the UserControl, we can do this:

<UserControl
    x:Name="Main">
    <!-- ... -->
    <TextBlock Text="{Binding User.Name, ElementName=Main}" />
    <!-- ... -->
</UserControl>

What the above does is tells the TextBlock, that the data for the Text is in an element called “Main”, which we’ve labelled our UserControl via the x:Name property, and in that element, get the data from “User.Name”.  “User” is the property on the control, “Name” is the member of the Data class that contains the user’s name which we want to put in this text box.

Had the dependency property been something like a simple string, we could have just done {Binding User, ElementName=Main}

In our main window, we’d populate the control with the data by doing the following:

<Window.Resources>
    <local:Data x:Key="User" Name="Matt" Age="33" />
</Window.Resources>
<Grid>
    <local:DataControl 
        User="{Binding Source={StaticResource User}}" />
</Grid>

Once that’s done, you can repeat as necessary to add more data members to your controls as required.  There is only one DataContext member, but you can add as many other members as you need.  It also helps towards type-safety.

My sample project can be downloaded here:  DataBindingEx.zip (9.25kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Mixing RichEditBox with ScrollViewer

Posted by Matt | Filed under , , ,

I was creating an application where I wanted to add a RichEditBox inside a ScrollViewer.  The window containing the ScrollViewer was resizable, so I wanted the ScrollViewer to resize.  Thusly, I wanted the RichEditBox to resize as well.

What I attempted was this:

<Window x:Class="RichTextViewScrollerView.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ScrollViewer HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
            <RichTextBox />
        </ScrollViewer>
    </Grid>
</Window>

However, once I ran the application and started typing, I got this:

image

I tried giving a MinWidth to the RichTextBox, that helped, but it would wrap at that width:  not the full width of the window.

The problem is a bit of a chicken and an egg problem:  the ScrollViewer doesn’t have a width until it’s children have a width, and the RichTextBox doesn’t have a width until it’s parent tells it how much space it can take up.

Since I only needed to have the ScrollViewer scroll vertically, I set HorizontalScrollBarVisibility=”Disabled” and the RichTextBox worked properly.  This was because the ScrollViewer now has a defined width.

image

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Silverlight 2 is Out

Posted by Matt | Filed under , ,

Silverlight 2 has been in beta for a while now.  The release candidate was released a few weeks ago.  But now the final product has been released.

Click here for more information.

I plan on updating my Silverlight samples to the RTM “soon”.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Macromedia Flash as a performance enhancer, solved.

Posted by Matt | Filed under , , ,

Well, after much struggling and many uses of AQTime profiler, the issue has been solved.  The problem was not in the painting, which was where I was looking initially. My first hypothesis (along with most of my other) was that the video card was drawing slowly, but flash somehow kicked up the video acceleration, or something to that effect.  But I knew it wasn’t necessarily a hardware problem because a quick MFC test application worked find.  So it must be something in our code.

That entire time, I was looking at the wrong end of the pipeline.   I was looking at the paint events and other message handlers.  The problem was at the message pump end.

Many Windows programs have a message pump that looks like this:

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

We needed some custom functionality to handle idle-time functions, so our pump looked different:

for (;;)
{
    MSG msg;
    yield(); // So we don't hog the CPU
    if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else
    {
        doIdleWork();
    }
}

What this ended up doing is yielding the CPU between messages, even though we had messages still pending.

Moving the yield to after the idle work did the trick:

for (;;)
{
    MSG msg;
    if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    else
    {
        doIdleWork();
        yield(); // So we don't hog the CPU
    }
}

The buttons paint faster, and theoretically, the application should be more responsive.  This chunk of code is over 10 years old, written for Windows 95/98.   We’ve noticed this problem for a while, but were unable to fix it until now.  All because we were looking in the wrong place.  In the end, it was a one-line fix.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Macromedia Flash as a performance enhancer?

Posted by Matt | Filed under , , ,

My application has run into an interesting problem on some computers:  it draws very slowly (especially toolbars).  However, on those same computers, if you go to a webpage that has a Macromedia Flash animation on it, the painting suddenly becomes lightning fast.

It could be Windows, it could be the video driver, it could be our code.

There is some common threads between the computers that are affected, many are Dell, but not all.

We cannot explain it.

The problem is that this is one of those things that once you start profiling or debugging, you’re then affecting the performance outcome which leads you down many false trails.  These are the worst types of problems to debug.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Compiler Optimizations

Posted by Matt | Filed under ,

I am in the process of profiling our application, and I wanted to do some tests to prove/disprove my hypothesis.

I hypothesize that:

class A { ... };

A Foo()
{
    A result;
    // ...
    return result;
}

void main()
{
    A a = Foo();
}

is slower than:

class A { ... };

void Foo(A &a)
{
    // Do stuff to a
}

void main()
{
    A a;
    Foo(a);
}

I thought the first example above would have 2 constructor calls, followed by 2 operator = calls.  I thought that passing in the result variable to be modified would be less expensive with only a single constructor call with no copy calls.

Running each of the examples in the debugger, I observed one constructor call followed by one copy constructor call for the first example, and one constructor call for the second example.

This was the case when running using a debug configuration.

However, running with a release configuration, I ended up with one constructor call in total for the first example.  It appears that the compiler arranged memory such that the "temporary" result in the function Foo() was actually initializing and modifying the local variable in main().  This is actually quite efficient.  No copying of data was occurring while moving from scope to scope.

Another observance was that:

A a = Foo();

is not equivalent to:

A a;
a = Foo();

The first one really is a copy constructor call (when in debug).  The second example is an operator = call.

What did I learn from all this? 

The work I was doing to attempt to reduce data copying was almost in vain (in my case) since the compiler was doing some very good optimizations.

This may not be the case for your data, so it's best to do your own benchmarks to see if the compiler is helping you or not.

In the end, I still believe that passing the result variable is better than returning the result variable when working blind, but the compiler makes them pretty much equivalent in some cases.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tutorial: How to Get a List of Available Network Interfaces

Posted by Matt | Filed under , , , ,

If you're creating a network application, often you need to know if you have an active network connection to a LAN.  (Note that this is not the same thing as determining if you have a valid Internet connection.)  Winsock has a function which will let you get a list of available network interfaces:  WSAIoctl.

Using WSAIoctl, you can get a list of interfaces on the computer as well as some status information about them.  For example, it will tell you if the connection is up, loopback, point-to-point, supports multicasting, or supports broadcasting.

To get the list, you would use:

INTERFACE_INFO interfaces[32];
unsigned long nReturned = 0;
int nRet = WSAIoctl(s, // socket handle
    SIO_GET_INTERFACE_LIST, 
    0, 
    0, 
    &interfaces,
    sizeof(INTERFACE_INFO) * 32, 
    &nReturned, 
    0, 
    0);

if (nRet == SOCKET_ERROR) 
{
    int nError = WSAGetLastError();
    _tprintf(_T("Error getting interface list: %i\n"), nError);
    return;
}

After this, your list of interfaces will be in the array and the number of interfaces can be determined by:

int nNumInterfaces = nReturned / sizeof(INTERFACE_INFO);

From here, you can iterate through the interfaces checking for a connected interface.

for (int i = 0; i < nNumInterfaces; ++i) 
{
    INTERFACE_INFO *pIf = &interfaces[i];
    if ((pIf->iiFlags & IFF_LOOPBACK) != 0)
        continue;

    if ((pIf->iiFlags & IFF_UP) == 0)
        continue;

    // If you get here, you have a valid
    // interface to the LAN.
}

In the above case, we're looking for an interface that is up and not a loopback.

There is some other useful information in the INTERFACE_INFO structure:  ip address of the interface, netmask, broadcast mask, whether the connection is point-to-point, and supports multicasting or broadcasting.

I will reiterate that this does not tell you if you are connected to the Internet, nor will it tell you whether there's anything on the other end to communicate with.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5