Wednesday, July 08, 2009

Enforcing a minimum of one check mark

Good evening, ladies and gentlemen. Here's a little code I tossed off recently in the Computer:

So, here's how I made sure that at least one check box remains checked in one of my forms. A quick search didn't turn out any vb.net samples for doing this, so here is my version so that I will have a source of copy-paste the next time I google :P

Private Sub dlgViewImageHistogram_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  chkRed.AutoCheck = False
  chkGreen.AutoCheck = False
  chkBlue.AutoCheck = False
  chkIntensity.AutoCheck = False
  chkIntensity.Checked = True
End Sub

Private Sub checkColor_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
chkRed.Click, chkGreen.Click, chkBlue.Click, chkIntensity.Click
  Dim checkCount As Integer
  For Each chkBox As Control In pnlChannels.Controls
    If Not TypeOf chkBox Is CheckBox Then Continue For
    checkCount = IIf(CType(chkBox, CheckBox).Checked, checkCount + 1, checkCount)
  Next

  If checkCount = 1 And sender.checked = True Then
    Exit Sub
  Else
    sender.checked = Not sender.checked
  End If

End Sub


Basically, you need to set the AutoCheck property of the checkboxes to 'false' to take over control of the setting of the 'Checked' property.
Also, make sure that you have at least one check box checked. I have put all the checkboxes in a Panel control. Maybe there's a better way of grouping the check boxes, but I haven't read up on it.

Monday, June 29, 2009

Bitmap cloning is not Bitmap cloning

Almost 7 years ago, I had a pet project called 'PBrush', a small paint program created in VB 6. It was where I experimented with implementing the Undo/Redo features, image processing (emboss/invert/edge-detection, etc) in addition to the standard tools found in MSPaint.
I lost the project files when my first RAID-0 array broke and I've forgotten about it since then. (Since then I don't keep my project files on a raid-0 array.)

So, here I am today trying to get back in touch with VB.net after quite some time working in the Java/JSP platform and out of nowhere, I am reminded of the old PBrush project of mine. Not surprising really, since I had the most fun working on that project, ya know, trying different things with pixels and coming up with names for them :) Good times.

So anyway, I downloaded VB.net 2008 express edition and began coding about a few days ago in my spare times. I've already got a couple of kernel-based filters for edge-detection (Sobel. Scharr) coded down using basic loops for now. I heard directx methods will be faster for this, so I might try to re-write these functions.

For now, I am using a PictureBox for the main display and pass its Image property to my functions that will do the image processing. I had it all working in a day. But, as these filters are time-consuming, I noticed that the form would go to 'Not Responding' state after about 3 seconds of processing. This turns out to be caused by the form and its child controls staying "invalided" for too long. That is, the controls want to re-paint themselves, but can't because of the current thread is busy executing the image proessing routine.
First I tried the good old Application.doEvents()
Turns out that this method is old alright but certainly not "good". Some call for its timely death, even, and I can see their point. After adding this method call, my functions were taking about ten times more time to complete. Online documentations say that this is expected behaviour(!). Called 're-entrancy', this function halts any method that's been running, processes form messages and, get this, re-enters the function that it interupted.

Well, I said "no thank you. Don't come again" and started looking into putting the functions in separate threads instead.

Now, this is my first (not counting lab projects) project with threading. But it turns out that putting a function call into a separate thread is too easy. I used the BackgroundWorker class for doing this. Now, the functions work quite faster and the UI doesn't stay "invalidated" either. Seems perfect on the surface. But the devil is in the details, yes?

Yes. To better illustrate this point, let me give you a brief overview of the code.
So, I have a Picture box that holds the bitmap that needs processing. This control is also the one that is visible to the user, so it has to stay "validated". When an image filter is chosen by clicking on a menu item, I pass the Image object of the PictureBox to the appropriate function to be processed in the thread's DoWork method. Like so:

Dim bmpSrc as Bitmap
bmpSrc = pbMainDisplay.Image
e.Result = pdc.applyFilter(bmpSrc)


I noticed that "sometimes", when the BackgroundWorker is executing an image processing routine in the background, the Picture box will show a big red 'X' on it instead of the bitmap and an exception will be thrown stating "Bitmap region is already locked." on the line where I try to do a GetPixel().

I thought that it was simply a matter of adding a clone() method call on the Image property as this would create a new Image object instead of passing the reference to the Image. When this didn't work, I searched for every usage of the bitmap on the code and added the Clone() method call to it. Finally, when all the Image properties were adorned with the Clone() method, I ran the project again.... and met with the same problem, unchanged.

I looked up this issue online and found a similarly frustrated developer's blog entry on this. I gladly gave his method a try, like this:

Dim bmpSrc as Bitmap
bmpSrc = New Bitmap(pbMainDisplay.Image)
e.Result = pdc.applyFilter(taMain).Clone()


Unfortunately, this approach didn't work for me either.
Frustrated, I tried to either set the visibility of the Picture Box to false whenever the picture is being worked on by the background thread. But this would make the picture not visible to the user, which is ultimately not very "smooth".
So, for now, I have opted to intercept the paint events of the picture box and disallowing it whenever the background thread is busy, like so:


Private Sub pbMainDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbMainDisplay.Invalidated
If bwMain.IsBusy() Then
Exit Sub
End If
End Sub

It might be a workaround, but it's the only thing I've tried that works.


Update: 8th July, 2009

I have since moved the code that sets the Bitmap variable from the background worker's DoWork event to the method that calls the background worker instance's RunWorkerAsync() method, et voila, no stinkin workarounds required now.
It had been a matter of inter-thread chatter, I suppose, between the main application thread and the background worker.

Sunday, February 01, 2009

A bad case of upgraditis with a touch of Nehalem

