Monday, November 12, 2018

Wix (Windows Installer) changing the color of a control

This is a simple gotcha related to changing the color of a control. Let's say you have a label that you want to present in a different color. Normally you would do something like this:
<!-- I put it somewhere in Product.wxs -->
<TextStyle Id="WixUI_Font_Normal_Red" FaceName="Tahoma" Size="8" Red="255" Green="55" Blue="55" />
 
<!-- somewhere in your UI -->
<Control Id="LabelRed" Type="Text" X="62" Y="200" Width="270" Height="17" Property="MYPROPERTY">
     <Text>{\WixUI_Font_Normal_Red}!(loc.MYPROPERTY)</Text>
</Control>

Yet for some reason, it doesn't work when the control is a checkbox, for example. The simple explanation is that this is by design: only text controls can change color. The solution is to split your control into the edit control without a text, then add a text control next to it with the color you need.

Here is an example of a checkbox that changes the label color based on the check value:
        <Control Id="DoNotRunScriptsCheckbox" Type="CheckBox" X="45" Y="197" Height="17" Width="17" Property="DONOTRUNSCRIPTS" CheckBoxValue="1"/>
 
        <Control Id="DoNotRunScriptsLabel" Type="Text" X="62" Y="200" Width="270" Height="17" CheckBoxPropertyRef="DONOTRUNSCRIPTS">
          <Text>!(loc.DoNotRunScriptsDescription)</Text>
          <Condition Action="hide"><![CDATA[DONOTRUNSCRIPTS]]></Condition>
          <Condition Action="show"><![CDATA[NOT DONOTRUNSCRIPTS]]></Condition>
        </Control>
 
        <Control Id="DoNotRunScriptsLabelRed" Type="Text" X="62" Y="200" Width="270" Height="17" CheckBoxPropertyRef="DONOTRUNSCRIPTS">
          <Text>{\WixUI_Font_Normal_Red}!(loc.DoNotRunScriptsDescription)</Text>
          <Condition Action="hide"><![CDATA[NOT DONOTRUNSCRIPTS]]></Condition>
          <Condition Action="show"><![CDATA[DONOTRUNSCRIPTS]]></Condition>
        </Control>

Wix Setup (Windows Installer) saving user input values

So you have one of those annoyingly XMLish setups from Windows Installer and you want to preserve the values you input so they are prefilled at future upgrades. There are a lot of articles on the Internet on how to do this, but all of them seem to be missing something. I am sure this one will too, but it worked for me.

Let's start with a basic setup.
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <UI Id="DatabaseAuthenticationDialogUI">
      <Property Id="DATABASEDOMAIN" Secure="yes"/>
      <Dialog
        Id="DatabaseAuthenticationDialog"
        Width="370"
        Height="270"
        Title="[ProductName] database authentication"
        NoMinimize="yes">
        <Control Id="DatabaseDomainLabel" Type="Text" X="45" Y="110" Width="100" Height="15" TabSkip="no" Text="!(loc.Domain):" />
        <Control Id="DatabaseDomainEdit" Type="Edit" X="45" Y="122" Width="220" Height="18" Property="DATABASEDOMAIN" Text="{80}"/>

So this is a database authentication dialog, with only the relevant lines in it. We have a property defined as DATABASEDOMAIN and then an edit control that edits this property. Ideally, we would want to make sure this property is being saved somewhere at the end of the install and it is retrieved before the install to be populated. To do this we will first define a DATABASEDOMAIN_SAVED property and load/save it in the registry, then link it with DATABASEDOMAIN.

First, there is the issue of where to put this code. Personally, I put them all under Product, as a separate mechanism for preserving and loading values. I am sure there are other places in your XML files where you can do it. Here is how my Product.wxs code looks like (just relevant lines):
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"
     xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
  <Product ... >
    <Property Id="SAVED_DATABASEDOMAIN" Secure="yes">
      <RegistrySearch Id="FindSavedDATABASEDOMAIN"
                      Root="HKLM"
                      Key="SOFTWARE\MyCompany\MyProduct"
                      Name="DatabaseDomain"
                      Type="raw" />
    </Property>
    <SetProperty Id="DATABASEDOMAIN" After="AppSearch" Value="[SAVED_DATABASEDOMAIN]" Sequence="ui">
      <![CDATA[SAVED_DATABASEDOMAIN]]>
    </SetProperty>
  </Product>
 
  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFiles64Folder">
        <Directory Id="MyFolder" Name="MyFolder">
          <Directory Id="INSTALLFOLDER" Name="MyProduct">
            <Component Id="InstallFolderComponent" Guid="c5ccddcc-8442-49e8-aa17-59f84feb4deb">
              <RegistryKey
                Root="HKLM"
                Key="SOFTWARE\MyCompany\MyProduct"
                >
                <RegistryValue Id="DatabaseDomain"
                               Action="write"
                               Type="string"
                               Name="DatabaseDomain"
                               Value="[DATABASEDOMAIN]" />
                </RegistryKey>
              <CreateFolder/>
            </Component>
          </Directory>
        </Directory>
      </Directory>
    </Directory>
  </Fragment>
</Wix>

This is what happens:
  1. We search in the registry and set the value for DATABASEDOMAIN_SAVED.
  2. We set the DATABASEDOMAIN value from the DATABASEDOMAIN_SAVED value, if that is set. Note that Sequence is set to "ui". This is very important, as the default value is "both". In my case I spent hours to figure out why the values were written in the registry, but then would never change again. It was because there are two sequences: "ui" and "execute". The code would read the value from the registry, the user would then change the values, then, right before installing anything, the value would be read from the registry AGAIN and would overwrite the user input.
  3. Finally, when we install the product we save in the registry the value of DATABASEDOMAIN, whatever it is.

