Monday, 17 November 2014

How to check if OSX is 32 or 64-bit?

In order to determine whether bitness of the operating system, we can query it for the value of the POSIX constant LONG_BIT which tells the number of bits in a long int:
$ getconf LONG_BIT
Bitness of the kernel can be fetched from the machine hardware name. x86_64 in the output means that machine has the 64-bit CPU:
$ uname -m

Thursday, 13 November 2014

Pulling content from the remote repository via SourceTree

Here is the list of commands used by SourceTree when you pull branch mybranch from origin for some repository myrepo:

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin
From ssh://
e855518..9f48d16 master -> origin/master

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree pull --no-commit origin mybranch
From ssh://
* branch mybranch -> FETCH_HEAD
Already up-to-date.

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree submodule update --init --recursive

Completed successfully

We can see that main commands used here are:

git fetch origin
git pull --no-commit origin mybranch
submodule update --init --recursive

Sunday, 9 March 2014

async - await idiom

async-await idiom has been introduced in C# 5.0 and it is basically a syntactic sugar for implementation of task's continuations.

Here are some interesting excerpts from the article "Easier Asynchronous Programming with the New Visual Studio Async CTP" by Eric Lippert, which was published in MSDN Magazine in October 2011:

The work that must come after a particular task is finished is called the continuation of the task.

An await expression (...) means “evaluate this expression to obtain an object representing work that will in the future produce a result. Sign up the remainder of the current method as the callback associated
with the continuation of that task. Once the task is produced and the callback is signed up, immediately return control to my caller.”

Every time an await is encountered, the currently executing method signs up the rest of the method as the thing to do when the current task is complete, and then immediately returns. Somehow each task will complete itself—either by being scheduled to run as an event on the current thread, or because it used an I/O completion thread or worker thread—and will then cause its continuation to “pick up where it left off ” in executing the rest of the method.

Note that the method is now marked with the new async keyword; this is simply an indicator to the compiler that lets it know that in the context of this method, the keyword await is to be treated as a point where the workflow returns control to its caller and picks up again when the associated task is finished.

Asynchronous Programming with Async and Await (MSDN)
async (C# Reference)(MSDN)
await (C# Reference)(MSDN)

Asynchrony in C# 5, Part One (Eric Lippert's blog)

Whenever a task is "awaited", the remainder of the current method is signed up as a continuation of the task, and then control immediately returns to the caller. When the task completes, the continuation is invoked and the method starts up where it was before.

Asynchronous Programming in C# 5.0 part two: Whence await? (Eric Lippert's blog)
Async articles from John Skeet's blog
Async/Await FAQ
What is thread doing after returning from async point? (SO)
What happens to an `awaiting` thread in C# Async CTP?

Thursday, 27 June 2013

Beginning Unit Testing with NUnit in Visual Studio

How to start with NUnit?

Download it from and install it. It should be installed at C:\Program Files (x86)\NUnit 2.6.2. (2.6.2 is the current release version in the time of writing this article)

How to organise my projects? Where to put unit tests?

Put the business logic of your app in a class library project (e.g. MyBusinessLogic.dll). Your main app (MyApp.exe) project references this dll. Put unit tests in a separate project (e.g. MyBusinessLogicTester.dll) but within the same solution. MyBusinessLogicTester.dll will reference nunit.framework.dll (located at C:\Program Files (x86)\NUnit 2.6.2\bin) and MyBusinessLogic.dll.

How to run NUnit tests?

In MyBusinessLogicTester project: go to Properties -> Debug. Set Start external program to C:\Program Files (x86)\NUnit 2.6.2\bin\nunit.exe and put MyBusinessLogicTester.dll in Command line arguments field.

Set MyBusinessLogicTester as a StartUp project in your solution and run it. Nunit GUI app (nunit.exe) will open and display all test fixtures and tests in the left-hand side tree view. You can select tests you want to run and then execute them.

If you're running MyBusinessLogicTester tests with ReSharper, make sure MyBusinessLogicTester is marked to be built in the Configuration Manager.

Links and References:

Thursday, 14 March 2013

Hello, world! in NSIS - the simplest NSIS script

NSIS requires two things to be present in a script:

1) call to OutFile function (in order to define installer's name and output directory)
2) at least one Section (NSIS script executes sections and each section can contain functions and instructions; functions contain instructions only)

So, the shortest script that compiles could be:


NSIS compiler creates HelloWorld-installer.exe in the same directory where HelloWorld.nsi resides. When run, installer looks like this:


If we click on Show details button, we can see the following:


We can use ShowInstDetails in order to make installer displaying details by default:

We can use DetailPrint in order to output our custom messages:

And so Installer displays our custom text and Details window by default:


If we want to perform some system checks when installer starts, we can place our code in predefined function .onInit. In the next example, we'll just put displaying message box:

When we run installer, message box is first displayed:


When we click on OK, main window shows:


I used instruction Name to customize title of Installer's windows.

We can do some more serious stuff in .onInit function, like Windows version check. Download GetWindowsVersion.nsh header, create directory headers in the same directory where HelloWorld.nsi is, place GetWindowsVersion.nsh in that new directory (as we want to keep downloaded scripts separately from our work), include that header in the script and call function it exposes - GetWindowsVersion:

Message box displays Windows version:


NOTE: Files with extension nsi are installer script files and files with extension nsh are installer headers.

NOTE: DetailPrint calls from .onInit are ignored.

Thursday, 10 January 2013

How to issue periodical execution of DOS command in a single line

Let's say we want to check the state of the socket connection on the port 4016 every second. We can use netstat command to display required information and ping invalid IP address with timeout of 1 second. These two commands can be grouped in the same line with & operator and that group can be put inside infinite FOR loop:

C:\Users\Scrillex>for /L %a in (0,0,0) do (netstat -an|find "4016" & ping - n 1 -w 1000 >nul)

C:\Users\Bojan>(netstat -an | find "4016" & ping -n 1 -w 1000 1>nul)
TCP [::]:4016 [::]:0 LISTENING

C:\Users\Bojan>(netstat -an | find "4016" & ping -n 1 -w 1000 1>nul)
TCP [::]:4016 [::]:0 LISTENING

C:\Users\Bojan>(netstat -an | find "4016" & ping -n 1 -w 1000 1>nul)
TCP [::]:4016 [::]:0 LISTENING

To stop the execution of this single line script use CTLR-C.

Friday, 28 December 2012

Safe release of resources

If class allocates unmanaged resources (database connections, sockets, window handles, bitmaps...) it has to implement IDisposable interface with a single method Dispose() which deallocates such resources. When user of this class does not need its instance any more, it has to make sure Dispose() is called - even in the case of an exception. This can be achieved by using try-catch-finally block or with using statement:

Some classes in .NET which implement IDisposable have method Close() along with method Dispose(). Dispose() usually calls Close(): it's perfectly safe omitting explicit call to Close() when using using statement.

Proper use of the IDisposable interface (SO)
close or dispose (SO)
Does Stream.Dispose always call Stream.Close (and Stream.Flush) (SO)