Calvin Hsia's WebLog

thoughts from a professional developer

Browse by Tags

Tagged Content List
  • 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...
  • Blog Post: Increase the memory available to your tests

        I love having test projects included in my solutions. Software is alive. I’m constantly making improvements/changes/fixes. When I have customers asking for various features in my code, or for code improvements, being agile and able to publish a changed build with utmost confidence relies...
  • Blog Post: Create multiple threads to see if work is done simultaneously

    When you hear about threads and threading, you hear that they allow concurrent execution of code, which is supposed to lead to better performance. Most computers nowadays have more than one CPU, meaning that each CPU can be executing code at the same time. Hit Ctrl-Shift-Escape to bring up Task Manager...
  • Blog Post: Be careful about nothing in managed code

      Here’s a pattern of code use I’ve seen in a few places. There’s a function DeserializeList that returns an array of various sizes, depending on the input. This code can be called to deserialize (rehydrate) an object from a stream. For example, an XML deserializer might create an XML node from...
  • Blog Post: Advanced debugging: change your program execution without Edit and Continue

    Last time ( Improve your managed debugging skills: examining registers and memory ) we examined some debugging techniques to understand the behavior of managed code. It might take a long time or many manual steps to reproduce a particular software behavior in an application. Changing the instructions...
  • Blog Post: Improve your managed debugging skills: examining registers and memory

      I was helping a colleague and we were deep in the middle of a debug session, single stepping some code and we wanted to see a value in the debugger. The debugger showed either nothing, because the intermediate value has been optimized out, or a message like “cannot display value, possibly because...
  • Blog Post: Automatic tests protect your code

      Last month   in   Dynamically create huge tooltips in WPF TreeView and ListView I showed some code that creates large tooltips to present lots of data.   Today, we’ll talk about creating automatic tests for this feature.   Testing User Interface features has been difficult...
  • Blog Post: Returning data from a recursive method

    Sometimes you need to write a method that requires recursion to be elegant. Often that method needs to accumulate data between recursion levels There are many ways to do so, and some are more elegant than others. I like the Optional Parameter method: it requires no additional class or static (shared...
  • Blog Post: The cost of using nothing

    What is the cost of using nothing? Seems like a silly question. Suppose you see code like this: void * MyClass::DoSomething() { int size = sizeof(MyThing) * m_NumItems; var ptr = malloc(size); return ptr; } It’s a method that allocates space for m_NumItems MyThings...
Page 1 of 3 (51 items) 123