In English :(
Saturday, June 7, 2008
Saturday, April 19, 2008
Careful with C#'s extension methods!
C# extension methods seem cool -- for example, let's bring Java's monitor-related methods into all C# objects:
public static class MonitorExtensions
{
public static void Notify(this Object obj)
{
Monitor.Pulse(obj);
}
public static void NotifyAll(this Object obj)
{
Monitor.PulseAll(obj);
}
public static void Wait(this Object obj)
{
Monitor.Wait(obj);
}
public static bool Wait(this Object obj, int millisecondsTimeout)
{
return Monitor.Wait(obj, millisecondsTimeout);
}
public static void Synchronized(this Object obj, Action action)
{
lock (obj)
{
action();
}
}
}
Ignore that all methods follow the .NET guidelines instead of being camelCase. Here's what you can do now that Object has been extended:
Object queueNotEmpty = new Object();
queueNotEmpty.Synchronized(() =>
{
// ...
queueNotEmpty.Notify();
});
Ignore that you don't type:
synchronized (queueNotEmpty)
{
// ...
}
Hell,
o.Synchronized(...)
looks even more OO-like (and sexier)[1]!
It turns out extension methods are not that cool.
I wanted to create a simple producer/consumer sample using the extension methods defined above. However, for some of the threading-related code I used an AutoResetEvent and a ManualResetEvent. Only the thing is, I got SynchronizationLockException when I ran the sample. Why? Certainly not because I can't use condition variables correctly. The crash as it turned out was caused by the fact that I was calling the Wait method on an auto-reset event, as in:
threadReady_.Wait();
// thread started...
... but because I've extended Object, all classes got the Wait, etc. extensions, so instead of executing AutoResetEvent's Wait method, the sample was executing the extension one and since I hadn't locked the object got the exception.
The solution? Don't extend Object! Be explicit and write intentional code - what you need is a condition variable, not to make every object look and feel like one:
public class ConditionVariable
{
public void Notify()
{
Monitor.Pulse(this);
}
public void NotifyAll()
{
Monitor.PulseAll(this);
}
public void Wait()
{
Monitor.Wait(this);
}
public bool Wait(int millisecondsTimeout)
{
return Monitor.Wait(this, millisecondsTimeout);
}
public void Synchronized(Action action)
{
lock (this)
{
action();
}
}
}
It's still "cool" and it doesn't mess with everything else ;)
[1] And it's not just being cool and sexy. I could now design a thread deadlock detector on top of that (which could only detect deadlocks in objects locked using these very extension methods), or perhaps if I'm the Edison, and not Tesla's type - log locks, waits, etc. and examine the logs manually to find deadlocks.
Labels:
C#,
extension methods,
lambda,
monitor,
rant
Tuesday, April 15, 2008
Here, get the latest... eer, hang on...
I downloaded Style Builder for Google SketchUp today and when I tried to install it it crashed miserably:
---------------------------
16 bit MS-DOS Subsystem
---------------------------
C:\NewTech\SketchUp\GOOGLE~1.EXE
The NTVDM CPU has encountered an illegal instruction.
CS:0f55 IP:fff9 OP:96 3a ff fe ff Choose 'Close' to terminate the application.
---------------------------
Close Ignore
---------------------------
Looks like a broken download, isn't it? So I headed up to download it again, only to get the infamous 404. Nice :(
Labels:
frustration,
google,
rant,
sketchup
Sunday, April 13, 2008
Apple's Safari hates MC Hammer...
... or at least it seems so! Just try to open MC Hammer's blog (no, I'm not reading it - just stumbled upon it ;) and your Windows version of Safari will spin loop taking 100% of one of your CPU's cores (don't do it if you have just one ;).
Friday, April 11, 2008
You gotta love Google Groups
This morning I wanted to clean up my Gmail account, which now exceeds 3GB and was frustrated Gmail doesn't have something like Outlook's "Large mail" filter (which would filter out only big messages, or these with big attachments).
Apparently, you could search for, say, "in:anywhere has:attachment filename:wmv" and find all those funny vids your wife and friends send to you on a daily basis, but then I need all big files, and not just the video ones. I couldn't find out how to do it and clicked on the Gmail Help Group link.
I have to admit I was so frustrated that I was about to post the question without even searching the archives for similar messages. To my dismay however, Google has done great job with Google Groups' web interface and I was pleasantly surprised to see this:
You gotta love Google Groups :)
Saturday, April 5, 2008
Aaah, the Tiguan...
The other day was a horrible working day. Everything sucked all day, colleagues and clients deserved to die, and I was in a horrendous mood until I got the call from the VW shop! Boy did they make my day!
The Tiguan is fucking amazing. If it were a girl I'd cheat w/ her on my wife (OK, I wouldn't ;) Anyway, I jumped on it and drove it home, but didn't have time to test-drive it as I want to (+ I was scared more or less because that's my first new car).
Yesterday, however, was the day! A friend of mine came to my place to take a look at it (he was actually the friend who "tricked" me into buying it ;) and he showed me what the Tiguan is all about. You know, the feeling of taking a 90 degree turn at 100 km/h is a holy shit moment! Taking several successive ones is a HOLY SHIT one :) Make no mistake - after he jumped off I had quite a few :O :O :O moments myself ;)
You don't want a car without ESP, trust me! And ESP is one of the 7 base security features built into the Tiguan, including ASR, ABS, EBV, MSR, BA, SRS (hell I don't even know what some of these mean! ;).
Oh, and for the gloating MFs out there: trust me, the engine is quite quiet! The 2.0 TDI (mine) engine is Common Rail and purrs like a cat :P
And finally, a vanity moment: the (Track & Field) Tiguan looks much better than all CUVs out there! Everybody on the road is looking at it (perhaps it's because it's so cool and not because it's so new... ;)
Aaaah the Tiguan...
Tuesday, April 1, 2008
SxS woes
For the 3rd time (in about an year) I spent all night reading every bit of information I could get my sore eyes about SxS, private assemblies, shared assemblies, manifests, removing public key tokens, adding .2.config to .dlls, unchecking embed manifest in VS, creating custom manifests, embedding custom manifests, defining magic macros, bugs about swapping debug and release manifests, bugs about prefering shared assemblies to private ones, binding redirects, and a lot more crap like that which could only make your head hurt like hell and make you actually crave for the old .dll hell.
And here's what I tried to do - it's so simple it almost hurts I couldn't do it.
I'm not trying to deploy a release .dll w/ a dependency on the the C runtime. I'm simply trying to register a debug version of a COM component I built on my development machine, which has a dependency on the C runtime so I can debug it. However, no matter what trick I pulled out of Google and my ass it didn't work and that is the end of it!
The "solution": I built a release version of the component and debugged it.
We have a saying: "The road to hell is covered laid with good intentions"... SxS is one of them.
Subscribe to:
Posts (Atom)

