C# things that make us happy

Sometimes our lives as programmers get a bit easier, Microsoft has launchedlaunched C# 7.0 some time ago. Here are a few examples of the things that made our lives a lot easier.

The microsoft page has better explanations, the code below is just random code from my project. These examples are only intended to outline some of the new features.

Pattern matching

            if (sender is ToolStripMenuItemTranslate)
                ToolStripMenuItemTranslate item = (ToolStripMenuItemTranslate)sender;
                TabPageAddOrShow(item.CustomTabPage, ModifierKeys == Keys.Shift);

Can be rewriten as:

            if (sender is ToolStripMenuItemTranslate item)
                TabPageAddOrShow(item.CustomTabPage, ModifierKeys == Keys.Shift);

Using the underscore we can ignore variables, which keeps the compiler (and resharper) happy:
DoSomething(out String variable1, out String _)
Which brings us the pleasure of inline variable definition as well as ignoring unused ones.

It can also create funny code (introducing underscores in useless places):

            if (sender is ToolStripMenuItemTranslate _)

While we have had basic tuples for a long time they grew up and gave us a weapon against the useless class/structs to perform returns (or way to many out variables).

        public (String results, Int32 exitCode) ExecuteCommand(String command)

Keep in mind that a tuple is a struct!

Switch statements with patterns
While switching still isn’t available on primitive types it sure improves our lives a lot:

                switch (Columns[i])
                    case DataGridViewTextBoxColumnDatabaseBackendTranslate a:
                        if (!a.CustomOnlyOnceCombined) continue;

                    case DataGridViewTextBoxColumnCustomIDTranslate b:
                        if (!b.CustomOnlyOnceCombined) continue;

                    case DataGridViewComboBoxColumnArrayBackendAndTranslate c:
                        if (!c.CustomOnlyOnceCombined) continue;

                    case DataGridViewComboBoxColumnDatabaseBackendAndTranslate d:
                        if (!d.CustomOnlyOnceCombined) continue;


The nature of C# locking issues explained

Sometimes in the life of a programmer everything seems peachy, C#/DotNet provide relatively easy multi threading capabilities which 99.9% of the time work just fine as explained everywhere online.

However when things get a bit serious as these online explanations tend to be off by a small bit, only that small bit creates havoc….

Imagine having to debug code within a lock() statement that crashes with exceptions like “Collection was modified; enumeration operation may not execute” which seemingly bypass any locking in place.
These are not fun to deal with as everyone will for some reason stubbornly tell you that that is not possible and you are simply lacking a lock somewhere and just moving on like they have just shed divine light.

First of all lets get the common misconception cleared that it is impossible for a single thread to execute code in parallel and even worse that events will always be fired with a new thread affinity. This is just plain wrong!

Create a simple empty form with the following code in the shown event:

            Button testButton = new Button { Dock = DockStyle.Fill };
            testButton.Click += Test_Click;

And paste the following somewhere below:

        private readonly object _lockHelper = new object();
        private void Test_Click(object sender, EventArgs e)

                int i = 0;
                while (i++ < 50)


If what everybody proclaims is right this code should just lock up the UI and neatly print "A", "B" in sequence when triggered.
This is where the bad news starts, it doesn't, simply clicking the button a few times very easily prints "A", "A", "B", "B".
So with a few lines of code we have disproved two very stubborn myths in one blow.

Now for a somewhat more sensible approach:

        private static readonly SemaphoreSlim _lockHelper = new SemaphoreSlim(1, 1);
        private void Test_Click(object sender, EventArgs e)

            int i = 0;
            while (i++ < 50)


