Just Attach

Attach to what? To the process you want to debug, of course. How many developers attach to and debug arbitrary processes running on their machines? Very few, I’d imagine. And I’d think that even those few people typically prefer Windbg or an equivalent debugger to the one supplied with Visual Studio. Which means that aside from ASP .NET developers, almost all of us using Visual Studio attach to the application that we are working on and nothing else. To attach 1. You summon the “Attach to Process” dialog by hitting Ctrl + Alt + P. Or if you are a … Continue reading Just Attach

Anonymous methods as event handlers – Part 2

The previous post discussed having anonymous methods as event handlers and ended with a question – why doesn’t unsubscription work while subscription works out alright? Vivek got the answer spot on – the way the C# compiler handles and translates anonymous methods is the reason. Here’s the code involved. 1: public void Initialize() 2: { 3: control.KeyPressed += IfEnabledThenDo(control_KeyPressed); 4: control.MouseMoved += IfEnabledThenDo(control_MouseMoved); 5: } 6:  7: public void Destroy() 8: { 9: control.KeyPressed -= IfEnabledThenDo(control_KeyPressed); 10: control.MouseMoved -= IfEnabledThenDo(control_MouseMoved); 11: } 12:  13: public EventHandler<Control.ControlEventArgs> IfEnabledThenDo(EventHandler<Control.ControlEventArgs> actualAction) 14: { 15: return (sender, args) => { if (args.Control.Enabled) actualAction(sender, args); … Continue reading Anonymous methods as event handlers – Part 2

Anonymous methods as event handlers – Part 1

The syntactic sugar offered by anonymous methods makes them great candidates for writing event handlers; together with smart type inference, they reduce the amount of code written by an order of magnitude. And that’s without considering the power offered by closures. With event handlers, closures allow you to kind of “stuff” extra parameters into the handler, without changing the actual number of formal parameters. This blog post shows a situation where an anonymous method acting as an event handler makes code simpler, and then goes on to show a gotcha with un-subscription and anonymous methods. Here’s a simple Control class … Continue reading Anonymous methods as event handlers – Part 1