Calvin Hsia's WebLog

thoughts from a professional developer

Browse by Tags

Tagged Content List
  • Blog Post: Execute tasks in parallel using async and await to speed up computation

    Suppose you have five tasks you want your computer to execute and that each takes 1 second to execute. In the old days, one would figure that each task would be executed sequentially, resulting in a total execution time of 5 seconds. Even today, many programmers would write a program to execute these...
  • Blog Post: Create your own Process Explorer

    I was playing around with showing some resizable content in a WPF window. I wanted 2 variable sized Lists, one on top of the other. The lists had various lengths, but also various widths: the user could adjust the size of the columns. This smelled like a GridSplitter . That means I need 3 rows in a grid...
  • Blog Post: Write native to managed interop code: reg free COM

    If you followed the steps of the last post ( Call C# code from your legacy C++ code ), then your C++ program simply started the CLR (Common Language Runtime), invoked a C# method that returned the length of the passed in string argument. Not very exciting. Let’s spice it up a little. We’d like to allow...
  • Blog Post: Call C# code from your legacy C++ code

    For many decades, folks have been quite productive in creating C++ code. These “legacy” apps have been quite useful. However, as most software developers know, software needs to be maintained. Business needs change, and the applications need to change with it. Sometimes a complete rewrite of an application...
  • Blog Post: Debugger features help to find memory leaks

    You can use the debugger $CALLSTACK and TracePoints to find memory leaks. Memory leaks are very tedious to find. Often they don’t affect an application at all except a gradual performance slowdown on a customer machine. Leaks can be found in old code bases that have multiple authors over years, with...
  • Blog Post: Create managed Tests for native code

    In the old days of code development, the developer would do several steps repeatedly: 1. edit the code 2. Save 3. Compile 4. Link 5. Deploy (if necessary) 6. Start (or switch to) the debugger 7. Start the app under the debugger. 8. Examine the code behavior changes with breakpoints and other debugger...
  • Blog Post: Fish vs Sharks: Predator Prey simulation

    Many years ago, A.K. Dewdney wrote an article in Scientific American Magazine (December 1984) about a world called Wator, which is a world of fish and sharks. The world is a cellular automaton with each cell being in one of three states: empty, a shark, or a fish. The world is calculated repeatedly,...
  • Blog Post: Logo Turtle Graphics in WPF

    Logo is a programming language that somehow involves a turtle. Imagine a turtle on a large piece of paper on the floor. The  turtle has a pen. You can command the turtle to go forward, turn right, then repeat 4 times to draw a square. The last time I wrote about Logo (in 2006) I hadn’t read the...
  • Blog Post: How to monitor and respond to memory use

    A colleague asked me how to run code in response to low memory condition. Apparently, data is buffered and can be flushed to disk or a server when memory gets low. So I showed him the code below. Start Visual Studio File->New->Project->C# WPF application Paste in the code below in the MainWindow...
  • Blog Post: Async methods failures can be hard to diagnose

    Using asynchronous programming can make your application be more responsive and seem faster. However, beware of possible failures that are hard to diagnose. I’ve just been analyzing some of these crash reports that are sent to Microsoft from the Problem Reporting Settings (below). A big part of writing...
  • Blog Post: The Enum changed to int: How do you convert the int back to a name?

    I wrote a tool that loaded some binary data and displayed it in a nice form, where the user could browse the data, sort by various columns, filter, export, etc. The tool relied on the fact that a column of the data contained an integer ID indicating the type of item. For our purposes, let’s say that...
  • Blog Post: See how often your code runs and how much time it takes

    Last post I showed how to create your own CLR profiler. This time, we’ll add a little code to run whenever a managed method is entered and exited. We’ll do this by signing up for the  Enter and Leave function hooks by calling ICorProfilerInfo2->SetEnterLeaveFunctionHooks2 . This will allow us...
  • Blog Post: Create your own CLR Profiler

    A CLR profiler is a very powerful way to examine your managed code. Let’s create your own CLR Profiler. This simple example will 1. Start the specified managed application that we will profile 2. Intercept the creation of all managed objects 3. Calculate the class name for the object (Like “System.String...
  • Blog Post: Use reflection from native C++ code to run managed code

    In the prior post ( Use Reflection to create instances of objects ) I showed how to create a plain C# console application that has no special references (including none to any WinForms assembly) that can load and invoke a WinForm.Exe program. Today, we’ll look at doing the same thing from a plain old...
  • Blog Post: Use Reflection to create instances of objects

    You can use reflection (specifically classes in the System.Reflection namespace) to create instances of objects. Last time ( Create and play your own Breakout game ) , I showed a Windows Form application that is a Breakout game. This time, let’s see if we can create and run that WinForm game (Paddle...
  • Blog Post: Create and play your own Breakout game

    I remember pouring quarters into a video game called Breakout a few decades ago. The summer of 1975, I spent at Rensselaer PolyTechnic Institute (RPI) in Troy, NY. I was taking a structured programming course using Fortran on an IBM 360, using punched cards. The student union building had an arcade in...
  • Blog Post: Use status events to log and analyze an application

    Applications can get quite complex, with multiple components, assemblies, subsystems, etc. Understanding this complexity can be daunting. An old but still very effective way of analyzing code is to modify the code to output a string whenever that section of code gets executed. Remember the old Printf...
  • Blog Post: Does the CLR release memory when no longer needed?

    A colleague asked the other day if the CLR releases memory when it’s no longer needed. Suppose you allocate lots of memory, then release it. The CLR will grow the managed heap (the green below), but it will also shrink it if it can. All memory in a process must come from a pool of virtual addresses,...
  • Blog Post: Compiling code at runtime and MVP program is 20

    This month marks the 20 th anniversary of the Microsoft MVP program, so MVPs were in the news. Apparently I had a lot to do with the founding of the program J Wikipedia: http://en.wikipedia.org/wiki/Microsoft_Most_Valuable_Professional Microsoft News: http://www.microsoft.com/en-us/news/features/2013...
  • Blog Post: Windows Error Reporting and the Appdomain.UnhandledException Event

    Sometimes applications fail. If you are the author and it fails on your machine, typically you fire up the code in a debugger, figure out the issue, fix it, and rebuild the code. If the applications is out in the wild, perhaps with millions of users, it gets much more complicated. Yet, the goal is still...
  • Blog Post: Create a logger for watching your test progress as it runs.

      I was creating a project that’s a Windows Service, which has components running in multiple processes, using multiple threads and named pipes for communication. To understand the behavior of the code, I wanted to have accurate logging that: · The timing was very critical, so I wanted the log to...
  • Blog Post: Turn your tests into stress tests easily

    It’s great to be able to write tests and execute them while developing a project. While I’m developing, I can hit a button and run the dozens of tests to see if I’ve broken anything. As code gets written lots of things get refactored, moved around, etc. Hitting a button to see if I broke anything is...
  • Blog Post: Its easy to Create your own FrameWorkElement in WPF

    Sometimes you want to have very fine control of how your application looks and behaves. Other times, you just want to draw something on your UI. It’s pretty easy to create your own control class and put instances of it on your form. The sample below create a new class MyFrameworkElement which allows...
  • Blog Post: The number of Garbage Collections indicate how much memory is used

    One of the performance improvements we made in .Net was with System.Text.StringBuilder . StringBuilder is used in lots of code to build strings: it has various methods to modify strings quickly. Once a string is built, the ToString method is called to create the string. We observed via MemSpect that...
  • Blog Post: Examine the layout of managed strings in memory

    Suppose you wrote some C# code like this: var str1 = "ThisIsAString" ; var str2 = "ThisIsAnotherString" ; As you’d expect, each string is stored in the resulting built binary and also in memory when the binary is loaded, resulting in 2 separate strings. Now suppose...
Page 1 of 3 (59 items) 123