This should be it, but there are a few gotchas. One of them is checkboxes. For Windows Installer the value of a checkbox either is or isn't. It's not set to 0 or 1, true or false or anything like that. So if you save the value attached to an unchecked checkbox control, when read, even if empty, it will be set. Your checkbox will always be set from then on. The solution I used was adding a prefix, then setting the value for the checkbox only if that value is what I expect it to be. Here it is, in a gist:

    <!-- Product.wxs -->
    <!-- this doesn't change -->
    <Property Id="SAVED_DONOTRUNSCRIPTS" Secure="yes">
      <RegistrySearch Id="FindSavedDONOTRUNSCRIPTS"
                      Root="HKLM"
                      Key="SOFTWARE\MyCompany\MyProduct"
                      Name="DoNotRunScripts"
                      Type="raw" />
    </Property>
    <!-- here, however, I check for Val1 to set the value of the property to 1 -->
    <SetProperty Id="DONOTRUNSCRIPTS" After="AppSearch" Value="1" Sequence="ui">
      <![CDATA[SAVED_DONOTRUNSCRIPTS = "Val1"]]>
    </SetProperty>
    <!-- Note the Val prefix when saving the value -->
    <RegistryValue Id="DoNotRunScripts"
                   Action="write"
                   Type="string"
                   Name="DoNotRunScripts"
                   Value="Val[DONOTRUNSCRIPTS]" />
 
    <!-- DatabaseSetup.wxs -->
    <!-- Note the checkbox value -->
    <Control Id="DoNotRunScriptsCheckbox" Type="CheckBox" X="45" Y="197" Height="17" Width="17" Property="DONOTRUNSCRIPTS" CheckBoxValue="1"/>

I Hope that helps people.

Sunday, November 11, 2018

Artemis, by Andy Weir

book cover The Martian was a total surprise as it took the world by storm and became beloved by all science, tech and fast talking pun lovers in the world. Artemis, on the other hand, will not be. Andy Weir writes another book about a supersmart tech savvy character with a great sense of humor, this time solving technical problems on the Moon. Is it as good as The Martian? I don't know, because now I have a template and knew what to expect. Without the element of surprise, it's difficult to compare the two books. Yet I finished reading the novel in a day. That's gotta count for something.

Stylistically it is first person, filled with dialogue and action. It's incredibly easy to read. This time, the main character is a young woman and the location is a city on the Moon. Andy Weir is nothing if not optimistic, so in his book we do get there. I thought the action was following the pattern of danger-action-solution too closely, so much in fact that at one time I saw the entire story as a big adventure game. I will bet you lots of slugs that Weir loved Sierra games as much as the next geek (that being me).

Bottom line is that I liked the book, I gobbled it up and enjoyed it to the end. I didn't like Jazz Bashara as much as Mark Watney and, while the technological descriptions kept me interested, I still think that space mechanics and Martian farming trump EVA shenanigans and vacuum welding any time. It was still damn entertaining, though.

Saturday, November 10, 2018

