Calvin Hsia's WebLoghttps://blogs.msdn.microsoft.com/calvin_hsia
thoughts from a professional developerWed, 31 Jan 2018 03:02:07 +0000en-UShourly1Store different derived classes in collections in C++ and C#: CoVariance, shared_ptr, unique_ptrhttps://blogs.msdn.microsoft.com/calvin_hsia/2018/01/31/store-different-derived-classes-in-collections-in-c-and-c-covariance-shared_ptr-unique_ptr/
https://blogs.msdn.microsoft.com/calvin_hsia/2018/01/31/store-different-derived-classes-in-collections-in-c-and-c-covariance-shared_ptr-unique_ptr/#respondWed, 31 Jan 2018 03:02:07 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7595I wanted to create a collection container that would hold objects of various types, all derived from a common interface/base class.In C# and VB, this is easy. Just create a new List<MyBase>() and add elements to it, including various MyBase derived typesAdding elements that are more derived is Covariance (not to be confused with covariance in statistics).

I wanted to do the same in C++. I’ve been using the STL containers, and knew that I could easily to this by storing references to the objects in e.g. a vector.This would require my code to manage the creation and destruction of the objects, meaning memory management.Wouldn’t it be nice if I could write the code without any memory management at all? In other words, no operator new or delete and no “*” operator: just use smart pointers.

std::shared_ptr and std::unique_ptr are really handy here.

Below is a sample showing both C# and C++ covariance in containersThe memory allocation calls (malloc and free) are used only for diagnostics to ensure no memory leaks and can be removed.

Right click Solution in Solution Explorer->Add New Project->C++->Windows Desktop->Windows Desktop Wizard->”CppCovariance”Application Type-> Windows Application (.exe)Click on Empty Project CheckboxCppCovariance->Add->New Item->C++ FileIf you get this error:1>Project not selected to build for this solution configurationBuild->Configuration->CppCovariance->Check the “Build checkbox”Project->Addd->New Item->CPP Source file->CoVariance.cpp.Paste in the CppCode below

Right click on CppCovariance in the Solution explorer and choose “Set as Startup Project”

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2018/01/31/store-different-derived-classes-in-collections-in-c-and-c-covariance-shared_ptr-unique_ptr/feed/0Calculate Pi to measure processor performancehttps://blogs.msdn.microsoft.com/calvin_hsia/2017/12/30/calculate-pi-to-measure-processor-performance/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/12/30/calculate-pi-to-measure-processor-performance/#respondSat, 30 Dec 2017 06:03:20 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7586We know that computers can calculate very quickly, but how do we compare performance between code? I know that processors have been improving immensely since my first processor in 1971 (see https://blogs.msdn.microsoft.com/calvin_hsia/2005/10/30/my-toys-over-the-years/ ). As improvements come to processors, not all programs take advantage of them. As processor manufacturers come up with new improvements (such as 64 bit, SIMD SSE and AVX), programs must be recompiled to take advantage of the new available instructions. However, once so changed, the programs won’t work on existing computers that do not have these features.

I needed a math intensive calculation, that takes a while, so I used the Leibniz series to calculate Pi. The goal of the code below is to measure the time to calculate Pi using various options. The algorithm saysPi= 4/1 – 4/3 + 4/5 – 4/7 + 4/9….

Which settings yield the fastest program for you? 32 bit? 64 bit? AVX?

(as an aside, does your program run slower when running under the debugger?)

Start Visual Studio (most versions will do).Use Visual Studio Online to create a free online account which can host your programming projects. It can store your source code using either GIT or Team Foundation, allowing you to store multiple versions of your code, allowing various computers you may use to sync and coordinate changes. You can also invite others to be a part of your team project.I like to start this way because it creates a local repo that allows local commits that’s easy to push to VSOnline but it doesn’t require a push

From Team Explorer control bar, choose Projects->New Repository. Name it “CalcPi”

Then click on New… Solution->C++->Win32 Project->”CalcPi”. Click on Finish to accept all the defaults for the Win32 Project Wizard.Paste the code below to replace the contents of “CalcPi.cpp”

Let’s add a menu item “Run” with the short cut key “ctrl+R”. Double click the CaclPi.Rc file in the solution explorer to open the resource editor. Drill into CalcPi.rc->Menu->IDC_CALCPIRight click the Exit item->Insert New->”Run”

This generates a “ID_FILE_RUN” definition in the resource.h fileDrill into CalcPi.rc->Accelerator->IDC_CALCPICreate a line for “ID_FILE_RUN” that specifies a Ctrl-R accelerator key:

