Wednesday, December 18, 2013

C# Multithreading Loop with Parallel.For or Parallel.ForEach

Loop is such a trivial and so frequently used thing. And sometimes loops just killing performance of the whole application. I want the loop perform faster, but what can I do?

Starting from .NET Framework 4 we can run loops in parallel mode using Parallel.For method instead of regular for or Parallel.ForEach instead of regular foreach. And the most beautiful thing is that .NET Framework automatically manages the threads that service the loop!

But don't expect to receive performance boost each time you replace your for loop with Parallel.For, especially if your loop is relatively simple and short. And keep in mind that results will not come in proper order because of parallelism.

Now let's see the code. Here is our regular loop:
for (int i = 1; i < 1000; i++)
{
    var result = HeavyFoo(i);
    Console.WriteLine("Line {0}, Result : {1} ", i, result);
}
And here is how we write it with Parallel.For:
Parallel.For(1, 1000, (i) =>
{
    var result = HeavyFoo(i);
    Console.WriteLine("Line {0}, Result : {1} ", i, result);
});

Sunday, December 1, 2013

WPF MultiBinding Example (MultiValueConverter)

As you probably know the main key of WPF is binding. We bind WPF control to a property and data flows from business logic to UI and vice versa. But what if we need to bind several properties to a single control and depending on the state of these properties we will decide what to display? This technique called MultiBinding.
MultiBinding in WPF is always accompanied with MultiValueConverter. Inside it we define the logic that will decide what value to pass to our WPF control.

Today I'll show a simple example of MultiBinding in WPF. In my sample application I have three TextBox controls and a single TextBlock. I want to achieve this scenario: I want to show the text in the TextBlock, but only if each of three TextBoxes contain some text.

WPF MultiBinding Sample Application

Wednesday, November 27, 2013

MEF Simple Application in C#

I believe the best way to learn something new is to learn by simple and easy to understand example. Once you got the principles you can always proceed to more complex stuff. So today I'll show a simple example of MEF that will give you the basics.

Managed Extensibility Framework (MEF) is a component of .NET 4 and it allows to developer to build extensible applications. Imagine situation when you have some application and you know that in the future you will want to add more features to it, but you don't know exactly what and when they will be added, or may be you want another developer to develop new feature and integrate it seamlessly into the main application. So it looks like we need some module structured application here, right? And this is exactly what MEF provides.
Now to our sample application. When user starts it he asked to enter a string and a command (name of operation) he want to apply on that string. Once it's done the program will print the result string. So every operation we want to be a separate module. Enough bla bla! Let's see the code! :)

First, the Core class which is a part of the main application. Core class concentrates and launches all available extensions:
public interface ICore
{
    String PerformOperations(string data, string command);
}

[Export(typeof(ICore))]
public class Core : ICore
{
    [ImportMany]
    private IEnumerable<Lazy<IOperation, IOperationCommand>> _operations;

    public string PerformOperations(string data, string command)
    {
        foreach (Lazy<IOperation, IOperationCommand> i in _operations)
        {
            if (i.Metadata.Command.Equals(command))
                return i.Value.Operate(data);
        }
        return "Unrecognized operation";
    }
}