This properly locks up the UI as expected (it's example code, not release code) and can even be used for some more interesting purposes when multi threading.

Please don't be like all the other assholes online who proclaim that this doesn't happen in normal production code (or even worse that a simple lock() should always be sufficient as events have different thread affinities).
Simply imagine a simple asynchronous communication protocol between two programs or a server and a client, having simple locks can seriously fuck up your day with some long debugging ahead.

MariaDB + Debian

For MariaDB fans there are some changes coming in the current Debian testing/unstable, which will hit mainstream sometime this year (I guess).
A lot of things compiled against libmariadbclient.so.18 will start failing, most of which can be solved with a simple symlink except a few…

Opendmarc wil fail to compile:
opendkim: malloc.c:3757: _int_malloc: Assertion `(unsigned long) (size) >= (unsigned long) (nb)' failed.

Spamassassin will fail to run
spamd[27186]: at /usr/share/perl5/Mail/SpamAssassin/Conf/SQL.pm line 138.
spamd[27186]: spamd: service unavailable: Error fetching user preferences via SQL
spamd[3690]: config: failed to load user (dmarc@black-mail.nl) scores from SQL database: install_driver(mysql) failed: Attempt to reload DBD/mysql.pm aborted.
spamd[3690]: Compilation failed in require at (eval 1409) line 3, line 2.

The opendkim one is pretty vague and seemingly unhelpfull but a bit of digging will reveal the following:
Can't load '/usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so' for module DBD::mysql: /usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so: symbol mysql_options4, version libmariadbclient_18 not defined in file libmariadbclient.so.18 with link time reference at /usr/lib/x86_64-linux-gnu/perl/5.24/DynaLoader.pm line 187.
at /usr/sbin/opendmarc-import line 22.
Compilation failed in require at /usr/sbin/opendmarc-import line 22.

Hey, now we have a finger to point, Perl::DBD:
ldd /usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so
/usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so: /lib/libmariadbclient.so.18: no version information available (required by /usr/lib/x86_64-linux-gnu/perl5/5.24/auto/DBD/mysql/mysql.so)
linux-vdso.so.1 (0x00007ffdca9bc000)
libmariadbclient.so.18 => /lib/libmariadbclient.so.18 (0x00007faf4fe63000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faf4fac5000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007faf4f8ab000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007faf4f68e000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007faf4f48a000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007faf4f184000)
libssl.so.1.1 => /usr/lib/x86_64-linux-gnu/libssl.so.1.1 (0x00007faf4ef18000)
libcrypto.so.1.1 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007faf4ea85000)
/lib64/ld-linux-x86-64.so.2 (0x000056045c708000)

For some reason no symlinking will solve this, but no worries, simply get the source, unpack it and run:
perl Makefile.PL
make install
make clean

After this things should work smoothly, but keep in mind to keep the package up date date and remove the manually installed files should Debian fix this.

Corrupting bash on Windows

Let’s asume bash is allready installed:
sudo apt-get install bb

If you are root exit to the normal user, bb won’t launch as root.

bb -loop -dim -bold -reverse -normal -boldfont -extended -inverse

After a while (especially during the heavier animations) the line width wil vary and result in some weird looking stuff.

Effective dovecot+sieve extension filtering

After some recent conversations with a colleague about creating a simple shield that would stop some of the dangerous crypto/ransomeware I set about creating such a filter.

The premise was simple, move all mail containing predefined attachments under a certain size to a different folder. Sounds like a fine job for the sieve filter to me.

A quick search on the internet resulted in some basic ways to achieve this sort of behavior, however most were plain wrong in the execution of the regex statements. Which as it turns out should not even be used, the pigeonhole addon does all the required mime handling for us.

Problems with most solutions:

  • Regex statement per extension which creates an unreadable mess (and plain kills the server performance on larger emails)
  • Not limiting the search areas, this results in a lot of false positives (e.g. any of the extensions appearing AFTER the filename has been specified (within the encoded attachment) would trigger the rule)
  • Failing to correctly identify and target the different filename formats (filename=”test.zip”, filename=test.zip[\r\n] or filename=test.zip; and in extreme cases just a file= with no filename=)

The result code:

require ["body","fileinto","regex"];
 File potentially dangerous extensions into a seperate folder
  if allof (size :under 200K, header :mime :param "name" :matches ["Content-Type", "Content-Disposition"] [ "*.exe", "*.pif", "*.scr", "*.zip", "*.7z", "*.rar", "*.bat", "*.reg", "*.wmf", "*.emf", "*.wmz", "*.emz", "*.com", "*.hta", "*.cmd", "*.vb", "*.vbs", "*.ws", "*.wsf", "*.wsc", "*.wsh", "*.ps1", "*.ps1xml", "*.ps2", "*.ps2xml", "*.psc1", "*.psc2", "*.msh", "*.msh1", "*.msh2", "*.mshxml", "*.msh1xml", "*.msh2xml", "*.scf", "*.lnk", "*.inf", "*.vbe", "*.js", "*.jse", "*.ocx", "*.docm", "*.dotm", "*.docx", "*.xlsm", "*.xltm", "*.xlam", "*.pptm", "*.", "*.potm", "*.ppam", "*.ppsm", "*.sldm" ])
    fileinto "Dangerous";

Now this doesn’t solve all problems presented with the current ransomeware (or other scams/virusses), but it does prevent a lot of the current exploits which were comming in in bulk.

Don’t forget to specify the folder in doveoct as a required folder:

namespace {
  type = private
  separator = /
  inbox = yes

  mailbox Trash {
    auto = subscribe
    special_use = \Trash
  mailbox Drafts {
    auto = subscribe
    special_use = \Drafts
  mailbox Sent {
    auto = subscribe
    special_use = \Sent
  mailbox Spam {
    auto = subscribe
    special_use = \Junk
  mailbox Virus {
    auto = subscribe
  mailbox Dangerous {
    auto = subscribe

Systemd @reboot cron replacement

Some of us enjoyed the old @reboot crontab entries but the recent upgrades to systemd have created some new challenges.
The most prominent being that a simple systemd service kills any spawned processes after it exits. (I know it is not supposed to be used like this, but for some simple services like mailgraph writing service files for everyone is just too cumbersome).

Description=Boot services


DotNet simple and effective profiling

There is one thing all programmers experience at one point or another, performance issues… One might use simple break points to get a coarse idea of the problem locations or when things get really frustrating a stopwatch with several measurement points.
But even that gets frustrating when there are lots of problem areas and it soon turns into a cumbersome practice which we all loathe.

But nothing beats some good old fashioned scripting to create a more elegant solution:

    public class SimpleProfiler : IDisposable
        private readonly Stopwatch _stopWatch = new Stopwatch();
        private readonly int _measurementFormatLength;
        private readonly bool _printFinalResult;
        public SimpleProfiler(string title = null, int measurementFormatLength = 30, bool printFinalResult = true)
            _measurementFormatLength = measurementFormatLength;
            _printFinalResult = printFinalResult;
            if (title != null)
        public void Restart()
        public void Dispose()
            if (_printFinalResult)
                Debug.WriteLine("Elapsed {0} ms", _stopWatch.ElapsedMilliseconds);

        public void AddMeasurement(string comment = "")
            Debug.WriteLine(" {0} {1} ms", comment.PadRight(_measurementFormatLength).Substring(0, _measurementFormatLength), _stopWatch.ElapsedMilliseconds);

Simple invoke:

            using (new SimpleProfiler())
                WebClient webClient = new WebClient();

More complex invoke:

            using (SimpleProfiler profiler = new SimpleProfiler("Download test", 15, false))
                WebClient webClient = new WebClient();
                profiler.AddMeasurement("http test");
                profiler.AddMeasurement("https test");

Next up the extension method, sometimes it’s better to itterate a function a “few” times in order to get a better idea:

    public static class ExtensionMethods
        public static Int64 Profile(this T input, Action action, int iterations = 1)
            Stopwatch stopwatch = new Stopwatch();
            for (int i = 0; i < iterations; i++)

            return stopwatch.ElapsedMilliseconds;

        public static Int64 Profile(this Action action, int iterations = 1)
            Stopwatch stopwatch = new Stopwatch();
            for (int i = 0; i < iterations; i++)

            return stopwatch.ElapsedMilliseconds;

Simple invoke:

            Action action = LongCalculation;

More complex invoke:

            Action action = LongCalculation;
            Debug.WriteLine(action.Profile(x => x.Invoke("Some input")).ToString(CultureInfo.InvariantCulture));

Although it won't provide you with some of the nice graphs, heatmaps or other goodies some external applications might offer, it will save a lot of time and annoyance.

JSON or XML intermediate data format

You might or might not have heard about XML and JSON. And wonder which one is better to represent data with for communication, save functionality or config.

Before I do so I will explain what JSON and XML are in depth. If you are not interested, skip to ‘One over the other’

What is JSON?
JSON stands for JavaScript Object Notation and is actual javascript code. When put in to a javascript evaluator JSON can directly be translated in to objects, array and primitive types (integer, string, float).

A basic JSON string containing a person object could look like this:

name: ‘John’,
age: 14,
length: 1.55,
profession: ‘Student’,
friends: ['Suzzy', 'Cate', 'Mike', 'Tom']

Although JSON can only be parsed when well formed. Their is no default possibility to define what every value should look like. If you want to put ‘hello world’ into the length property this is valid. To do such checks code should be written to check these properties.

As noted before that JSON is actual javascript code. So parsing of these string is in general very quick.

What is XML?
XML stands for Extensible Markup Language and has been standardized by the W3C (World Wide Web Consortium). And has been created to standardize the way HTML (HyperText Markup Language) is written.

The same object shown above could look like this.


I would like to point out that this representation automatically makes ‘objects’ from friends. Make it easier to put more properties on these. But the same can be done in JSON. So far you can represent the same complexity of data.

XML however has 2 things that make it more powerful then JSON.

First off is XSD (XML Schema Document) can be used to validate the structure and data types (not if it’s well formed, because it needs to be well formed to be able to check it against an XSD). XSD are XML’s with tight regulation on how they should be build and describe how your XML should look like. Thus using an XSD makes it very easy to pinpoint troubles in your data structure as it will give an accurate error of the problem.
For further information check: http://www.w3schools.com/schema/default.asp

Second is XSLT (EXtensible Stylesheet Language) which can be used to format a XML to more readable formats. Although only text can be produced by default, like HTML pages or txt files. This format is also tightly standardized and is being used as a base for PDF generators and other formats.
Check http://wiht.link/XSLT-intro/ for more information.

Finally I would like to point out that XML needs some getting used to when first used to write a data structure for. With JSON objects and arrays explain themselves. Defining structures comes naturally as it is clear what every part does.
With XML it might not always be clear how something should be represented at first and some research is needed. This is especially true when using XSD or XSLT documents.

One over the other
Here under I will point out the strengths and weaknesses of both formats.

Using JSON over other interchange-formats

  • Can be read more quickly
  • Little overhead
  • Fast writing
  • Easier to learn
  • High performance deserializing


  • Needs time getting used to
  • If you write your own JSON parser its hard to debug
  • Harder to cut in to chunks

Thoughts on XML

  • Better error handling on syntax errors
  • More easily readable with deep levels
  • Superiour data integrity checking when XSD is used
  • XSLT can be used to formating for readability


  • To make error handling work their is need to make a XSD
  • Overhead with closing tags (slows down reading too)
  • Chance of conflicting names and functions of nodes
  • Necessity to create well formed XML constructions to represent your data
  • Harder to learn when structures become more complex

Although you might be dazzled or tempted to choose one over the other. I would like to point out that neither is absolutely superior and either can be used for all purposes you might have to represent data. However one is more suited for certain applications then the other.

If you have a website with lose data calls and no refresh, use JSON. It’s faster and has less overhead. In this fashion JSON is suitable for other communication and cases you need small size and high decoding speed.

If you need readability and tight error control over large sets of data that are being maintained by people. Use XML. It’s XSD data integrity checking makes it far easier to write by hand and XSLT can speed up human checking. So it’s ideal for config files, documents and dumps of large chunks of data that are complex in nature.

ExtJs 3.4 – Using tooltips

Tooltips provide an easy way to add useful information without cluttering the screen with enourmass chunks of data. ExtJs has fitted a couple of elements with a default tooltip. Button, Grid.Column.Header and Window are examples of that.

But sometimes you find yourself wanting to emphasize other things like a grid, rows, grid cells, an icon or blocks of text.

There is qtip, which can be placed within the HTML code directly to provide the user with a simple tooltip. But you have to encode everything to not make the first “ or ‘ break it.

So I recommend using the tooltip class.

Basic tooltip

This will set a tooltip on any element with the CSS class “tooltip-target” that is within the the panel “this.mainPanel”.

The CSS class does not have to exist for this to work. You might however want to add an empty CSS class with a comment, just to be sure it doesn’t get cleaned out by accident by someone that stumbles upon it.

var tip = new Ext.ToolTip({
   target: this.mainPanel.getEl(),     //target to apply tooltip on (also scope of the search, MUST be an Ext.element)
   delegate: '.tooltip-target', //target the tooltip will be shown on
   delay: 0,                   //how long to hover before showing
   trackMouse: false,          //follow the pointer while over element
   renderTo: document.body,    //where to draw, document.body is recommended
   autoHide: false,            //don’t hide after a while
   anchor: "right",            //where to show the arrow on the tooltip
   html: "Tooltip content"     //content to show

Grid cell

To make it work on a grid, just provide a different delegate and target. This will popup when hovering a row. If you want to make it select on just some cells, make up your mind in the renderer, by adding the previously mentioned “.tooltip-target” as delegate.

var gridTip = new Ext.ToolTip({
   target: this.grid.getView().mainBody,
   delegate: '.x-grid3-row',
   renderTo:  document.body,
   html: "Tooltip content"

Updating the content from given element

Nice, but now I got fixed content within the tooltip, making it hard to use. You first thought might be: “I could make more CSS classes to hang other tooltips from .”

Consider that every tooltip will be activated when entering the scope of its target elements. This could create a performance hit.

Instead update the content of the tooltip with the element you just hovered over. Is most cases like with a grid the data that the element you hovered over originated from is in a store. Or more precise, inside a record.

So for a grid you could use this after creating the tooltip.

gridTip.on("beforeshow", function(tip) {
   //the element being hovered over
   var triggerElement = tip.triggerElement;            

   //get row index
   var rowIndex = this.grid.getView().findRowIndex(triggerElement);

      //get record
      var record = this.grid.getStore().getAt(rowIndex);

      //you could also format this
      //or use another store to add even more information
      var content = record.get(“some-column”);

      //update the tooltip
}, this);

Update tooltip with call

As long as the function is delayed, the tooltip will not be shown. So it is a good practice to add a loading message and update the tooltip as soon as data from example an AJAX call is available.