Oryx and Crake (MaddAddam #1), by Margaret Atwood

book cover All in all I liked Oryx and Crake. Margaret Atwood is writing in a very technical manner, at least it felt like that to me, with scenes and characters constructed in a certain way. That is both instructional and a bit off putting, because once you become aware of it, it distracts from the plot. The plot itself is simple enough: the world has ended and there is this half crazed old man who is reminiscing over what happened. At first it is all unnervingly vague, with things being kept from the reader as the feelings and mental issues of the main (and single) character of the book are being declared. Then, when vagueness cannot be sustained anymore, it is all told as a linear story of what happened, with clear explanations of everything. No mystery left.

To me, it felt like Atwood wanted to warn about the way we treat our world and she just exaggerated some things while letting others be whatever she wanted them to be. From a scientific and sociological standpoint some of the things described are spot on, others are complete bogus. I would go into details, but I don't want to spoil the story. One example is the marketing brands that she cooked up and that are all (and I mean all) phonetically spelled descriptions or sometimes puns. All companies, all product names, all new animal names are like that and it gets really tiresome after a while. Also people are able to do wonderful (or horrible) new things in some areas and not advance at all in others. The timeline is a little screwed up, too.

My favorite quote, the one that made me think the most, was: "All it takes," said Crake, "is the elimination of one generation. One generation of anything. Beetles, trees, microbes, scientists, speakers of French, whatever. Break the link in time between one generation and the next, and it's game over forever" . He is referring to technology after a post apocalyptic event. There are few people that understand any instructions left by makers of advanced technology and the ones that can are ill equipped for survival.

The book does feel like it was written as standalone, with the possibility of being continued, which is a good thing: no ending cliffhangers and too much setting up the next books. The title is weirdly misleading, though, as both Oryx and Crake appear very little in the memories of the main character. Why was the book named so? It makes little sense. I am not sure if I will be reading the next books in the MaddAddam trilogy.

Bottom line: good, although a tad bland writing. A single character reminiscing is all that happens in the book. Grand ideas that most of the time make sense, but sometimes fall flat. Scientifically things don't go as she described.

Sunday, November 04, 2018

Analyzing memory leaks in .NET applications running in production

Intro


I had this situation where an application that was running in production was getting bigger and bigger until it would devour all memory. I had to fix the problem, but how could I identify the cause? Running it on my computer wouldn't lead to the same increase in memory, what I wanted to see was why a simple web API would take 6Gb in memory and keep growing.

The process is simple enough:
  1. Get a memory dump from the running process
  2. Analyze the memory dump

While this post relates specifically to process memory dumps, one can get whole memory dumps and analyze those. There are two types of process memory dump formats: mini and full. I will be talking about the full format.

Fortunately, for our process, there are only two steps. Unfortunately, both are fraught with problems. Let's go through each.

Getting a memory dump from a running process


Apparently that is as simple as opening Task Manager, right clicking on the running process in the Details tab and selecting Create dump file. However, what this does is suspend the process and then copy all of its memory in a file in %AppData%/Local/Temp/[name of process].dmp. If the process is being watched or some other issue occur, the dumping will fail. Personally, I couldn't dump anything over 2Gb when I was trying to get the info from w3wp.exe (the IIS process) as the process would restart.

There are a lot of other ways of getting a process memory dump, including crash dumps, periodic dumps, event driven dumps. ProcDump, one of the utilities in the free SysInternals suite, can do all of those. The Windows Error Reporting (WER) system also generates dumps for faulty or unresponsive application or kernel. Other options for collecting a dump are userdum.exep, Windows Debugger (ntsd or windbg). Process Explorer, of course, can create two types of memory dumps for a process: mini and full.

Analyzing a process memory dump


This is where it gets tricky. What you would like is something that opens the file and shows a nice interactive interface explaining exactly what hogs your memory. Visual Studio does that, but only the Ultimate version gives you the memory analysis option. dotMemory from JetBrains is great, but it also costs a lot of money.

My personal experience was using dotMemory in the five days in which it is free and it was as seamless as I would have wanted: I opened the file, it showed me a pie graph with what objects were hogging my memory, I clicked on them, looked where and how they were stored and even what paths could be taken to create those instances. It was beautiful.

Another option is SciTech's .NET Memory Profiler, but it is also rather expensive. I am sure RedGate has something, too, but their trials are way to intrusive for me to download.

I have been looking for free alternatives and frankly all I could find is WinDBG, which is included in Windows Debugging Tools.

Windows Debugger is the name of the software. It is a very basic tool, but there are visualizers which use it to show information. One of them is MemoScope, which appears unmaintained at the moment and is kind of weird looking, but it works. Unfortunately, it doesn't even come close to what dotMemory was showing me. If analyzing a random .NET app dump showed that the biggest memory usage was coming from MainWindow (makes sense) which then had a lvItems that held most of memory (it was a ListView with a lot of data), WinDbg and therefore MemoScope show the biggest usage comes from arrays of bytes. And it also makes sense, physically, but there is no logical context. A step by step memory profiling guide using WinDbg can be found here.

Conclusion


I am biased towards JetBrains software, which is usually amazing, although I haven't used it a quite a while because their obnoxious licensing system (you basically rent the software). I loved how quickly I got to the problem I had with dotMemory. .NET Memory Profiler from SciTech is way cheaper and seems to point in the right direction, but doesn't really compare in terms of quality. Anything else I've tried was quite subpar.


Doing it yourself


As they say, if you want to do something right, you gotta do it yourself.

There is a Windows API called MiniDumpWriteDump that you can use programatically to create the dumps. However, as you've seen, that's not the problem, the dump analysis is.

And it's hard to find anything that is not very deep and complicated. Memory dumps are usually analysed for forensic reasons. There are specialists who have written scores of books on memory dump analysis. What's worse, most people focus on crash dumps, with memory leak analysis usually done while debugging an application.

I certainly don't have the time to work on a tool to help me with this particular issue. I barely had the time to look for existing tools. But if you, guys, can add information to this blog post, I am sure it will help a lot of people going through the same thing.

Pretty Pictures


I didn't find a nice way of putting the images in the post's text, I am adding them here.








Friday, November 02, 2018

MemoryCache.Default suddenly not working

MemoryCache has a really strange behavior where all of a sudden, without any warning or exception, it stops caching. You use Set or Add, it doesn't matter. It returns true for Add, it throws no error for Set, but the cache stubbornly remains empty. This behavior can be replicated easily by running this line:
MemoryCache.Default.Dispose();
From that moment on, MemoryCache.Default will not cache anything anymore.

Of course, you will say, why the hell would you dispose the default object? First of all, why give me the option? It's not like I can set the Default instance after I dispose it, so this breaks a lot of things. Second of all, why pretend to continue to be working when in fact it is not? Most disposable classes throw exceptions if they are used after being disposed. And third of all, it might not be you who does the disposing.

In my case, I was trying to be a good boy and declare all my dependencies upfront. I was using StructureMap as a IoC framework and for my class that was using a MemoryCache I declared a constructor parameter of the basest type I could: ObjectCache. Then I registered MemoryCache.Default as the instance used when needing an ObjectCache. The problem is that each unit test would initialize and tear down the StructureMap container and it would take all the instances used that implement IDisposable and dispose them, including our friend MemoryCache.Default. As I couldn't find a simple way to tell StructureMap to not dispose the object, I had to create a MemoryCacheWrapper that implemented ObjectCache and was NOT IDisposable and was using MemoryCache.Default for all methods and use that as the singleton instance for an ObjectCache in StructureMap.

If you are working with a .NET version prior to .NET 4.5, you may encounter the same issue with a bug in .NET: MemoryCache Empty : Returns null after being set

Tuesday, October 30, 2018

The Ends of the World: Supervolcanoes, Lethal Oceans, and the Search for Past Apocalypses, by Peter Brannen

Book cover This guy can write! I mean, Peter Brannen is writing about paleontology and climate change and the words just sing to you, make you see worlds that we actually know very little about and feel for ammonoids like there would be the cutest kittens, looking at you with big eyes and begging not to get extinct. As someone who wants to write himself someday, I really hate this guy. He is that good.

That being said, The Ends of the World is a book about the major extinctions in Earth's history, their causes and how they relate to our present lives. It's a captivating read, evoking lost worlds and very carefully analyzing how they disappeared and why. There is humor, drama, interesting interviews with fascinating people. Dinosaurs? Puh-lease. This takes you back to the good old days of the Ediacaran and slowly brings you around our time and even speculates on what could come after us, the hubris filled species that, geologically speaking, was barely born yesterday - or a few seconds ago, depending on how you measure it - and has the chance to outshine all the other species that came, ruled and went.

There is no way I can do it justice other than to say that I loved the book. In itself it's a summary of life on Earth so summarizing it myself would be pointless. I highly recommend it.

Here is the author, presenting his book at Google Talks:

Friday, October 26, 2018

Wix setup Error 0x8007007a: Failed to copy SqlScript.Script or Error 0x8007007a: failed to read SqlScripts table

You are trying to install an application and in the detailed log you get something like this:
InstallSqlData:  Error 0x8007007a: Failed to copy SqlScript.Script: SqlWithWindowsAuthUpv2.9.5UpdateGetInstallJobBuildByInstallJobBuildIdScript
or
InstallSqlData:  Error 0x8007007a: failed to read SqlScripts table

For me it was related to the length of an <sql:SqlScript> tag Id property. If you are trying to use more than 55 characters for the BinaryKey property, you get a build error that you should use a max of 55 characters. For the Id you get no such warning, but instead it fails on install.

I hope it helps people (I lost two hours figuring it out).

Sunday, October 21, 2018

What's Expected of Us and others, by Ted Chiang

book cover What's Expected of Us and Others is a collection of stories by Ted Chiang including
  • Exhalation
  • Dacey's Patent Automatic Nanny
  • The Truth of Fact, the Truth of Feeling
  • The Lifecycle of Software Objects
  • What's Expected of Us
  • The Merchant and the Alchemist's Gate
  • The Great Silence

All the stories are speculative fiction, their subjects related to the meaning of life and evolution in relation to technology. His writing style is direct, unsophisticated, often based on first person perspective and dialogue. It's the ideas that he is exploring where he spends most of his efforts. All in all a nice collection of sci-fi, but not something extraordinary in nature.

Friday, October 19, 2018

.Net execution of async methods synchronously

Sometimes we need to execute an async method synchronously. And before you bristle up and try to deny it think of the following scenarios:
  • event handlers are by default synchronous
  • third party software might require you to implement a synchronous method (like a lot of Microsoft code, like Windows services)

All I want is to take public async Task ExecuteStuff(param1) and run it with ExecuteSynchronously(ExecuteStuff, param1).

"What's the problem?", you might ask, "Just use SomeMethod().Result instead of await SomeMethod()". Well, it doesn't work because of the dreaded deadlock (a dreadlock). Here is an excerpt of the .Net C# code for Task<T>.InternalWait (used by Result and Wait):
// Alert a listening debugger that we can't make forward progress unless it slips threads.
// We call NOCTD for two reasons:
//    1. If the task runs on another thread, then we'll be blocked here indefinitely.
//    2. If the task runs inline but takes some time to complete, it will suffer ThreadAbort with possible state corruption,
//       and it is best to prevent this unless the user explicitly asks to view the value with thread-slipping enabled.
Debugger.NotifyOfCrossThreadDependency();
As you can see, it only warns the debugger, it doesn't crash it just freezes. And this is just in case it catches the problem at all.

What about other options? Task has a method called RunSynchronously, it should work, right? What about Task<T>.GetAwaiter().GetResult() ? What about .ConfigureAwait(false)? In my experience all of these caused deadlocks.

Now, I am giving you two options, one that I tried and works and one that I found on a Microsoft forum. I don't have the time and brain power to understand how this all works behind the scenes, so I am asking for assistance in telling me why this works and other things do not.

First of all, two sets of extensions methods I use to execute synchronously an async method or func:
/// <summary>
/// Execute an async function synchronously
/// </summary>
public static TResult ExecuteSynchronously<TResult>(this Func<Task<TResult>> asyncMethod)
{
    TResult result = default(TResult);
    Task.Run(async () => result = await asyncMethod()).Wait();
    return result;
}
 
/// <summary>
/// Execute an async function synchronously
/// </summary>
public static TResult ExecuteSynchronously<TResult, TParam1>(this Func<TParam1, Task<TResult>> asyncMethod, TParam1 t1)
{
    TResult result = default(TResult);
    Task.Run(async () => result = await asyncMethod(t1)).Wait();
    return result;
}
 
/// <summary>
/// Execute an async function synchronously
/// </summary>
public static void ExecuteSynchronously(this Func<Task> asyncMethod)
{
    Task.Run(async () => await asyncMethod()).Wait();
}
 
/// <summary>
/// Execute an async function synchronously
/// </summary>
public static void ExecuteSynchronously<TParam1>(this Func<TParam1,Task> asyncMethod, TParam1 t1)
{
    Task.Run(async () => await asyncMethod(t1)).Wait();
}

I know, the ones returning a result would probably be better as return Task.Run(...).Result or even Task.Run(...).GetAwaiter().GetResult(), but I didn't want to take any chances. Because of how the compiler works, you can't really use them as extension methods like SomeMethod.ExecuteSynchronously(param1) unless SomeMethod is a variable defined as Func<TParam1, TResult>. Instead you must use them like a normal static method: MethodExtensions.ExecuteSynchronously(SomeMethod, param1).

Second is the solution for this proposed by slang25 :

public static T ExecuteSynchronously<T>(Func<Task<T>> taskFunc)
{
    var capturedContext = SynchronizationContext.Current;
    try {
        SynchronizationContext.SetSynchronizationContext(null);
        return taskFunc.Invoke().GetAwaiter().GetResult();
    }
    finally {
        SynchronizationContext.SetSynchronizationContext(capturedContext);
    }
}

I haven't tried this, so I can only say that it looks OK.

A third options, I am told, works by configuring every await to run with ConfigureAwait(false). There is a NuGet package that does this for you. Again, untried.

On relativity

It is said that the great theory of relativity of Einstein's doesn't apply to things moving slowly. Today I realized that is not true. There is a direct relationship between space and time and speed affects space, so it must affect time. Here is a practical example: a car moves faster than a person walking, so its speed makes distance shrink relative to time. Inversely, that means that it makes time expand, become more expensive, from the car's point of view.

That is why, when you see a car approaching and you have the option of walking in front of it forcing it to stop, you wait, because the driver's time is more expensive than yours. Stopping the car and wasting time would impact him much more than it would you. It also has the side effect that it saves your life if the car doesn't stop for some reason.

Just a thought.

On wisdom

The Romanian language has a word: deștept. It means smart, but it leans into knowledgeable, so it means both "knowing things" and "thinking fast". There is no relation to wisdom and this is the case in other languages as well. Sometimes wise is used to denote knowledgeable, yet I don't think they are related. While to know things means to be able to recall things you have learned, wisdom, I've come to realize, means to understand what little you know. Someone might be wise and know very little and think rather slowly. Wisdom is the maturation of the soul, like a well kept wine it provides subtle flavors.

Even a superficial and forgetful person as myself can gain wisdom in time. It is important to note this, because as people get older, stuck between that limit of usefulness and the onset of senility, we tend to dismiss them, flaunt our new found (and invented) knowledge to their faces, ignoring a very important aspect of their evolution: wisdom. Sure, their wisdom might not apply to your field or need, but even if it were, are you acknowledging it?

Just a thought.

Wednesday, October 17, 2018

Tower of Babylon, by Ted Chiang

book cover As far as I can see, Ted Chiang's Tower of Babylon is the first thing he published. It's a short story (that you can find online for free) about two workers climbing the tower of Babylon to dig through the Vault of Heaven.

In this story the tower is not struck down and the people have been working on the tower for centuries. It's a fun read, although not particularly funny or captivating. It does show Chiang's penchant for speculative fiction, though. I liked it, but not enough for four stars and three seems too little. Read it. It's short and I don't want to write more about it than it is itself in length :)

