Upgrade your butler to Async

The async and await keywords have been around for a while (since .Net 4.5) yet there still remains a fair amount of confusion regarding how it works. First off, while the compiler is arguably doing something akin to magic, you could achieve much the same results with BeginInvoke and EndInvoke. The async/await way of doing things makes the code more natural to write and reason about but there is nothing new in the operating system to make this magic.

A basic thing async/await does for you is to allow scalability by allowing your thread to go off and do other work while you wait on some asynchronous activity. It does not mean you spin up new threads and do processing in parallel (though, you could). I’ll describe this in non-technical terms to make it clear.

Suppose you have a butler and we will say he is a synchronous butler. Your butler has been tasked with folding your underwear but you decide to order pizza so you tell your butler to stop folding and go wait at the door for the delivery. Your butler is effectively blocked from doing anything while the pizza is cooked and delivered. When the doorbell rings he will open the door, pay the driver, serve your pizza then get back to folding your spiderman underoos.

What a waste. Why can’t your butler keep folding clothes while the pizza is on its way? If you got the asynchronous model he easily could. He would call to order your pizza, put the task of answering the doorbell on hold, then go do other work. When the doorbell rings, he will pick up his task of serving you pizza where he left off and get the door.

That is the very basics of async/await. When you await a task the thread is free to do other work but it will come back and finish up when it needs to. This provides scalability by not wasting threads and forcing them to sit around idle. You are allowing your application and the operating system to operate more efficiently.

 

Input, Output and Input/Output SqlParameter Behavior Explained


In this article I will explain the sometimes surprising behavior of  the ADO.Net SqlParameter and particularly the effects of ParameterDirection, null value and DBNull.Value on how the parameter is sent to the database.

The companion VisualStudio 2015 solution contains the database project and unit tests for each scenario outlined below.

Continue reading

3 Ways to Create a MS Word Table in Office.js Add-In

Here are some handy code snippets for inserting a table into a Word document through a Task Pane Add-In. If you don’t know what that is, you can get up and running with this hello world example.

I demonstrate inserting at the selection, inserting at a named binding and inserting HTML. There is another way, you can insert raw Office Open XML (OOXML), and while that provides the most power and flexibility, it is also by far the most complex way to do it. Continue reading

Execute a Java process in a .Net Azure WebJob

I recently had the need to execute a .jar file from within an Azure WebJob built with C#. I found a few resources that showed how to upload a .zip file, for instance, and have that run  a .bat file as a job. Or, how to create a Java web app. But my webjob needed to do more than just run a .jar, and I didn’t have the time to get up to speed on Java and the Azure Storage SDK for Java. As the Google let me down, here I demonstrate the solution. Continue reading

OUTPUT parameters are the most efficient way to retrieve a single row from a Stored Procedure

T-SQL stored procedures that return a single a value (scalar) or a single row can benefit from the use of OUTPUT parameters. This is not appropriate for multiple row result sets, but if you just need one value, or the results of a single row, you can do it most efficiently with OUTPUT parameters as opposed to SELECTing into a DataReader or DataSet. Continue reading

A Basic Example of CRUD with DataGridView in VB.Net

Intended for new developers, or at least new to ADO.Net, this post will demonstrate how to Create, Read, Update and Insert data into a MS SQL Server database. We will use a VB.Net winforms project, DataGridView and SqlDataAdapters. You can pull down the complete project from GitHub here.

UPDATE: I have also published a similar example application done with Code First EntityFramework 6.

The finished Form looks like this:
finishedform
Continue reading