You can observe the assembly output by hitting breakpoints and viewing disassembly (Ctrl-F11). Or you can create an Assembly listing: Project->Properties->C/C++->Output Files->Assembler Output->”Assembly, Machine Code and Source (/FAcs)” Open the generated .COD file in the VS editor and examine it. Observe how the release version has optimized the code and inlined a lot of the functions.

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/12/30/calculate-pi-to-measure-processor-performance/feed/0Manipulate Visual Studio from Excel or Wordhttps://blogs.msdn.microsoft.com/calvin_hsia/2017/12/01/manipulate-visual-studio-from-excel-or-word/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/12/01/manipulate-visual-studio-from-excel-or-word/#respondFri, 01 Dec 2017 01:19:09 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7555You can use automation to manipulate various applications, like Word, Excel, Visual Studio, Visual FoxProAutomation can make many tasks easier, including testing.You can imagine something like a Word macro that launches Excel to create a concordance of the words in the current Document.

You can turn on macro recording and execute a few steps to see what kind of commands are available.

Similarly, you can start Word to create a new document, hit Alt-F11, etc.

You may ask yourself How does this work?

If you start RegEdit you can see the kinds of things you can create (Called Com Servers. You can easily create your own in FoxPro or )

Using Visual Foxpro (which also has an object model and is likewise automatable), ox = CREATEOBJECT("VisualStudio.Solution")dte=ox.DTEdte.Application.MainWindow.Visible=1dte.Solution.Open("C:\Users\CalvinH\Source\Repos\Bounce\Bounce\Bounce.sln")

