Lambda Expressions and Threads in C#

Here’s a small program… what do you think the output will be?

public static void Main(string[] args){
    Thread[] t = new Thread[10];
    for (int tNum = 0; tNum < 10; tNum++) {
        t[tNum] = new Thread(() => {
            Thread.Sleep(new Random().Next(20));
            Console.Write(tNum + " ");
        });
        t[tNum].Start();
    }
    // wait for the threads to finish
    for (int tNum = 0; tNum < 10; tNum++){
        t[tNum].Join();
    }
    Console.WriteLine("\nPress a key...");
    Console.Read();
}

Continue reading “Lambda Expressions and Threads in C#”

Using a GTK# TreeView Widget in C# – Very Simply

I’ve been experimenting with using GTK# and C# to build GUI applications which will run on Linux, Windows and Mac. Monodevelop has a graphical form designer for Gtk# front ends, but it’s not as simple as building WinForms applications under Visual Studio. An example is the TreeView widget – it’s designed from an MVC perspective, with a ListStore or TreeStore object containing the model (or data), various objects collectively representing the view (columns, cells, cell renderers etc) and the controller functions allow you to sort and filter the data. All the building needs to be done in code rather than at design time. This is a lot to wrap your head around if you simply want to display some data in a tabular format, as I did, when I wanted to display the parsed contents of a log file!

To make this easier for me in future, I’ve written a small class which hides just about everything. Read on…

Continue reading “Using a GTK# TreeView Widget in C# – Very Simply”