Monday, October 15, 2018

Focus Assist in Windows 10 not working and Settings crashing

I am writing this post to let people know why a particular annoying problem happens on Windows 10 with regards to notifications. Before it was called "Focus Assist" it was called "Quiet hours" and you would turn it on to make annoying notifications not show while you were working or playing. After a Windows update, Microsoft renamed it to Focus Assist and turned it into a more complex setting, rather than on/off. But then the problems appeared.

Symptoms:
  • Notification traybar bubble is white (outline filled) and going over with the mouse it either says nothing or says "1 notification"
  • When you click on it, it shows no notifications and the bubble remains filled
  • If you right-click it, none of the options in the Focus Assist menu seem to be working and none are checked
  • If you go to Settings and click on Focus Assist, Settings crashes with no error message
  • You also may have Avast antivirus installed

People have been tracking the problem on this Microsoft forum: Settings app crashes accessing "Focus Assist" settings and here are our findings:

  1. The problem comes from Avast (or some other source) turning off the Windows Push Notifications User Service. Turning it on, restores Focus Assist functionality.
  2. Avast has something called Silent Mode, which many people use because Avast started pushing all these annoying messages lately
  3. In the Avast configuration (go to Menu , Settings, Components, scroll down the page until Performance, Do Not Disturb Mode, then Customize) there is a setting called "Silence notifications from 3rd-party apps". By default it's on. Turn it off and Avast will no longer kill the service
  4. If the cause of this behavior is different from Avast's silent mode, let me know. An immediate fix is to go to Services (services.msc), scroll down to Windows Push Notifications User Service (followed by underscore and some meaningless numbers and letters) and make sure it is started.