Try the same code in Excel or Word. (Excel & Word use VBA, which requires the “Set” command when assigning an object to a variable:Sub t() Set ox = CreateObject("VisualStudio.Solution") Set dte = ox.dte dte.Application.MainWindow.Visible = 1 dte.Solution.Open ("C:\Users\CalvinH\Source\Repos\Bounce\Bounce\Bounce.sln")End Sub

FoxPro intellisense is nice here because it shows the available members/properties and it shows help information in a tooltipHow did I know this? I wrote the code for FoxPro intellisense.

It used to be I could create “VisualStudio.Application”, but thtat doesn’t seem to work any moer

Does this work from other office applications like Outlook and PowerPoint? Try it and see!

In Graph Poker Hand Distributions I showed some code to graph poker hands and the distributions of their values: e.g. how many times a Pair, Full House, etc. were found in sets of one thousand random hands.It shows that the probability of a Pair is roughly 42% of deals. A royal flush was .000169%, pretty rare

now you can add a “/” at the beginning of the line that says add a “/”That change causes the inserted lines not to be a comment any more and the original chart datasource code to be commented out.The new Datasource is the dictPairDist accumulates the number of pairs found in batches of 1000 deals: e.g. if the first batch of 1000 deals might get 400 pairs, so dictPairDist[400] == 1. Subsequent batches of 1000 deals with 400 pairs will increment that value.

You’ve probably heard of a bell curve, or a Normal Distribution. The Law of Large Numbers says that if you do an experiment (e.g. flip a coin) a large enough number of times, the average number of “heads” will be the expected value: e.g 50%

The Central Limit Theorem says that when independent random variables are added their sum tends to be a bell curve. If you define an experiment to be the number of heads from flipping a coin 1000 times, the expected value is 500 heads, but there will be some experiments where there are 499 heads, and even 300. The distribution of the actual value of the number of heads will be a bell curve with the center around the mean expected value (500). Similarly the number of pairs found in a batch of 1000 deals will be a normal curve centered around the value 420.A number called the Standard Deviation indicates the variation of a set of values. About 68% of values in a normal distribution fall within one standard deviation of the expected value. 95% are within 2 standard deviations, and 99.7% are within three standard deviations.

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/10/31/the-central-limit-theorem-normal-distribution-and-poker-hand-distributions/feed/0Create a Visual Studio Extension with minimal impact on VS startup performancehttps://blogs.msdn.microsoft.com/calvin_hsia/2017/09/19/create-a-visual-studio-extension-with-minimal-impact-on-vs-startup-performance/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/09/19/create-a-visual-studio-extension-with-minimal-impact-on-vs-startup-performance/#respondTue, 19 Sep 2017 01:53:36 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7465The good news: It’s very easy to create an extension in Visual Studio. The bad news: It’s also easy to make that extension have an adverse effect on the start time of Visual Studio.

Let’s examine how we can mitigate the impact on Visual Studio performance.

I called mine “VSIXTest”. A Getting Started page shows with instructions on how to add extensibility items to your project, like ToolWindows, Margins, etc.I chose to add a new Custom Tool Window and called it MyToolWindow.cs At this point, hit F5 to Start an experimental instance of VS with your code installed. Click the menu item Tools->Other Windows-> “MyToolWindow” to see your fancy toolWindow. You can move it around and dock it like any other tool window.

If you now shut down the experimental instance of VS (saving the window layout), hit F5 again, VS will restore “MyToolWindow”at startup, which means it runs your initialization code before allowing the user to interact with Visual Studio.

Now you’ll notice in solution explorer that your ToolWindow code lives in a Package that was created for you: “MyToolWindowPackage.cs”In that file, let’s paste some logging code:

The log shows time and elapsed time for each message. The thread shows “1” which means its running on the UI or main thread.There’s a lot of other unrelated stuff showing in the output window, but it’s easy to filter to our output: Shift-Ctrl-F (Find In Files), search for “MyExtensionLog” change the “Look In” to “Current Window” and click “Find All”. The search results show the line number at the beginning of each line in parentheses along with the log message

Now imagine that the ToolWindow needs to do a lot of expensive initialization work: in MyToolWindow class add this method to simulate the work with a Sleep:

That hard work is just sleeping for N seconds, but it includes some logging. Call it from the MyToolWindow constructor with a 10 second parameter.Find all "MyExtensionLog", Find Results 1, Current Document (2):02:03:47 0.0 MyExtensionLog 1 Initialize (3):02:03:47 3.0 MyExtensionLog 1 MyToolWindow (4):02:03:47 5.0 MyExtensionLog 1 Starting DoToolWindowInitWorkThatCanBeDeferred with 10 SecondsDeferredWork (16):02:03:57 10,010.6 MyExtensionLog 1 deferred work done

Now we can see the 10 second delay in the log, with all work on the main thread.VS will now appear unresponsive and VS startup has been delayed by 10 seconds, causing unhappy users.

How do we fix that? (hint: why did we add Thread ID to the log?)In the ToolWindow Constructor, call a method called “doInitialize” which looks like this:

It’s not difficult to schedule work after VS starts, and that the work can be on a background thread or the main thread. To make users happy, minimize the work on the main thread so the UI is more responsive.

Note that there are many options to determine if Tasks should run or Continue, and that authors should understand that Tasks might not complete successfully and can be cancelled.Also, if you have a task that runs on the main thread for > 200 msecs, and you delay it til after Idle, then the user could be typing into the editor or moving the mouse and experiencing slow or blocking performance. You can break the work into smaller pieces and Yield after each.

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/09/19/create-a-visual-studio-extension-with-minimal-impact-on-vs-startup-performance/feed/0Graph Poker Hand Distributionshttps://blogs.msdn.microsoft.com/calvin_hsia/2017/08/31/graph-poker-hand-distributions/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/08/31/graph-poker-hand-distributions/#respondThu, 31 Aug 2017 00:38:56 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7405Last time I showed how to Calculate Poker hand probabilities with code that showed how often a particular hand occurred, such as a Pair or a Full House. Now that we can calculate these probabilities, lets graph them.

First we’ll accumulate the results of dealing 5 card hands into a Dictionary<string, int>, where entries might be “Pair” = 223, “Straight”=3

Windows Forms has a nice graphing assembly System.Windows.Forms.DataVisualization, which calls a graph a “Chart.Because the charting code is written for Windows Forms, and the Poker code uses Windows Presentation Foundation, we need to host the Windows Forms chart as a child of a WindowsFormsHost control.We’ll create a ChartArea and add it to the chart.Set the chart DataSource property to the dictionary. We’ll add a Series to the chart, with the type “Bar” for a bar chart. (Try changing it to a Line chart for fun)

Here’s a screenshot of 14 million deals, indicating visually that a Pair occurs just a little less than “Nothing” (42% vs 50% of the deals).

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/08/31/graph-poker-hand-distributions/feed/0Calculate Poker hand probabilitieshttps://blogs.msdn.microsoft.com/calvin_hsia/2017/08/01/calculate-poker-hand-probabilities/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/08/01/calculate-poker-hand-probabilities/#respondTue, 01 Aug 2017 01:44:42 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7385In the last post I showed some code that displays playing cards using WPF. Now we’ll have some fun with the cards.

Let’s calculate the probability of getting a particular poker hand. The program below shuffles the deck, then deals 5 cards by taking the first 5 cards into a “hand”. The hand is then evaluated to see if there is a pair, flush, etc.

Updating the User Interface is put into one task, and the calculations (shuffle, deal, evaluate) are put in another task.

The latter task is done in a background worker thread, allowing the Window to be responsive to the user.

Click on the “Go” button to stop/start the dealing to see partial results. You can control the number of deals per batch with the textbox.

There is test code in the comments at the end to help validate that the logic for hand evaluation is correct. Correctness can also be verified by comparing the probabilities to the well-known poker probabilities on the web.

Note that the code to evaluate the hands is based on Linq and is probably not the most efficient.

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/08/01/calculate-poker-hand-probabilities/feed/0Display Playing cards in WPFhttps://blogs.msdn.microsoft.com/calvin_hsia/2017/06/30/display-playing-cards-in-wpf/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/06/30/display-playing-cards-in-wpf/#respondFri, 30 Jun 2017 00:49:38 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7375I remember each version of Windows had new games. On Windows 3.1 many people played Solitaire. Windows for Workgroups 3.11 introduced Hearts. Somewhere along the way came FreeCell, MineSweeper and Spider Solitaire. This was years before more modern User interfaces, such as WPF existed.

Windows no longer ships Cards.dll which has the images of the cards and some routines that could be called to draw the cards using older techniques, such as Drawing Contexts and GDI.

Using more recent drawing techniques, such as Windows Presentation Foundation (WPF), and more modern languages, such as C# or VB.Net can make programming games easier.

Below is some sample code that extracts bitmaps from a the native Cards.Dll and converts it to a WPF BitmapSource which can be scaled and displayed as an Image in WPF. A timer shuffles the cards. You can click on the form to pause the shuffling.

You can use this code as a basis of a card game. Before writing the code, I was toying with the idea of drawing the cards on a WPF form using WPF Immediate Mode graphics or a combined Immediate and Retained mode graphic system, with HWndHost

Solitaire used a Dll in the Windows\System32 folder called Cards.dll. Because many of you may not have access to a Windows XP machine, I’ve made it available here.

You can open a native DLL, like Cards.dll directly in Visual Studio to examine and edit the resources in the resource editor: File->Open->File->Cards.dll

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/06/30/display-playing-cards-in-wpf/feed/0Debugging Monkeys on a Raspberry Pihttps://blogs.msdn.microsoft.com/calvin_hsia/2017/05/31/debugging-monkeys-on-a-raspberry-pi/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/05/31/debugging-monkeys-on-a-raspberry-pi/#respondWed, 31 May 2017 00:31:30 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7335The last couple times I wrote about using a RaspBerry Pi and Windows IoT:

I was contemplating getting Monkeys working on an Arduino, but that would have required a lot more effort (I’d have to implement a lot of the base functionality, like collection classes, hash tables, I/O, etc.) and it would not have enough memory to generate the text. The Pi has more memory and using the .Net framework dictionary and async support made the task a breeze.

Below is some sample code showing how we can get the Shakespearean monkeys working on the Raspberry Pi. Note that the code also opens a General Purpose I/O pin (if available on the current platform). It then toggles the pin high and low in the timer. So you can connect, e.g. an LED to Pin 5 and see it flash as the code runs.

The difference in the heart of the C# algorithm is pretty small. The original program was a Console app, and the IoT version is a Universal Windows App, which can run on an Xbox, IoT, Windows, HoloLens, and Windows Phone.

You can set up your environment to be Local Machine (x86 (32 bit), x64 (64 bit)), or remote machine ARM (the Raspberry Pi), or Device (like a USB connected Windows Phone).

Once you set up your environment, the debug experience is essentially the same for each platform. You can set breakpoints, tracepoints, view disassembly, single step, SetNextStatement, examine values, etc.

You can even view the machine registers and single step the Jitted code. Hit Ctrl-F11 (show disassembly), right click and Show Code Bytes.

Here’s a view at a breakpoint for ARM, showing the Raspberry PI register values, the assembly code and the machine code. The memory window is showing the executable code at the current PC (Program Counter) 0x05ADF0AC. You can twiddle the register values and even the memory contents

Changing the platform to x64 (Local machine) you can see that the registers are 64 bits wide and the machine

And here’s the same for x86:

Seeing the Jitted code and how it differs hugely depending on the platform makes one appreciate the work done under the covers to make the same code run identically on multiple platforms. Also, the debugger knows how to disassemble the binary to asm code for each platform.

Btw, it’s very useful to remember that 0x90 is a NOP or No Operation for x86 (and, interestingly, x64). When debugging a scenario and I want to comment out some code, I just replace each of the code bytes in the Memory Window with 0x90 so it won’t be executed. This is much faster than stopping debugging, rebuilding, redeploying, replaying the debug scenario to the breakpoint. (Of course, Edit And Continue is similarly very helpful, but not always available) Similarly, a 0xCC is an Int 3, which is the equivalent of a DebugBreak(), which causes a breakpoint.

Try it yourself: add the line nChars = 77; Just before the line, replace the code bytes with 0x90. This works for both x86 and x64. It’s handy to have a single byte NOP instruction. I couldn’t find one for the ARM, which uses MOV R0, R0 (0xe1a00000). Then single step through the NOPs you just replaced and notice that the added line is not even executed (nChars stays as 0).

Another debugger feature I like for managed code is the Make Object Id. Right click on a value in the Watch or Locals windows and choose Make Object ID. That particular instance will now be displayed with the suffix {$1}, {$2}, etc. If you have many instances of the same type, this is very handy to distinguish each instance. Managed code needs this because the object’s address can change through Garbage Collection.

BTW, I can’t emphasize this enough: the Set Next Statement command (Ctrl Shift F10) is extremely powerful: you can change the next line of code that will be executed. So you can re-execute or skip code easily. Combined with twiddling variable values, this is a big time saver.

Software Development is a cycle of:

Write code

Deploy (could be automatic)

Debug code

Change code

Repeat as necessary

Of course my favorite development time saver is not debugging at all: use Test Driven Development, where you run a test to execute your code and observe the results. Then you can use released build optimized code most of the time, run tests while coding, verify you didn’t break something, etc.

Write test and code simultaneously

Run test

Change code

Repeat as necessary

Download WarAndPeace.txt and add it to the VS Solution. Change it’s “Copy to OutputDirectory” Property to “Copy if newer”. This will deploy the file with the application. Or, you can use the built in Hamlet Act III.

]]>https://blogs.msdn.microsoft.com/calvin_hsia/2017/05/31/debugging-monkeys-on-a-raspberry-pi/feed/0Create a security system with cameras on your Windows IOT Raspberry Pihttps://blogs.msdn.microsoft.com/calvin_hsia/2017/04/29/create-a-security-system-with-cameras-on-your-windows-iot-raspberry-pi/
https://blogs.msdn.microsoft.com/calvin_hsia/2017/04/29/create-a-security-system-with-cameras-on-your-windows-iot-raspberry-pi/#respondSat, 29 Apr 2017 02:14:14 +0000https://blogs.msdn.microsoft.com/calvin_hsia/?p=7275Last time I wrote about getting started using a Raspberry Pi and Windows Internet Of Things. This time I played around with adding USB Web cameras to the Raspberry Pi.