The chronic (and costly) disease of upgradeitis has reared its ugly head again. And this time, it's the worst yet. The symptoms were all over the place, I just didn't realize it.

The Symptoms
It all started when my mouse finally decided to die on me. The shop-keeper said that my mouse is out of warranty period and there's no way of resurrecting my high-dpi pet. It had pined-for (and reached) the fjords. Fine, I said. It is time for a mouse upgrade anyway, I said. In the meanwhile, I am back to using my faltering old mouse.

I was researching (choices, choices) my next mouse when another peripheral, my Razer Barracuda HP-1 5.1 channel gaming headphones decided to act up and its front-left channel went silent, permanently. I was disgruntled at this and asked Razer web store for a refund instead of a replacement. I got my refund, but now I was without a high-end headphone pair. Fine, I said. It is time for a mouse upgrade and a high-end headphone pair upgrade, I said. In the meanwhile, I am back to using my old and so-so philips shp805 cans.

While all this was happening, I had noticed that my rig, especially after my new Dell 24" upgrade, was being brought to its knees by recent gaming titles. Playing at less than native resolutions and/or with decreased eye-candy might be an option for most, but not for me (of course, Crysis would be the one exception to this. For now.). This, of course, would entail upgrading the core system components, i.e., processor and video card.

My initial findings indicated that an upgrade to a quad core 45nm Penryn was the best upgrade path for now. But my current motherboard, the legendary Asus P5N32-E SLI, has iffy support for 45nm CPU s, and this of course calls for a motherboard upgrade, and possibly a memory upgrade, depending on the motherboard chipset or other factors as you will find out below.

Power Situation
Now there's another reason that enticed me to consider a motherboard upgrade, and that is the power consumption and, in-turn, the heat situation. The 680i chipset of nvidia that is in my motherboard is a sucker for watts and produces so much heat that I am still amazed that the solder hasn't melted and run off the motherboard. High power draw also decreases the UPS's on-battery time, which is made all the more important now because of the recent scheduled power-cuts happening all over Tamil Nadu. Another smaller issue is of the electricity bill. To give you a clue of why this may be a concern, take a look at the current system power draw as I type this blog post:
Free Image Hosting at www.ImageShack.us

All these reasons create a case against the current generation (65nm fabrication) mother board/CPU combination and the move toward a more recent (45nm) componentary. So, I had almost decided on the Intel Q9450 CPU on a P45/780i chipset, with the possibility of moving to a power-efficient DDR3 memory modules. And hence I went about trawling the inter-wibble for reviews on the various brands of the chipsetery. I had selected the CPU, but the choice of motherboard and memory was a lot tough to make. Inter-wibbling after a day of work, I took about a month's time looking around for the perfect deals or looking up on some fault or the other that someone in a forum would report regarding one of my components.
Many a times I had filled up the online shopping carts with a motherboard or a RAM module and come close to clicking on the checkout button. I have also asked most of the shops in Ritchie street for the best deals on the components. I heard that Intel was slashing the price of the CPUs. So I waited for that.

In the interim period of this waiting game did I hear of the arrival of Nehalem (pronounced NAH-HAY-LEM. Bet you didn't pronounce it that way till now. I wasn't either!) CPUs "next month". So I decided to wait some more. In the PC Hardware universe, or heck the entire electronics universe, those who wait will get better products at a better price, but at the price of owning an aged hardware that has also depreciated in resale value. And to live in India is to live a year in the past, as most electronic gadgetery is not deemed suitable for Indian consumption (unless at a heavy premium) until it's novelty has been well worn off in the rest of the World. But to heck with resale value, I decided to wait.

Fast-forward a couple of months, now I have committed to a Core i7 (920) CPU (previously codenamed Nehalem), a X58 chipset motherboard (MSI Eclipse) and 6GB of DDR3 low volt memory modules (G.skill 3x2GB 1600MHz 8-8-8-21 @1.6-1.65V PI-Black series). And about the ageing vide0 card that is my Geforce 8800GTX; it's predecessor will be a pre-SLI ed monster called GTX 295. Yep, after loathing SLI, mainly for its chance of not working in non-SLI-supported games and too much hot-air (literally) for non-linear performance gain (2 cards for around 150% perf increase), I am finally (and tentatively) getting on the SLI-bandwagon. Bring on the kool-aid.


Now, sharp readers might've noticed that the choice of a 130Watt TDP Core i7 processor and a bleeding-edge SLI graphics card are in direct opposition to the points I mentioned under Power Situation. But, the truth is that, because these are bleeding edge componentry, they have good power saving features that make them the very power efficient (i7 , especially, consumes lesser power than any contemporary quad-core processors out there) in IDLE state. Since my computer spends most of its time idly downloading torrents, this is the state in which it will spend most of its time. The LOAD power consumption, is, of course, is among the most power hungry of all. But I really don't care about this as the time spent in fully loaded condition is very small. You get the idea.


That leaves me with the Mouse and Headphones in my shopping list:

After my bad experience with headphones that only vibrate your noggin in the name of "bass", I am forever apprehensive of headphones with so-called "sub-woofers". I am currently researching on the subject of Headphones. Maybe, I could go for a "virtual surround" or Dolby Headphones. Still a lot to research on this subject so I will provide an update when I know more about the choices.

I am almost done with the mouse selection though. It could be the much revered game mouse, the Logitech MX518, or it could be the new champion Logitech G9. Or perhaps it could be the new-kid-on-the-block Microsoft Sidewinder mouse. Razer's DA, CH, Lachesis, etc are dead reptiles to me. While I ponder the choices, I also catch a glimpse of some great keyboards (mmm... MS Reclusa... Oooh, Razer Lycosa...). Come to think of it, I am kinda about the non-illuminated keys on my Microsoft Multimedia keyboard...