Hope this helps.

Thursday, October 11, 2018

A Conjuring of Light (Shades of Magic #3), by V.E. Schwab

book cover A Conjuring of Light ends the Shades of Magic trilogy, although apparently a new trilogy set in the same universe is on its way. All the major threads are closed, although some of them felt a little forced and some of the drama was clearly artificial. But at least it all ends, which is one of the major reasons I read this book, only to see how Victoria Schwab's characters will end up.

I felt that this was the more ambitious volume of the three in the series, with all three Antari having to interact, with foreign dignitaries stuck in the royal palace while it was under siege from a demented magical creature who believed itself a god, with ties with families and past revealed, with new places and new magic. However, the book was quite inconsistent. For example, there is a plan to use a spell to seal the god in a body. When it is inside one, they forget about it and fight him with knives and fire and what not. There is a spell that could restore Kell's memory. He wonders if he should use it, then we forget about it. As for Grey London (ours) and Black London (the one where the creature originated), they are completely ignored.

The personalities of the characters also change a lot, with everyone acting brave and selfless (sometimes to stupidity) as if forgetting we are talking about a ruthless street thief, a killer turned sociopath by years of torture and so on. To me it seemed as if the author wanted a tough story, but she couldn't help turning it into a classic hero quest with a happy ending.

Bottom line: I had fun reading the series, but I won't continue with the next trilogy. It's not bad, but it's not above average either.

Wednesday, October 03, 2018

Maintain boolean state in CSS? Use label and for with a hidden checkbox.

Kind of obvious, but I wanted to make it clear and to remember it for the future. You want to make something appear or not based on a toggle button, usually done by adding a click handler on an element with some code to determine what happens. But you can do it with HTML and CSS only by using the label element pointing to a hidden checkbox with its for attribute. Example:

   I have been toggled!

Here is the HTML and CSS for it:
<style>
     /*  you have to use a caption element instead of a control element inside the label, so a button would not work and we use a span to look like a button */
     .button {
       border: solid 1px gray;
       padding: 5px;
       display: inline-block;
       user-select: none;
       cursor: pointer;
     }
 
     /* the element after the unchecked checkbox is visible or not based on the status of the checkbox */
     #chkState + span { display: none; }
     #chkState:checked + span { display: inline-block; }
  </style>
  <label for="chkState">
    <span class="button">Toggle me!</span>
  </label>
  &nbsp;&nbsp;&nbsp;
  <input type="checkbox" style="display:none" id="chkState" />
  <span>I have been toggled!</span>

Update: You might want to use an anchor instead of a span, as browsers and special software will interpret it as a clickable, but not input control.

Update: You can, in fact, instruct the browser to ignore click events on a button by styling it with pointer-events: none;, however that doesn't stop keyboard events, so one could navigate to the button using keys and press Space or Enter and it wouldn't work. Similarly one could try to add an overlay over the button and it still wouldn't work for the same reason, but that's already besides the point. Anyway, either of these solutions would disable the visual style of the clicked button and web sites usually do not use the default button style anyway.

There is one reason why you should not use this, though, and that is usability. I don't know enough about it, though. In theory, a label should be interpreted the same as the checkbox by software for people with disabilities.

CSS3 in 30 Days - a YouTube tutorial that is worth checking out

There is this channel I am subscribed to, with various people discussing or demonstrating software development concepts, with various degrees of success. I want to tell you about this series called CSS3 in 30 Days which is very well organized and presented by a clearly talking and articulate developer, Brad Hussey. He's nice, he's Canadian.

And before you go all "I am a software programmer, not a web designer", try watching it for a bit. Personally I am sure I would have solved a lot of what he demos using Javascript. Changing your perspective is a good thing. Note it is about CSS3, which had quite a few improvements over the classical CSS you may know already.

Here is the first "day" of the tutorial:

Tuesday, October 02, 2018

Paranoia Agent (妄想代理人 Mōsō Dairinin), a thought provoking anime

The anime's characters Paranoia Agent (or Delusion Agent, maybe) is an anime by famous anime director Satoshi Kon, unfortunately killed by cancer in 2010. His work is always more than it seems, focusing on the inner worlds of people and how they all perceive things differently.

The anime is only 13 episodes and starts with a simple case of violent assault on the street and then becomes stranger and stranger until it is not clear which is real and which is in someone's head. It critiques the repressive Japanese society and human nature in general, it goes from police procedural to slapstick comedy, from horror to psychological drama. The ending is, as they say, a mystery that doesn't stay solved for long. I quite liked the anime and I recommend it highly. It is rarer and rarer to find Japanese anime which is not derivative or simply idiotic.

Monday, October 01, 2018

Doctor Zhivago, by Boris Pasternak

book cover It involves Russia! The story of how Boris Pasternak's Doctor Zhivago got popularized and received the Nobel prize for literature is fascinating and one of the reasons why my wife and I decided to read it as part of our own private book club. She loved the book, although she admitted she didn't understand a lot. I couldn't finish it because I didn't understand it at all!

Let me get this straight, this is not a bad book, the fault lies solely with me. That being said, I've read half of it before I decided to watch the TV adaptation from 2002 and realized I had no idea who anyone was. I had read half of a book, enjoying the way Pasternak describes individual lives, but I didn't remember one character or scene. And the explanation is simple: this is like Crash, on steroids, had sex with Gone With The Wind and had this bastard child around the first World War in Russia. People have three different names, plus nicknames, that the author just splays around without explanation. Events are described through short chapters that sometimes connect via a character seeing the same things from a different perspective or saying something about a character, using a different name than the one we read about it previously. And all these people keep bumping into each other again and again. Sometimes there is no rhythm in how things are written, sometimes it sounds like poetry. There is huge attention to some details and complete ignoring of others. And so on. It is not an easy book; it requires your full attention.

It is obvious that Pasternak loved people and he described their experiences and toils during times of great upheaval, but for him those paled compared with the love stories and the feelings of the characters involved. I can understand how he was confused on why people thought his book was against the Soviet system, where it was clearly about people. I am sure this book is great, it is just not for me. If you want to try it, I suggest you read the summary in Wikipedia so you understand what is going on and you do not read it in bits of 15 minutes in the subway.

Wednesday, September 19, 2018

Codename Villanelle (Villanelle #1), by Luke Jennings

book cover I started watching Killing Eve, the BBC TV series starring Sandra Oh and Jodie Comer, and I quite liked it. So I've decided to read the books it is based on, Codename Villanelle being the first one. The result is a mixed bag. I clearly prefer the TV show, but the book is not bad either. They are, I have to say, quite different.

While both focus on a police woman trying to find a highly skilled female professional sociopath assassin, the show adds a little dark humor, which is both nice and off putting, while the book is going for the full "secret world of spies and weapon technobabble" route, which is also half and half. I think Luke Jennings wanted to write a simple spy story, inspired by the likes of John le Carré, while the show screenwriters have more ambitious goals. Most of the little episodes there are based on stuff in the book, but wholly reinterpreted, the characters changed or added, their fates changed.

But enough about TV. The book is easy to read, but a bit one sided: kill after kill and the chase of the department that has to uncover not only the identity of the killer, but also who are the high placed people who help hide her tracks, without most of the emotional exposition that makes a character relatable. Funny enough, there is more emotional content related to the killer than the cop. Makes you wonder which one of them is the psycho sometimes.

In conclusion, I will not read the rest of the books, but I will continue to watch the TV show. I feel like reading the first book opened my eyes on what the characters were supposed to be and thus Codename Villanelle acted like a nice companion book for an interesting series that I enjoy watching.

Saturday, September 15, 2018

Binti (Binti #1), by Nnedi Okorafor

book cover I liked Binti, even it is a short story. It is my first contact with the Nigerian-American author Nnedi Okorafor and I loved how African ideas blended with science fiction concepts in Binti. I will probably read the others in the trilogy, sooner or later.

The story is about a girl that leaves her tribe and planet to go to a university she was just admitted to. Just when getting there, an alien race of Medusae kills everybody except her. You will have to read why that happens and how she becomes the ambassador of the aliens, because the story is short enough to get spoiled by a detailed review. The writing is not without flaws and the character is dangerously close to the one in Who Fears Death, but I felt that it made a lot more sense in the context of an interstellar Federation as in Binti.

Read it. It was fun.

Who Fears Death, by Nnedi Okorafor

book cover Who Fears Death is an interesting book mainly because of the Nigerian background of Nnedi Okorafor, the author, and the subject vaguely inspired by the atrocities in Sudan. The fantasy plot revolves around a girl resulted from intertribal rape who has to deal with the injustices of her world using her magical powers. Imagine a Harry Potter book in a German extermination camp scenario where he is a Jewish girl and you get close. In fact, this and other books on the subject of tribal hatred in Africa should make any white supremacist feel good about themselves, because there is no worse racism than between violent religious uneducated tribes that pretty much look the same.

Yet the book's interesting factor kind of stops there. The African culture references throughout the story keeps it entertaining, but the plot is a classic hero arc with pretty much all of the flaws inherent to the genre. Hero is young and powerless, discovers powers, is guided by mentors that keep from her just about everything there is to know and use up until she needs it, evil villain to whom she has a direct connection and opposition, friends that are there just to support the hero, things that happen for no other reason than they needed to further the story in a particular direction, she has amazing powers but uses them sparingly and often is stopped from using them for moral reasons by her friends and so on. In fact, it gets a lot worse, considering the hero is an African girl who could get killed at any moment by an angry mob just for the color of her skin, moving around in a society where even her boyfriend thinks it's OK to keep things for himself or treat her patronizingly just because of her gender, not to mention the old male mentors.

So while the book is interesting and well written, it does have a major flaw in the way it was structured as a story. Perhaps the docile Black woman who occasionally gets upset and then regrets it resonates better with African audiences, but for a Westerner it might get a little frustrating. It is a book worth reading, mainly because of the culture shock one experiences while reading it, but it could have been better.

Monday, September 10, 2018

The Great Influenza: The Story of the Deadliest Pandemic in History, by John M. Barry

book cover Who writes like this in this day and age? Around 10% in the book I was convinced The Great Influenza was written sometime in the middle of last century, when people still did painstaking research and paid attention to every detail, not like now, when researching something involves trying different Google keyword combinations ad nauseam. And how many people can pack so much information and knowledge into a book that is also easy to read and incredibly captivating? Apparently, John M. Barry, with this book published in 2004.

I can't stress enough how much I liked this book. I would give it 6 stars out of 5 just because I rated books with 5 stars and this is an order of magnitude better. Imagine that I only found it because I was curious about the influenza epidemic from 1918, what is popularly known as The Spanish Flu, and at the time I was worried that one book on the subject would clearly not be enough. Not only would it present just one point of view, but surely various interests or lack of resources would influence (pun not intended) the end result. Instead I was shamed, that is the right word, by the amount of care the author used to research and write this book.

In order to explain what the book is about I would have to write a lot about The Spanish Flu, a disease that killed between 50 and 100 million people globally within six months in the middle of a five year world war that killed just 20 million in military and civilian deaths combined. Yet very few people care about it or even know about it. There are few documents about it, no movie that I know of, very few books. Inside the pages of The Great Influenza are the seeds of a dozen Steven Spielberg war movies, various crossings of The Outbreak with The Knick and a few Netflix series - I find it criminal that no one thought about it so far. Therefore, I urge you to first read a little about the disease itself, on Wikipedia and Google like normal people, then read this book. It will blow you away.

John Barry is clearly an investigator at heart. Not only does the work for this book, but clearly empathizes with the people who fought to understand disease and find cures, locked in laboratories and sacrificing everything for understanding. He calls them The Warriors, the name of the first part of the book, where he describes the history of medical science in the Unites States and the people how would ultimately champion it further and fight the outbreak. The second part (out of ten) describes the disease: a type of influenza so virulent that it makes the immune system destroy the body it's supposed to protect. Only from the third chapter on we start reading about how the pandemic started, what influenced its movements, how people reacted and so on. The last two chapters are solely about the after effects (almost as important as the ones during the pandemic). A whopping 10% of the book is just notes and thanks and references.

Also relevant, I think, is the fact that Barry is American and clearly proud of it. The book has a very American centric view, not only because of the nationality of the author, as probably because it would have been a lot more difficult to research the actual events in other parts of the world. Yet even for an American patriot the book is filled with outrage at the way governments and local authorities and narrow minded bureaucrats treated the disease and the people affected by it. For an European as I am, the way a fanatical American president managed to turn an entire country into a weapon, effectively disabling the values that Americans are known for: democracy, freedom of speech, pursuit of happiness, etc, is beyond chilling. America was a sleeping giant way before Pearl Harbor and the way it awakens and consumes everything and everyone that stands in its way is ironically similar to the way the influenza virus swept the world. One of the main reasons people don't know about the disease is because any mention of it that could have affected morale during times of war was censored in the land of the free. The name Spanish Flu itself comes from the fact Spain was not censoring its media at the time.

Bottom line is that I urge everyone to read this book. It's a wonderful example of how one man can dedicate seven years to research and document something as scary, monumental and mysterious such as the great influenza pandemic of 1918, analyse it from multiple viewpoints, name, quote and praise the people who were right in the midst of battle, the unsung scientist heroes of unheard of laboratories and the people who gave them the wind under their wings. Great job, John Barry, great job indeed!

Friday, August 31, 2018

How to generate Windows Installer logs for all installs

There is a way to execute an installation using msiexec, like this: msiexec /i MySetup.msi /l*v "mylog.log", but what if you routinely install stuff on a machine and want to be able to read the log only when there is a problem? Then you can use the group policy editor to set it up:

  1. Run "Edit group policy" (gpedit.msc)
  2. Go to Computer Configuration → Administrative Templates → Windows Components → Windows Installer
  3. Select "Specify the types of events Windows Installer records in its transaction log"
  4. Select "Enabled"
  5. Type any of the letters in 'voicewarmupx' in the Logging textbox
  6. Click OK





This will create the following registry entry:
[HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\Installer]
  "Logging"="voicewarmupx"
  "Debug"=dword:00000007

Warning: this setting will add time to the installation process based on the options selected. Here is a list of the possible options:
  • v - Verbose output
  • o - Out-of-disk-space messages
  • i - Status messages
  • c - Initial UI parameters
  • e - All error messages
  • w - Non-fatal warnings
  • a - Start up of actions
  • r - Action-specific records
  • m - Out-of-memory or fatal exit information
  • u - User requests
  • p - Terminal properties
  • + - Append to existing file
  • ! - Flush each line to the log
  • x - Extra debugging information. The "x" flag is available only on Windows Server 2003 and later operating systems, and on the MSI redistributable version 3.0, and on later versions of the MSI redistributable.

The log files will be found in your %TEMP% folder, usually C:\Users\[your user]\AppData\Local\Temp.

Thursday, August 30, 2018

A pattern of encaspulation of mock objects for unit tests

I wrote some unit tests today (to see if I still feel) and I found a pattern that I believe I will use in the future as well. The problem was that I was testing a class that had multiple services injected in the constructor. Imagine a BigService that needs to receive instances of SmallService1, SmallService2 and SmallService3 in the constructor. In reality there will be more, but I am trying to keep this short. While a dependency injection framework handles this for the code, for unit tests these dependencies must be mocked.
C# Example (using the Moq framework for .NET):
// setup mock for SmallService1 which implements the ISmallService1 interface
var smallService1Mock = new Mock<ISmallService1>();
smallService1Mock
  .Setup(srv=>srv.SomeMethod)
  .Returns("some result");
var smallService1 = smallService1Mock.Object.
// repeat for 2 and 3
var bigService = new BigService(smallService1, smallService2, smallService3);

My desire was to keep tests separate. I didn't want to have methods that populated fields in the test class so that if they run in parallel, it wouldn't be a problem. But still, I didn't want to copy paste the same test a few times only to change some parameter or another. And the many local variables for mocks and object really bothered me. Moreover, there were some operations that I really wanted in all my tests, like a mock of a service that executed an action that I was giving it as a parameter, with some extra work before and after. I wanted that in all test cases, the mock would just execute the action.

Therefore I got to writing something like this:
public class BigServiceTester {
  public Mock<ISmallService1> SmallService1Mock {get;private set;}
  public Mock<ISmallService2> SmallService2Mock {get;private set;}
  public Mock<ISmallService3> SmallService3Mock {get;private set;}
 
  public ISmallService1 SmallService1 => SmallService1Mock.Object;
  public ISmallService2 SmallService2 => SmallService2Mock.Object;
  public ISmallService3 SmallService3 => SmallService3Mock.Object;
 
  public void BigServiceTester() {
    SmallService1Mock = new Mock<ISmallService1>();
    SmallService2Mock = new Mock<ISmallService2>();
    SmallService3Mock = new Mock<ISmallService3>();
  }
 
  public BigServiceTester SetupDefaultSmallService1() {
    SmallService1Mock
      .Setup(ss1=>ss1.Execute(It.IsAny<Action>()))
      .Callback<Action>(a=>a());
    return this;
  }
 
  public IBigService GetService() =>
    new BigService(SmallService1, SmallService2, SmallService3);
}
 
// and here is how to use it in an XUnit test
[Fact]
public void BigServiceShouldDoStuff() {
  var pars = new BigServiceTester()
    .SetupDefaultSmallService1();
 
  pars.SmallService2Mock
    .Setup(ss2=>ss2.SomeMethod())
    .Returns("some value");
 
  var bigService=pars.GetService();
  var stuff = bigService.DoStuff();
  Assert.Equal("expected value", stuff);
}

The idea is that everything related to BigService is encapsulated in BigServiceTester. Tests will be small because one only creates a new instance of tester, then sets up code specific to the test, then the tester will also instantiate the service, followed by the asserts. Since the setup code and the asserts depend on the specific test, everything else is reusable, but also encapsulated. There is no setup code in the constructor but a fluent interface is used to easily execute common solutions.

As a further step, tester classes can implement interfaces, so for example if some other class needs an instance of ISmallService1, all it has to do is implement INeedsSmallService1 and the SetupDefaultSmallService1 method can be written as an extension method. I kind of like this way of doing things. I hope you do, too.

A Gathering of Shadows (Shades of Magic, #2) , by V.E. Schwab

book cover I liked A Gathering of Shadows more than the first book in the series, A Darker Shade of Magic. It felt more grounded, more meaty. A large part of the book is split into two parts as Kell and Lila walk their separate paths only to collide in the middle and be tragically broken apart towards the end. There are more characters, more world building.

Not all is rosy, though. There are still huge swaths of pointless exposition, followed by truncated bits of action. In this book, a part of the focus is on a magical tournament that all characters are somehow involved with, so a big part of the story you wait to see what is going to happen when wizards fight each other. And then there are the scenes of battle which are either a few paragraphs long or simply summarized because Victoria Schwab didn't really care for the characters fighting or because she wanted to focus on a (bloody hell, I am so tired of this cliché!) love triangle. I also wish Lila would have had a more interesting arc, other than going to sea for 4 months and returning a world class magician without an actual event to drive her evolution. It's basically a "she read some books, watched some YouTube tutorials and she was smart" story.

I found it funny that this book ends in a gigantic cliffhanger. So large, in fact, that in the acknowledgements at the end the author apologizes. She managed to write four books that didn't end in cliffhangers, so she is allowed this one, she reasons. Is this an emotional need for writers, to hang their readers? Something so powerful that they just have to do it from time to time in order to feel alive? Kind of like magic, eh? :)

Anyway, I am happy to say that I will probably read the next book in the series and not only because of the cliffhanger, but because I am actually interested in what the characters will do. I have other things on my plate first, though.