Friday, October 7, 2011

f.lux - software to make your life better

Just came across a small piece of software, f.lux, that changes the color temperature for the displays when the night comes. It seems pretty interesting and the author says it is very helpful. I tend to agree, though some time to accommodate with the night settings might be needed.

On the f.lux page there is also a Sleep Research that might be worth reading.

Saturday, September 10, 2011

The Open File Dialog Plurality

Open a Notepad instance and check how many threads the notepad.exe creates using the Task Manager. (If the Task Manager does not show the number of threads column in the Process tab, then enable it from the View > Select Columns... menu).

One thread and 1364 KB of memory (on a Windows 7 64 bits OS).

Now, in notepad open the Open File dialog (and keep it open), and check the number of threads again.

22 threads! And 9400 KB of memory.

Now cancel  the Open File dialog. A number of 18-21 number of threads will still be there!

Is this a waste of resources? Or is this an intended waste of resources?

Thursday, July 28, 2011

WM_PAINT and WM_ERASEBKGND return values

It's funny that, the return values that represents that the message was processed for the WM_PAINT and WM_ERASEBKGND messages differ. From MSDN:

WM_PAINT:
  • An application returns zero if it processes this message.
  • An application should return nonzero if it erases the background; otherwise, it should return zero.
So when you try to convince Windows not to repaint in any way one window (in which you render with OpenGL for example), you get some WndProc like this:

Tuesday, June 7, 2011

Bind to static ObservableCollection



Wednesday, May 4, 2011

Invoking anonymous methods in WPF

To execute some work on a Dispathcer, synchronously, the Dispatcher.Invoke method is used, which takes a Delegate as an argument representing the work to be done.

However, as stated on MSDN, the Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types. - Hence the need for the explicit cast to a derived-from-Delegate type.

Most languages implement a delegate keyword, and compilers for those languages are able to derive from the MulticastDelegate class; therefore, users should use the delegate keyword provided by the language.

Here are a few ways to synchronously invoke an anonymous method:

by declaring a delegate type and casting the delegate to it:

Starting with version 3.5, .Net contains an Action class that is defined like:

so it can be used:

by casting a delegate to an Action:

by casting a lambda expression to an Action:

by creating an instance of Action passing a delegate to its constructor:

Sunday, April 17, 2011

Removing EventHandler from itself

Lambda expression offers a great way to write less code for registering event handlers:

But what if we need to remove the event handler after the WriteLine call, from the event handler itself? It is possible, if the lambda expression delegate had a name:

One problem with this though, when initializing the handler variable, inside the code that initializes it, at the line button.Click -= handler we access the handler which at this point is not initialized yet! So, to fix this, simply initialize it with null first (which might look a little bit awkward):

Monday, April 11, 2011

Managed GetLastError

When p/invoking a native method from managed code, it's not ok to call GetLastError to check for an error, because internally the .Net engine can call other API native methods who might override the last error you actually want from the method you invoke. The solutions is Marshall.GetLastWin32Error.

Tuesday, February 15, 2011

Top 10 Programming Quotes

10. If debugging is the process of removing software bugs, then programming must be the process of putting them in. -  Edsger Dijkstra

9. The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.  – Tom Cargill

8. “There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.”-  C.A.R. Hoare

7. Measuring programming progress by lines of code is like measuring aircraft building progress by weight. – Bill Gates

6. “Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” – Martin Golding

5. “The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.” – Seymour Cray

4. Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. – Rick Cook

3. “Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris.” – Larry Wall

2. “Sometimes it pays to stay in bed on Monday, rather than spending the rest of the week debugging Monday’s code.” – Christopher Thompson

1. Walking on water and developing software from a specification are easy if both are frozen. –Edward V Berard

Source: ginktage.com