The code below will dynamically check for cameras as they get connected and disconnected from the device. It runs on Windows IoT, so works on a PC, Raspberry Pi, Windows Phone, Xbox.

The code has a timer that checks for cameras and takes a picture every tick, defaulting to 7 seconds. If there are multiple cameras, it will alternate cameras. I’ve had a Pi going with 4 USB cameras and a touchscreen display from a single 5V power supply, continuously taking pictures for weeks.

I noticed after a while that the clock would drift. After a many hours of taking pictures, the time might be off by several minutes. A little research showed that the Pi does not have a Real Time Clock (the electronic equivalent of a wrist watch). Because it can easily connect to a network, I added some code I found to look up the current time on the network. This code conditionally runs on the Pi. On other devices, it just uses the host OS time function.

I could use the Windows IoT Remote Client to see the camera images from any machine on the network. So I could place the cameras in an area I want to secure, and view the pictures remotely.

Several different cameras were hooked up and all seemed to work to some degree. I found that the LifeCam Show was very good at lighting and the pictures were pretty good. The LifeCam VX-5000 was good too, but not as good at adjusting to lighting conditions. The LifeCam Studio didn’t seem to work as well (although it worked fine attached to a Windows PC). I found a generic WebCam for which I don’t know the manufacturer, and it seemed to work fine.

Some enhancement ideas:

Upload the pictures to a server

Use a motion detector to trigger photos, and perhaps even a light, sending email or calling the police

Play around with video and sound

This setup can be used to take time lapse pictures of, say, a construction site, weather, or the daily position of the sun in the sky